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:
Here's a trace of running this command:
ubuntu@ip-1:~$ locale -a
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.
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.
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:
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
ubuntu@ip-1:~$ locale -a
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!