Read more

Linux: Running a program with a different locale than your default

Arne Hartherz
September 22, 2014Software engineer at makandra GmbH

When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

I will use the date command and print the current weekday, just for the sake of an example.

Changing the locale using environment variables

Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.

$ date +%A
Montag

$ LC_ALL=C date +%A
Monday

C is usually fine to simply get English output (though there is more to it Show archive.org snapshot ).

If LC_ALL does not work, try using LANG (if that still does not work, try LANGUAGE):

$ LANG=C date +%A
Monday

There are also other (more specific) environment variables, like LC_MESSAGES.
You can list them using the locale command.

Locale names

Note that you can only use locales that are actually available on your system.

Using LC_ALL=de will only work if there really is a "de" locale -- Ubuntu will not automagically guess something like de_DE.utf8.

Run locale -a to list all available locales.

To generate other locales, use the locale-gen command.

$ sudo locale-gen es_CR.utf8
Generating locales...
  es_CR.UTF-8... up-to-date
Generation complete.

$ LANG=es_CR.utf8 date +%A
lunes
Posted by Arne Hartherz to makandra dev (2014-09-22 13:12)