[This answer is target on linux platform only]
The first thing you should know is most of the locale config file located path can be get from localedef --help
:
$ localedef --help | tail -n 5
System's directory for character maps : /usr/share/i18n/charmaps
repertoire maps: /usr/share/i18n/repertoiremaps
locale path : /usr/lib/locale:/usr/share/i18n
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>
See the last /usr/share/i18n
? This is where your xx_XX.UTF-8 config file located:
$ ls /usr/share/i18n/locales/zh_*
/usr/share/i18n/locales/zh_CN /usr/share/i18n/locales/zh_HK /usr/share/i18n/locales/zh_SG /usr/share/i18n/locales/zh_TW
Now what ? We need to compile them into archive binary. One of the way, e.g. assume I have /usr/share/i18n/locales/en_LOVE
, I can add it into compile list, i.e. /etc/locale-gen
file:
$ tail -1 /etc/locale.gen
en_LOVE.UTF-8 UTF-8
And compile it to binary with sudo locale-gen
:
$ sudo locale-gen
Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
...
en_LOVE.UTF-8... done
Generation complete.
And now update the system default locale with desired LANG
, LC_ALL
...etc with this update-locale
:
sudo update-locale LANG=en_LOVE.UTF-8
update-locale
actually also means to update this /etc/default/locale
file which will source by system on login to setup environment variables:
$ head /etc/default/locale
# File generated by update-locale
LANG=en_LOVE.UTF-8
LC_NUMERIC="en_US.UTF-8"
...
But we may not want to reboot to take effect, so we can just source it to environment variable in current shell session:
$ . /etc/default/locale
How about sudo dpkg-reconfigure locales
? If you play around it you will know this command basically act as GUI to simplify the above steps, i.e. Edit /etc/locale.gen
-> sudo locale-gen
-> sudo update-locale LANG=en_LOVE.UTF-8
For python, as long as /etc/locale.gen
contains that locale candidate and locale.gen
get compiled, setlocale(category, locale)
should work without throws locale.Error: unsupoorted locale setting
. You can check the correct string en_US.UTF-8
/en_US/....etc
to be set in setlocale()
, by observing /etc/locale.gen
file, and then uncomment and compile it as desired. zh_CN GB2312
without dot in that file means the correct string is zh_CN
and zh_CN.GB2312
.