Blog / Linux/ Configuring Chinese Language Support on Ubuntu: Fixing Filename and Terminal Garbled Text

Configuring Chinese Language Support on Ubuntu: Fixing Filename and Terminal Garbled Text

Ubuntu 系统中文支持配置指南:解决文件名与终端乱码问题

Problem Overview

On Ubuntu systems, incorrect locale configuration can cause garbled characters in terminal output, filenames, or application interfaces. This guide explains how to add full Chinese language support to Ubuntu.

Check Installed Language Packs

First, check which locales are already installed on your system.

locale -a

If the output includes zh_CN.utf8 or similar entries, Chinese language packs are partially installed.

Install Chinese Language Packs

Method 1: Using locale-gen (Recommended)

This is the traditional method for configuring system-wide locales.

  1. Edit the locale generation configuration file:

    sudo vi /etc/locale.gen

    Or use your preferred text editor (e.g., nano).

  2. Find and uncomment (remove the # from) the following lines (or similar):

    # zh_CN.UTF-8 UTF-8

    Note: Modern Ubuntu systems should use zh_CN.UTF-8. Other encodings like GBK are for legacy compatibility and are not recommended.

  3. Generate the enabled locales:

    sudo locale-gen

Method 2: Interactive Configuration with dpkg-reconfigure locales

This tool provides an interactive menu for selecting and generating locales.

sudo dpkg-reconfigure locales

After running the command:

  1. Use the spacebar to select zh_CN.UTF-8 from the list.
  2. Press Tab to move to <Ok> and press Enter.
  3. In the next screen, select zh_CN.UTF-8 as the default system locale and confirm.

Set the System Default Locale

After installing the language pack, configure the system's default locale. Edit the file:

sudo vi /etc/default/locale

Ensure its contents are similar to the following (recommended settings):

LANG="zh_CN.UTF-8"
LANGUAGE="zh_CN:zh"

Note: Setting LC_ALL overrides all other locale variables. For most users, setting LANG and LANGUAGE is sufficient.

Configure the Shell Environment for the Current User

System settings may not apply immediately to your current session. Add the environment variables to your shell configuration file (e.g., ~/.bashrc or ~/.profile).

Add these lines at the end of the file:

export LANG=zh_CN.UTF-8
export LANGUAGE=zh_CN:zh

Then apply the changes:

source ~/.bashrc

Verification and Activation

  1. Verify Configuration: Open a new terminal and run:

    locale

    All variables should show zh_CN.UTF-8 or similar values.

  2. Install Chinese Input Method (Optional): Desktop users may need an input method framework, e.g.:

    sudo apt install fcitx5 fcitx5-chinese-addons
  3. Reboot: For all system services and the graphical interface to load the new settings, a reboot is recommended.

    sudo reboot

Troubleshooting

  • Command Not Found: If locale-gen or dpkg-reconfigure is missing, install the locales package: sudo apt install locales.
  • GUI Not Switched: For Ubuntu Desktop, use the "Region & Language" tool in System Settings for automatic dependency and font handling.
  • Garbled Characters Persist: Ensure your terminal emulator's encoding is set to UTF-8 and that files are saved in UTF-8 encoding.

Best Practice: On servers, install only the zh_CN.UTF-8 locale to avoid encoding complexity. On desktops, install the full Chinese support package: sudo apt install language-pack-zh-hans.

Post a Comment

Your email will not be published. Required fields are marked with *.