Understanding Linux Locales
Before diving into the configuration, it's important to understand what locales are and why they matter. A locale is a set of parameters that defines the user's language, country, and character encoding preferences. It affects:
- Language: The language used by applications for messages and interface text
- Date and time formats: How dates and times are displayed
- Number formatting: Decimal separators, thousands separators, etc.
- Currency: Currency symbols and formatting
- Character encoding: How text is stored and displayed (typically UTF-8)
Common Locale-Related Issues
You might encounter locale warnings when:
- Installing or updating packages
- Running applications that require specific locale support
- SSH-ing into remote servers
- Using applications with multilingual features
These warnings typically look like:
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings...Checking Your Current Locale Configuration
Before making any changes, let's check the current locale configuration:
locale
This command displays all locale-related environment variables. You might see output like:
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_GB
LC_ALL=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
...
To see which locales are currently available on your system:
locale -aStep-by-Step Configuration Guide
Step 1: Generate and Install Required Locales
First, we need to generate the locale files for the languages we want to support. In this example, we'll configure support for Chinese (China), British English, and German:
sudo locale-gen zh_CN.UTF-8 en_GB.UTF-8 de_DE.UTF-8
This command generates the necessary locale data files. You should see output indicating that each locale is being generated.
Next, reconfigure the locale packages to ensure everything is properly registered:
sudo dpkg-reconfigure locales
This will open an interactive dialog where you can:
- Use Space to select/deselect locales
- Select:
zh_CN.UTF-8,en_GB.UTF-8, andde_DE.UTF-8 - Press Tab to move to "OK" and press Enter
- Choose your default locale (typically
zh_CN.UTF-8if Chinese is your primary language)
Step 2: Configure System-Wide Locale Settings
Edit the system-wide locale configuration file:
sudo nano /etc/default/locale
Replace its contents with:
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_GB:de_DE
LC_ALL=zh_CN.UTF-8
Understanding these variables:
- LANG: Sets the default locale for all categories
- LANGUAGE: Defines the fallback language priority list (if Chinese translation is unavailable, try British English, then German)
- LC_ALL: Overrides all other locale settings (use with caution)
Step 3: Update System Environment
Apply the changes system-wide using the update-locale command:
sudo update-locale LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:en_GB:de_DE LC_ALL=zh_CN.UTF-8
This ensures that the locale settings are properly registered in the system configuration.
Step 4: Apply Changes
To make the changes effective, you have two options:
Option A: Source the configuration file (temporary, current session only)
source /etc/default/localeOption B: Log out and log back in (recommended)
Logging out and back in ensures all processes start with the new locale settings.
Step 5: Verify the Configuration
Check that your new locale configuration is active:
locale
You should now see output that includes support for Chinese, British English, and German locales:
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_GB:de_DE
LC_ALL=zh_CN.UTF-8
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
...
Verify that all required locales are available:
locale -a | grep -E "zh_CN|en_GB|de_DE"
You should see:
de_DE.utf8
en_GB.utf8
zh_CN.utf8Understanding the LANGUAGE Variable
The LANGUAGE variable deserves special attention. It defines a priority list for language selection:
LANGUAGE=zh_CN:en_GB:de_DE
This means:
- Applications will first try to display content in Chinese (Simplified, China)
- If Chinese translations are not available, they'll fall back to British English
- If British English is unavailable, they'll try German
- If all else fails, they'll use the default C/POSIX locale
This fallback mechanism is particularly useful for:
- Development environments with mixed-language documentation
- Applications with incomplete translations
- Reducing locale-related warnings
Best Practices and Tips
Use UTF-8 Encoding
Always use UTF-8 encoding for modern systems. It provides the widest character support and is the standard for internationalization.
Be Cautious with LC_ALL
Setting LC_ALL overrides all individual LC_* variables. While it ensures consistency, it can also prevent fine-grained control. Consider setting only LANG and LANGUAGE for most use cases.
User-Specific Locale Settings
If you need different locale settings for a specific user, add the locale variables to their ~/.bashrc or ~/.profile:
export LANG=en_GB.UTF-8
export LANGUAGE=en_GB:en_USTesting Applications
After changing locales, test your critical applications to ensure they behave as expected:
# Test date formatting
date
# Test application with specific locale
LC_ALL=en_GB.UTF-8 your-applicationServer Environments
For servers, especially those accessed via SSH, ensure that:
- Required locales are installed on the server
- SSH client doesn't force its locale settings (check
/etc/ssh/sshd_configforAcceptEnv LANG LC_*)
Troubleshooting Common Issues
Issue: "locale: Cannot set LC_* to default locale"
Solution: The locale you're trying to use isn't generated. Run:
sudo locale-gen <locale-name>
sudo dpkg-reconfigure localesIssue: Locale warnings persist after configuration
Solution:
- Ensure you've logged out and back in
- Check that locale files exist:
locale -a - Verify
/etc/default/localeis correct - Restart services that show warnings
Issue: Applications still display wrong language
Solution:
- Check application-specific language settings
- Verify that translation packages are installed (e.g.,
language-pack-zh-hans) - Clear application cache if applicable