English switching configuration and garbled problem in Linux environment

Source: Internet
Author: User
Tags i18n ssh secure shell

Problem

These days to play the server, the default language is Chinese, and sometimes is very inconvenient, such as met a more complex problem, the information is Chinese, but Baidu there is no good information, only Google, but let me Google search noon.

So you want to restore your environment into English.

Basic Configuration

The essence is to modify the lang variable of the system

Lang is the abbreviation of language, a slightly English-based user can see that this variable is the default language of the system, that is, the system's menu, the program's toolbar language, input method default language and so on.

Configure your own Lang

If you want to modify the encoding and language information for the entire system, you can modify the system's configuration file to modify Lang, and if you do not want to affect other users to configure Lang directly in your own ~/.BASHRC

# use englishexport LANG=en_US.UTF-8

Modify the configuration file CentOS

Configuration file /etc/sysconfig/i18n
Chinese environment settings:

LANG="zh_CN.GB18030"    LANGUAGE="zh_CN.GB18030:zh_CN.GB2312:zh_CN"    SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"    SYSFONT="lat0-sun16"    

English environment settings:

LANG="en_US"    LANGUAGE="en_US"    SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"    SYSFONT="lat0-sun16"    SYSFONTACM="8859-15"    
Ubuntu

1. Install the Chinese Language pack

sudo apt-get install language-pack-zh

2. Configure language environment variables with VIM

vim /etc/environment

Add the following two lines below

LANG=”zh_CN.UTF-8″ LANGUAGE=”zh_CN:zh:en_US:en” 

If you want to use the English environment, change to these two lines is OK

LANG=”en_US.UTF-8″ LANGUAGE=”en_US:en” 
Advanced Configuration

In addition, for the domestic Linux users, a frequent problem is:
System often in need to display Chinese when it is garbled, and for some reason, the need for English interface system, but the system can not input and display the normal Chinese.

In addition, since most major Linux distributions are English-based, English-language interface systems and applications are slightly more aesthetically pleasing and more stable than Chinese, with fewer strange bugs.

As a result, many Linux users who have a slightly English base prefer to use the English interface system.

However, the contradiction is also emergent: under the English system, how can normal display and input Chinese? Is there a solution for both worlds? Therefore, the author began to explore how to solve this problem.

My perfect state is: the system and the application are all in English (System menu, application toolbar, default input method, etc.), and I need to read and write Chinese documents, I can correctly display the Chinese language and the Chinese input method. After trying, Setup succeeded, now take Linux as an example to explain some of the relevant knowledge and setup process.

This article mainly by modifying the system configuration process to show the modification of the Linux system configuration of the general idea and process, if you are not too patient to read, please skip the article section 1-4, directly see the fifth section of the Quick Setup section.

Locale directive

Run locale command to get details of current system encoding settings
The organs of the locale

1. Language symbols and their classification (LC_CTYPE)
2, Digital (lc_numeric)
3. Comparison and sequencing habits (lc_collate)
4. Time display format (lc_time)
5. Monetary unit (lc_monetary)
6, information is mainly information, error information, status information, title, tags, buttons and menus, etc. (lc_messages)
7. Name writing method (Lc_name)
8. Address writing method (lc_address)
9, telephone number writing method (Lc_telephone)
10. Expression of weights and measures (lc_measurement)
11. Default paper Size (lc_paper)
12. An overview of the locale itself containing information (lc_identification).

Introduction to related variables

We know that most Linux systems do not matter in Chinese and English, in the case of Linux, when the system is distributed all over the world, whether the system is Chinese or English depends entirely on the language pack you choose.

People in different countries choose their own country when installing the language pack, the application language is not written dead, it according to the system's settings to invoke the relevant language, so an application written out without modification, users in different countries around the world can use it in a native language interface, which ratione materiae so-called Internationalization (internationalization), referred to as i18n. This is also the future of software development trend.

So, if I install different language packs and different fonts in the system, how does the system judge the language interface I want and invoke the relevant font? What are the files and variables in the system controlling these?

In the Redhat series of Linux systems, the documentation system uses the default language files /etc/sysconfig/i18n ,
If the Chinese system is installed by default, the contents of i18n are as follows:

