Usually, I run aptitude -y install locales
then dpkg-reconfigure locales
to set up locale.
Now I want to put it into a shell script, how can I reliably do the following, automatically / non-interactively?
- Choose
en_US.UTF-8
and set it as system default - Disable all other locales
- (Optional) Verify if
/etc/default/locale
contains one-and-only entry ofLANG=en_US.UTF-8
as expected
Could not get @stone's answer to work. Instead, I use this method (for Dockerfiles):
Based on the fine work in @EirikW's answer. Specific to a
Dockerfile
:See locale-gen:
and
For me it was necessary to set the additional 3 ENV-Vars:
However Thanks to ErikWs for his answer: (https://serverfault.com/a/689947)
To reconfigure the timezone and locales non-interactively, from within a script, here is what works for me (under Debian):
For configuring the timezone, I first create '/etc/localtime' as a soft link to the appropriate zoneinfo file under the '/usr/share/zoneinfo' directory. Then, I run the dpkg-reconfigure command, and everything will be put in place. So, for instance, to set up timezone 'Europe/Brussels':
(Note that the AREA is a subdirectory under '/usr/share/info', and the ZONE is a file under the AREA subdirectory).
For configuring the locales, I first run a sed script that will create a new copy of the '/etc/locale.gen' file, based on the contents of the '/usr/share/i18n/SUPPORTED' file. Every line from the input file will be copied, but it will be turned into a comment unless it is an entry of a UTF-8 locale for a language that I wish to make available on my system (e.g., English, Dutch, French, and German):
Next, I set the default environment locale in the debconf database, e.g., to British English:
I subsequently remove the existing '/etc/default/locale' file (just to make sure that its old contents will not interfere with my new settings), and run the dpkg-reconfigure command to generate all of the locales that the sed script selected, and to create a new '/etc/default/locale' file with just an entry to set the 'LANG' variable to my selected default environment locale:
Then, depending on my requirements, I may want to run a few update-locale commands, to override, e.g., the variables that affect the formatting of values, and set them to a different locale (such as Irish English):
(I could have specified all of these parameters on a single invocation of the update-locale command, but apparently, the order in which the entries get written to the '/etc/default/locale' file is unpredictable if I do so. I prefer them to always be in the same order, which is why I generate them one by one.)
And finally, I may want to run the update-locale command one last time, to set up the LANGUAGE variable (i.e., the list of languages in which I want translatable text messages to get displayed):
Throw all this together into a bash script, and you can easily reconfigure your locale settings with a simple run of the script, which will not require any further user interaction.
In my experience, however, the new settings will not become entirely activated until after you reboot your system twice (at least on Debian Jessie Xfce). After the first reboot, the login manager will take the new settings alright, but after you login, your user session will continue to use the old settings; then, after the second reboot, your user session will take the new settings as well.