Sunday, April 11, 2010

Mixed Locale in Ubuntu

If you're not a native English speaker the odds are that you most likely want to change the default locale. Well, the right thing in that case would be to select the correct language during installation. Or perhaps you come from a multilingual country and a single locale just won't cut it. I'm from Switzerland where we have four official languages (English not included - yet). I like to have my OS talking to me in English, like the makers intended, but I also want the currency, date & time, etc. formatted in the familiar way.

Installing necessary locales
Before you can start to tweak the locale, you must first install all locales you are planning to use. 

The easiest way to install support for additional languages in Ubuntu is through the aptly named 'Language Support' tool. It is located in System -> Administration -> Language Support. This tool will also allow you to specify the language of your user environment as well as the language for the login screen.  I keep both settings at US English. At the bottom you will see a button called "Add/Remove Languages". When you press it, another menu will pop up where you can check all languages that you require. In my case, I added German. When you apply the changes support for all selected languages will be installed.

If you're working on the command line. You can install language-pack packages through your favorite CLI package management tool, like apt or aptitude or whatever you like. There are a couple of language-packs for different languages. 

When I additionally picked support for German, the "Language Support" tool installed the following packages on my system:
  • language-pack-de
  • language-pack-gnome-de
  • language-pack-de-base
  • language-pack-gnome-de-base
  • language-support-writing-de
There are also language-pack-kde-de and language-pack-kde-de-base, respectively for KDE. But you probably won't need them unless you are on Kubuntu.

Mixing locales
As I mentioned in the previous paragraph. You can select your locale in System -> Administration -> Language Support. But there you can only select one locale for everything. For example, I like English GUI & CLI a lot, but I don't like their date & time formats so much - I'm just not used to that. Not to mention the American system of measurements :)

Luckily UNIX/Linux actually allows more fine grained control over the locale settings than the Ubuntu GUI wants to make you believe. The language you select on the GUI will act as the default, but you can also tweak it to your liking.

First create a new file called .custom_locale in your home directory. This will contain the custom locale settings for you alone:

# Custom locale settings:
# use Swissgerman date, time, currency, numbering etc.
export LANG=de_CH.utf8
# keep the CLI/GUI in US english
export LC_MESSAGES=en_US.utf8
# sort in alphabet the Swiss way
export LC_COLLATE=de_CH.utf8
# change currency formatting behavior of strfmon(3)
export LC_MONETARY=de_CH.utf8
# change time formatting behavior of strftime(3)
export LC_TIME=de_CH.utf8
# change number formatting behavior of printf(3) and scanf(3)
export LC_NUMERIC=de_CH.utf8

The LC_* variables are described in more detail in man locale(7). The most important settings are the first two. This file alone won't do much. You have to include it to your environment.

In ~/.profile, add the following lines:

# Include ustom locale settings if they exist
# (gdm parses .profile so these settings will 
# also be active within the Gnome environment).
if [ -f "$HOME/.custom_locale"]; then
   . "$HOME/.custom_locale"
fi
Additionally, you will also have to add the same snippet to your ~/.bashrc or ~/.zprofile (or rc file of whatever shell you use) because unlike gdm, the shells usually don't read .profile. Once you're done, reboot.

After the login, you can verify the locale settings e.g. from the shell with the command locale:

~$ locale
LANG=de_CH.utf8
LANGUAGE=en_US.UTF-8
LC_CTYPE="de_CH.utf8"
LC_NUMERIC=de_CH.utf8
LC_TIME=de_CH.utf8
LC_COLLATE=de_CH.utf8
LC_MONETARY=de_CH.utf8
LC_MESSAGES=en_US.utf8
LC_PAPER="de_CH.utf8"
LC_NAME="de_CH.utf8"
LC_ADDRESS="de_CH.utf8"
LC_TELEPHONE="de_CH.utf8"
LC_MEASUREMENT="de_CH.utf8"
LC_IDENTIFICATION="de_CH.utf8"
LC_ALL=

Systemwide Changes
The above tweak will only work for individual users. If you want to make a system wide change, you should change move the custom_locale file to /etc and then modify /etc/profile rather than ~/.profile. And likewise /etc/bash.bashrc & /etc/zsh/zprofile