LANG="zh_CN.UTF-8" SYSFONT="latarcyrheb-sun16" SUPPORTED="zh_CN.UTF-8:zh_CN:zh"

Where lang variable is the abbreviation of language, a slightly English-based user can see that the variable is the default language of the system, that is, the system's menu, the program's toolbar language, input method default language and so on.

Sysfont is the abbreviation for system font, which determines which font is used by default.

The supported variable determines the language that the system supports, i.e. the language that the system can display. It is necessary to note that since computers originate in English-speaking countries, English is always supported by default, regardless of the font you use, and regardless of the typeface, the text is included.

These variables in the lang variable is used in the character mode and the graphical interface, after you log in the system is read and effective, I believe many people in the character interface when the Linux command is often encountered in the display of error message is garbled, must Install Zhcon or CCE and other characters mode of Chinese software in order to correctly display the error message in Chinese. If I don't want him to display Chinese garbled characters, I also do not want to see a very simple error message and deliberately to the zhcon what should I do? A simple zero-resolution approach is to set the lang variable:

LANG="en_US.UTF-8"

That is, the language of the system is temporarily set to English, or a little more simple, you can directly:

LANG=""

That is, the lang variable is empty, because English is supported in any case, the lang variable is emptied, the system will default in English. After this setting, the error message output in character mode is all in English. But this setting is temporary, just temporarily changing Lang's bash variable. It will not work when you exit the login again or switch to a different character terminal. By now, the reader should have thought of, just set the lang variable in the i18n file to "en_US" in English. UTF-8 ", you can solve this problem permanently.

LANG="zh_CN.UTF-8"LANG="en_US.UTF-8"SYSFONT="latarcyrheb-sun16"SUPPORTED="zh_CN.UTF-8:zh_CN:zh"

Please do not simply empty the lang variable, because this variable is not only used in the character mode, in the graphical interface is also used, simple emptying in the character mode does not have a problem, but in the graphical interface, it will cause the Chinese can not display properly, in the past re D ha t In the version of the series, there is also a variable called language in the i18n file that specifically controls the language settings under the graphical interface, and now the FC series has integrated these two variables into one variable.
After modifying this variable and restarting the graphical interface, you can see that the interface is fully English once. But press CTRL + space can not bring up the Chinese input method, input menu can not add Chinese input method. We have simply modified the lang variable to change the system language settings, but this step can also be modified with the tools under the graphical interface without having to modify the configuration file yourself.

About operating levels

This question seems to have nothing to do with the topic of this article, but there are a growing number of new Linux novice encounters with Linux GUI problems, and in the Chinese input method is also involved in the setup process, so I would like to mention.
Now that the Linux installation is complete, the default is to run at the 5th system RunLevel. In system V-style UNIX systems, systems are divided into different runlevel, which differs from the BSD branch of UNIX and is commonly used for 0~6 seven levels:

is
Tables Cool
0 Shutdown
1 Single User
2 Multi-user with no network
3 Multi-user with network
4 Reserved, the user can define self-sufficiency
5 Graphical interface for multi-user
6 Re-starting the system

Since the Linux system is now installed in the 5th level, that is, after the system started directly into the graphical interface, rather than in the character mode after logging in with StartX or xinit to start the graphical interface.

This looks very convenient. But what's the downside?

Once you change some settings, show the problem, the system constantly in the graphics and word putting faction irresolute, the novice does not know how to deal with, very troublesome, and for the study of Linux people, this is not conducive to understanding and learning some of the Linux underlying things. Old Linux users have long been aware that the past Linux, such as redhat6.0, is the default to run the connection is 3, even if the later RedHat9.0 can also be installed in the candidate default character logon or graphics login.

But now most versions do not care about 3,721 directly to help users choose a direct graphical interface login. While Linux is really getting easier for most novices, it's a lot of fun, and it's not for beginners to experience.

Perhaps you do not believe that direct graphics login to the system does have a lot of problems, so I strongly recommend that the system after the installation of the default operating level set at level 3rd, after the character terminal login, and then manually enter the STARTX command to start the graphical interface. You can modify it in the following ways:
Modify the/etc/inittab file with a text editor to

