Posted about 5 years ago. Visible to the public.

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

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.

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.

Copy
$ date +%A Montag $ LC_ALL=C date +%A Monday

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

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

Copy
$ 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.

Copy
$ sudo locale-gen es_CR.utf8 Generating locales... es_CR.UTF-8... up-to-date Generation complete. $ LANG=es_CR.utf8 date +%A lunes

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
almost 4 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more