What are System Locales and How to manage them in Linux?

System locale is a system configuration that controls the language, character encoding, and regional settings used by the system and applications. It determines how text, dates, numbers, and system messages are displayed.

For example:

  • en_US.UTF-8 → English (United States) with UTF-8 encoding
  • fr_FR.UTF-8 → French (France) with UTF-8 encoding

System libraries and locale-aware applications rely on these environmental variables to adapt to user preferences. Here’s a guide on managing locales in Linux, including viewing, generating, changing, and troubleshooting locale settings.


Viewing System Locales:

1. Display Current Locale Settings

Use the `locale` command:

$> locale
2. Check Global Locale Settings

Use the `localectl` command:

$> localectl status
3. View Specific Locale Variables

For example, view details of `LC_TIME`:

locale -k LC_TIME
4. List All Available Locales
$> locale -a

Generating System Locales

1. Install the Locale Package (if necessary):
$> sudo apt install locales
2. Edit Locale Configuration File:

Open and edit `/etc/locale.gen` to enable desired locales, here we are using nano text editor:

$> sudo nano /etc/locale.gen

you can remove the # symbol from the lines of the locales you want to enable.

3. Generate the specified locales:
$> sudo locale-gen
4. Update Current Locale Settings:

Set your desired locale (e.g., `en_US.UTF-8`) globally:

$> sudo update-locale LANG=en_US.UTF-8
5. Verify Generated Locales:

Check the list of available locales:

$> locale -a

Changing System Locales:

1. Set Locale Using `update-locale`

Example: Set `LANG` to `en_IN.UTF-8`:

$> sudo update-locale LANG=en_IN.UTF-8
2. Set Locale Using `localectl`

Example: Set `LANG` to `en_IN.UTF-8`

$>sudo localectl set-locale LANG=en_IN.UTF-8
3. Manually Edit Global Locale Settings

Edit `/etc/default/locale` for manual configuration:

$> sudo nano /etc/default/locale

Setting System Locales for a Single User:

1. Edit User Profile

Add the following lines to the end of `~/.bash_profile`:

LANG="en_IN.UTF-8"
export LANG
2. Make Changes Persistent Across Sessions

Add locale settings to `~/.bashrc`:

$> echo 'export LANG=en_IN.UTF-8' >> ~/.bashrc

Apply Changes by Reloading the profile:

$>source ~/.bashrc

Overriding System Locales for a Session:

1. Temporarily Set Locale Variables
$> export LANG=en_US.UTF-8
2. Verify Changes
$> locale

These settings persist only for the session.


Troubleshooting System Locales Issues:

1. Check Installed Locales:

Verify available locales:

$> locale -a
2. Regenerate Locales:

Ensure changes to `/etc/locale.gen` are applied:

$> sudo locale-gen
3. Verify Environment Variables:

Check for proper formatting and configuration in `~/.bashrc` or `~/.bash_profile`.

4. File Permissions:

Ensure sufficient permissions for system-wide locale changes. Use `sudo` where necessary.

5. Restart Services:

Some applications require a restart to apply new locale settings.

6. Consult Documentation:

Access manual pages for more details:

$> man locale

$>man update-local

$> man localectl