id:5:initdefault:

This line, modified into

id:3:initdefault:

Restart after saving, the system will start to the character interface by default.

The difference between the different run levels is that the system starts with a different service, such as running Level 3 does not start the X graphical interface service by default, and RunLevel 5 is started by default.

There is no difference in nature, and it does not matter whether the function is strong or weak between different levels. Users are fully self-sufficient to define default services at different levels. At any run level, the user can use the init command to switch to another runlevel.

Bring up Chinese Input method

The reason why I want to spend so much ink on it is that the system is running at the level of the system because it starts from the bottom up.

Change the default RunLevel to Level 3 first and, of course, temporarily switch to level 3rd with the init 3 command if you really don't want to modify it.

This allows you to start the graphical interface with STARTX and then use Ctrl+alt+backspace to exit the graphical interface. Please note that I am talking about "exit" the graphical interface instead of pressing CTRL+ALT+F2 to cut to a character terminal.

Well, everything starts with startx. When you need to set something up in a Linux system, or configure a service, the key is to know how it all started. It is necessary to know the reason why.

If you have time to read through the scripts that run when the system is started in the/ETC/RC.D directory, you will know exactly what the various configuration files are used for, how to modify them, what effects to modify, and so on.

Play the system can also be arbitrary how to change how to change. This is what I have been stressing, knowing that it must know why. Be sure to drill down into the system, read the script, learn to use commands and manually modify the system configuration file. This will be a thorough understanding of the system, all day with the graphical interface tools can not help you to have a thorough understanding of the system, different Linux systems provide a different graphical interface configuration, but the command and configuration files are the same, the more the bottom of things more universal. Therefore, you should first learn to manually configure and modify the system configuration files, and so familiar with the later, then use the graphical interface tools to modify, in order to reduce the workload.

The above mentioned my idea of solving the problem. I started with this idea:
The Chinese input method is used under the graphical interface and is a program that runs under the graphical interface.

And everything in the graphical interface is run by the STARTX program. This is the root of the problem.
Find the location of the STARTX:

which startx /usr/X11R6/bin/startx

See if StartX is a script or a binary file:

fileshelltext executable

Discover that StartX is a shell script, so I open it to analyze and read to see if I can find some clues about the input method starting process and related variables:

vi /usr/X11R6/bin/startx

I found information about other scripts and configuration files that the script calls during the run:

userclientrc=$HOME/.xinitrc userserverrc=$HOME/.xserverrc sysclientrc=/etc/X11/xinit/xinitrcsysserverrc=/etc/X11/xinit/xserverrc

and know that the purpose of STARTX is to look for the desktop System x Server system available in the system, as well as user-defined parameters, and finally call Xinit to initialize the X graphical interface. I did not find the direct and starting input method related code in the StartX script, so I can be sure that the input method related code in the script called StartX.

So I came to the/etc/x11/xinit/directory, read and analyze the script under this directory, some of these scripts are directly called by StartX, some are called by the STARTX Call of the script, there is a multilevel nested relationship, It's not clear that there is no patience. I finally found the code related to IME in the xinput.sh script in the/ETC/X11/XINIT/XINITRC.D directory:

lang_region=$ (echo   $tmplang  | Sed -e   ' s/"...  *//' ) lang_region= "ZH_CN"   #这一行是修改后加上去的 for f in $HOME/.xinput.d/${lang_region} "$HOME/.xinput.d/default "/etc/x11/xinit/xinput.d/${lang_region}" /etc/x11/xinit/xinput.d/default; do  [-R  $f ] && source   $f  && break  done   

By analyzing the script, I know that when the graphical interface is started, the script is based on the Lang variable to determine whether the input method is enabled, and which language input method is enabled. The problem is: before we change the lang variable to English, the system gets the lang variable is Chinese, so it knows the need to start the graphical interface to enable Chinese input method, but the lang variable into English, the system according to Lang variable know the system is English, it will not start the Chinese input method , and the related variables are no longer set and exported, which makes the Chinese input method unavailable. Therefore, as long as in this script, "cheat" the system, let the input method script "think" system is Chinese, it does not run the Chinese input method, and export the relevant variables? So, by analyzing the script, I'm in xinput.sh:

lang_region=$(echo$tmplang-e‘s/"..*//‘) 后面又添加了 lang_region="zh_CN"

Directly put

lang_region=$(echo$tmplang-e‘s/"..*//‘)

Modified into

lang_region="zh_CN"

can also

Add one more row is for the sake of later changed to convenient, directly delete the added line can be.

Of course, put the for loop in the /etc/X11/xinit/xinput.d/${lang_region}
/etc/X11/xinit/xinput.d/zh _CNOr you can.

Of course there are other changes, if you have to understand the syntax of the shell script, you can understand the meaning of the script. After this modification, even if the system is in English, the xinput.sh script will read the/etc/x11/xinit/xinput.d/zh _cn file and export the contents, set the Xmodifers and other input method variables, and run the Iiimx IME program.
So why not run the Iiimx IME program directly after the graphical interface is started? Experiment to know, this is not at all. Because the IME program is the software that needs to run with the application being entered, many variables need to be exported during the run. Running the IIIMX directly only runs the main program, without the relevant variables, no way to work with the application to complete the input.

After you have finished modifying your work, save the script file. Enter the STARTX command to start the graphical interface, you can use the full English system interface and Chinese input method. However, it should be noted that: because the system is in English, the default input method is also in English, through the GNOME or KDE menu starting the application of the first time you input Chinese can not press CTRL + SPACE to switch to Chinese, you need to use the mouse on the taskbar click the Input method icon switch, After the first switch, you can use CTRL + Space shortcut to switch between Chinese and English input methods.

Some follow-up questions

Some software, such as Open Office, through the GNOME or KDE menu start, even if switching to Chinese input method also lose Chinese, this is because the entire desktop system environment is English, the software "inherit" the English environment related variables, the software is "Rensili", is not allowed to input Chinese, You can open a GNOME terminal and temporarily set the lang variable to ZH_CN. UTF-8:

LANG="zh_CN.UTF-8"

Then, in this Gnome terminal, open office with a command:

oowriter &

In this way, open office "inherits" the Lang variable of the Gnome terminal, and after the start, the toolbars and menus are all Chinese and can be entered in Chinese. By the way, any software can use this method, as needed, open the Chinese interface software and English interface software. To run the software in English, simply open it from the GNOME or KDE menu and use the Chinese interface to run the software, and fix the lang variable in the terminal by running it from the terminal where the lang variable was modified. Of course, if you also have fonts installed in other languages, you can also run the program in a different language interface. such as Japanese:

LANG="ja_JP.UTF-8"gedit &

The Gedit editor I opened with the two commands above is the full Japanese interface, but can be entered in Chinese and English, and displayed in Japanese. thereby achieving, a system, multi-lingual and literal coexistence purposes.
Of course, the premise is to install Japanese fonts and Japanese locale, otherwise all the text will be displayed as a series of question marks. In short, first understand the principle, then want to how to play on how to play, arbitrary, completely unrestricted, fully enjoy the fun with Linux.

Quick Setup Steps
    1. Modify the/etc/sysconf/i18n file to
LANG="zh_CN.UTF-8"

Modified to:

LANG="en_US.UTF-8"
    1. Modify the/etc/x11/xinit/xinitrc.d/xinput.sh file to include one line:
lang_region=$(echo$tmplang-e‘s/"..*//‘)

Modified to:

lang_region=”zh_CN”
    1. Restart the graphical interface, you can use the English interface and display Chinese correctly and input Chinese.

The following is a personal supplement:
SSH login to Linux, SSH terminal sometimes garbled, even if you set Lang=en_us. UTF-8, too. I've tried securecrt,openssh,ssh Secure Shell client, and that's the problem. Sometimes it's fine to change the client's settings, but sometimes you can only run one command at a time after changing the client's settings, and then it becomes garbled.
An accidental opportunity to see on the internet that someone said to change the value of the lang variable to "C" on the line. But really evil, just to a lang=c, what problems have been solved, do not set the client, it is inexplicable! But I still do not know what the "C" means, it is so powerful.

English switching configuration and garbled problem in Linux environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.