Oct 23, 2012

f Comment

How Do Unix Locales Work? Installing a Missing Locale in SECONDS!

Amazon Have you ever wondered how locales work on Unix? Whenever you manipulate locales in your favorite programming language such as PHP and Java to provide localization do you ever wonder how locales are supported? This article will explain how locales work and how to install new locales if your Unix system does not support the locales you need!

First of all the following shows my Unix box's version and shell's version:

ubuntu@domU-1:~$ cat /etc/issue
Ubuntu 10.04.4 LTS
ubuntu@domU-1:~$ bash -version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

Here's the procedure you should follow to install a locale.

1. See if the locale is already available.

2. If the locale is not available, find all supported locales. Otherwise it's ready to be used.

3. If the missing locale is included in the list of supported locales, install it and use it. Otherwise your system does NOT support this locale.

We'll explain how to accomplish each task below. Let's suppose we are to install the locale 'zh_TW.UTF-8'!

Show all Available Locales
By definition an "available locale" is a locale that your program may use. If your program is trying to use a locale that is not on the list of available locales then your program will not display characters correctly. The following is the command to show available locales:

locale -a
Here's a trace of running this command:

ubuntu@ip-1:~$ locale -a
C
en_US
en_US.utf8
POSIX

As you can see 'zh_TW.UTF-8' is NOT on the list. Let's see if 'zh_TW.UTF-8' is supported by our system next!

Show all Supported Locales
By definition a "supported locale" is a locale that your Unix platform supports. Your available locales are a subset of your system's supported locales. Only supported locales may be installed and be used by your program.

Here's the command to display a list of all supported locales.

cat /usr/share/i18n/SUPPORTED
Here's the sample output of running this command:

# This file names the currently supported and somewhat tested locales.
# If you have any additions please file a glibc bug report.
aa_DJ.UTF-8 UTF-8
aa_DJ ISO-8859-1
aa_ER UTF-8
aa_ER@saaho UTF-8
aa_ET UTF-8
af_ZA.UTF-8 UTF-8
af_ZA ISO-8859-1
am_ET UTF-8
an_ES.UTF-8 UTF-8
....
zh_HK.UTF-8 UTF-8
zh_HK BIG5-HKSCS
zh_SG.UTF-8 UTF-8
zh_SG.GBK GBK
zh_SG GB2312
zh_TW.EUC-TW EUC-TW
zh_TW.UTF-8 UTF-8
zh_TW BIG5
zu_ZA.UTF-8 UTF-8
zu_ZA ISO-8859-1

As you can see 'zh_TW.UTF-8' is indeed on this list! Let's install it next!

Installing a Locale
Installing a locale on your Unix system means compiling a locale definition file for the locale you are trying to install. Here's the command of installing a locale:

locale-gen zh_TW.UTF-8
Here's a trace of installing locale 'zh_TW.UTF-8' on my Unix box:

ubuntu@ip-1:~$ sudo locale-gen zh_TW.UTF-8
Generating locales...
zh_TW.UTF-8... done
Generation complete.

ubuntu@ip-1:~$ locale -a
C
en_US
en_US.utf8
POSIX
zh_TW.utf8

As you can see 'zh_TW.utf8' is on the list of available locales now and is ready to be used by your program! This article, Setting a Locale in Joomla, presents an example of setting a locale in PHP.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael