25

In the console to input dpkg-reconfigure locales,a gui jump out ,i select en_HK.UTF-8 as my target ,click enter.

enter image description here

dpkg-reconfigure locales
Generating locales (this might take a while)...
en_HK.UTF-8... done

How to automate the whole process into one command ?

1

7 Answers 7

20

What worked charm for me was a combination between the @DevRobot's and the @Gunnar Hjalmarsson's answers (run as root). EDIT: Added some improvements to avoid annoying errors:

update-locale "LANG=en_HK.UTF-8"
locale-gen --purge "en_HK.UTF-8"
dpkg-reconfigure --frontend noninteractive locales

Another possibility (that works even better) is through the debconf-utils utilities and debconf-set-selections (run as root):

echo "locales locales/default_environment_locale select en_HK.UTF-8" | debconf-set-selections
echo "locales locales/locales_to_be_generated multiselect en_HK.UTF-8 UTF-8" | debconf-set-selections
rm "/etc/locale.gen"
dpkg-reconfigure --frontend noninteractive locales

Hope this might help.

0
6

One command:

sudo update-locale LANG=en_HK.UTF-8

Easy day :)

1
  • 5
    update-locale is for updating /etc/default/locale. In other words it's something else... Oct 9, 2015 at 16:00
6

I would suggest:

echo "en_HK.UTF-8" | sudo tee -a /etc/locale.gen
sudo locale-gen

or if it absolutely has to be "one command":

echo "en_HK.UTF-8" | sudo tee -a /etc/locale.gen; sudo locale-gen

For an explanation see my answer at https://askubuntu.com/a/1246655/912933

1
  • Worked for me in docker. Thank you!
    – RandomGuy
    Jan 17 at 17:03
2

If you want to generate a single locale, this should theoretically suffice:

echo "locales locales/locales_to_be_generated multiselect en_HK.UTF-8 UTF-8" | debconf-set-selections
dpkg-reconfigure --frontend noninteractive locales

In practice though, dpkg-reconfigure will fail to generate the locale under Ubuntu 20.04. To workaround, you need an additional command:

echo "locales locales/locales_to_be_generated multiselect en_HK.UTF-8 UTF-8" | debconf-set-selections
rm /etc/locale.gen
dpkg-reconfigure --frontend noninteractive locales
1

I would probably run:

sudo locale-gen --purge

That regenerates all the locales for your installed languages.

1

This gist had the solution for me:

sudo sed -i '/^#.* en_US.* /s/^#//' /etc/locale.gen
sudo locale-gen
1
  • First I reset all, and the activate just 'my' country: LOCALE='en-us'; sed -i -re "/^[^#]/s/.*/# &/" /etc/locale.gen; sed -i -re "s/^# (${LOCALE})(.*)/\1\2/Ig" /etc/locale.gen;
    – fcm
    Oct 25, 2021 at 21:13
0
sudo EDITOR='sed -Ei "
    s|locales/locales_to_be_generated=.+|locales/locales_to_be_generated=\"en_GB.UTF-8 UTF-8\"|; 
    s|locales/default_environment_locale=.+|locales/default_environment_locale=\"en_GB.UTF-8\"|
    "' dpkg-reconfigure -f editor locales

Related answer: How do you set a locale non-interactively on Debian/Ubuntu?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .