How to automate 'dpkg-reconfigure locales' with one command?

Solution 1:

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.

Solution 2:

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