Fix Linux garbled characters and start the command line mode by default.

Source: Internet
Author: User
Tags i18n

For Linux users in China, it is a problem that the system often displays garbled characters when it needs to display Chinese characters. For some reason, when it needs a system with an English interface, however, the system cannot normally input and display Chinese characters. in addition, because most major Linux distributions are dominated by English, the system and applications of the English interface are slightly better than those of Chinese in terms of appearance and Stability of the interface, there are also fewer strange bugs. Therefore, many Linux users who have a basic English language prefer to use an English interface system. However, the contradiction emerges again: in the English system, how can we display and enter Chinese characters properly? Is there a perfect solution for both worlds? Therefore, I began to explore how to solve this problem. My perfect state is that the system and applications are all in English (System menu, application toolbar, default input method, etc.), and when I need to read and write Chinese documents, display Chinese characters correctly and call up the Chinese input method. After a successful setup, we now use FC4 Linux as an example to explain some related knowledge and setup processes.

This article mainly describes the general idea and process of modifying the Linux system configuration by modifying the system configuration. If you are not patient, skip Section 1-4 of the article and go directly to Section 5 "Quick Setting.

1. Introduction to related variables

We know that most Linux systems do not support both Chinese and English versions. Taking FC4 Linux as an example, the system is released all over the world. whether the system is Chinese or English depends on the Language Pack you choose. When installing and using different countries, people in different countries select their own language packs. The language in the application is not completely written. It calls the relevant language based on the system settings, once an application is written, it can be used by users in different countries around the world on the mother tongue interface. This is called internationalization (International), or i18n for short. This is also the future trend of software development.

If I have installed different language packs and different fonts on the system, how does the system determine the language interface I want and call the relevant fonts? What files and variables are under control in the system?

In the RedHat and fcseries Linux systems, the default language file used by the system is/etc/sysconfig/i18n. if the system is installed in Chinese by default, the i18n content is as follows:

Code:

Lang = "zh_CN.UTF-8"
Sysfont = "latarcyrheb-sun16"
Supported = "zh_CN.UTF-8: zh_cn: ZH"
 

The Lang variable is short for language. A user with a slight English base can see that this variable determines the default language of the system, that is, the system menu, the tool bar language of the program, and the default language of the input method. Sysfont is short for system font and determines which font is used by default. The supported variable determines the language supported by the system, that is, the language that the system can display. It should be noted that because the computer originated from the English-speaking country, no matter what you set these variables, English is always supported by default, and no matter what font you use, the English font is always included.

Among these variables, Lang variables are used in the character mode and graphic interface. They are read and take effect after you log on to the system, I believe that many users often encounter garbled error messages when entering Linux commands on the Character interface, you must install Chinese software in character mode such as zhcon or CCE to display Chinese error messages normally. What should I do if I don't want him to show Chinese garbled characters or use zhcon specially to read a very simple error message? A simple zero-time solution is to set the Lang variable:

Code:

[Root @ gucuiwen ~] # Lang = "en_US.UTF-8"
 

That is, to set the system language to English temporarily, or to be simpler, you can directly do this:

Code:

[Root @ gucuiwen ~] # Lang = ""
 

That is, the Lang variable is cleared. because English is supported in any situation, after the Lang variable is cleared, the system uses English by default. After this setting, the error messages output in character mode are all in English. However, this setting is temporary, but it only changes the bash variable Lang temporarily. It is invalid when you log out and log on again or switch to another character terminal. Now, the reader should think of, as long as the i18n file in the Lang variable set to English "en_US.UTF-8", you can solve this problem permanently. The modified file is as follows:
Code:

# 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 used not only in character mode, but also in the graphic interface. It is no problem to simply clear it in character mode, however, in the graphic interface, Chinese characters cannot be displayed normally. In the past, the i18n file of the Re d ha T series also contains a language variable, the language settings under the graphic interface are specially controlled. Now the fcseries has integrated these two variables into one variable.

After modifying this variable and restarting the graphic interface, you can see that the interface is completely in English. However, you cannot call up the Chinese Input Method by pressing CTRL + space. You cannot add a Chinese Input Method to the input menu. We simply modified the Lang variable to change the system language settings. Of course, this step can also be modified using a tool in the graphic interface without modifying the configuration file.

Ii. Operation Level

This question seems to have nothing to do with the topic of this article, but now more and more Linux beginners are encountering problems with the Linux graphic interface, and these problems are also involved in the Chinese Input Method setting process, so I 'd like to mention it by the way.

After the current Linux installation is complete, it runs at the 5th system running level by default. In the system v-style UNIX system, the system is divided into different running levels, which is different from the Unix of the BSD branch, commonly used as 0 ~ 6. Seven levels:

0 Shutdown
1 single user
2. Multiple users without Networks
3. Multiple users with networks
4 reserved, user-defined
5. Multiple users in the graphic interface
6. restart the system

Since the current Linux system runs at 5th levels after installation, that is, the system directly enters the graphic interface after startup, instead of starting the graphic interface with startx or xinit after logon in character mode. This looks very convenient. But what are the disadvantages? Once you have changed some settings and displayed problems, the system keeps sending tokens between graphics and characters. It is very troublesome for new users to learn how to deal with them, and for those who study Linux, this is not conducive to understanding and learning some underlying Linux things. Old users who have used Linux for a long time know that in the past, Linux such as redhat6.0 had a default running node of 3. Even if redhat9.0 was later installed, they can select the default character logon or graphical logon. However, the current fcseries and most other versions directly allow users to directly log on to the GUI regardless of November 21. Although Linux is becoming easier for most cainiao, there is a lot of fun and new users cannot experience it.

Maybe you don't believe that there are a lot of problems when you log on to the system using graphics. Therefore, as a Linux and Solaris System Administrator with 6 years of Linux experience, I strongly recommend that you set the default running level of the system to 3rd after the system is installed. After logging on to the character terminal, manually enter the startx command to start the graphic interface. You can modify it as follows:
Use a text editor to modify the/etc/inittab File
Code:

ID: 5: initdefault:
 

Modify this line
Code:

ID: 3: initdefault:
 

After saving, the system restarts and starts to the character interface by default. The difference between different running levels is that the system starts services by default. For example, Level 3 does not start the x gui service by default, but Level 5 is started by default. There is no difference in nature, and it doesn't matter whether the functions of different levels are strong or not. Users can define different levels of default services on their own. At any running level, you can use the init command to switch to another running level.

3. Call up the Chinese Input Method:

The reason why I have to pay so much attention to the system operation level is that the understanding of the system starts from the bottom up.
First, change the default running level to 3. Of course, if you really don't want to modify it, temporarily use the init 3 command to switch to level 3rd.

In this way, you can start the graphic interface with startx, and then exit the graphic interface with CTRL + ALT + backspace. Note that I am talking about the "exit" graphical interface, instead of pressing CTRL + ALT + F2 to switch to a character terminal.

Okay, everything starts with startx. When you need to set something in a Linux system or configure a service, the most important thing is to know how it starts. You must know why. If you are free, set/etc/rc. the script that runs when the system is started in the d directory is read through, and you will be fully aware of what the configuration files under/etc are used for, how to modify, and what effect the modifications have. You can change the system as you like. This is what I have always stressed. You must know why. Be sure to go deep into the system, read the script, and learn to use commands and manually modify the system configuration file. In this way, you will have a thorough understanding of the system. Using graphical interfaces all day long cannot help you to have a thorough and in-depth understanding of the system, different Linux systems provide different GUI configuration programs, but the commands and configuration files are the same. The more underlying the program, the more universal it is. Therefore, you should first learn to manually configure and modify the system configuration file, and then use the graphical interface tool to modify it to reduce the workload.

I mentioned my ideas for solving the problem above. I started with this idea:

The Chinese input method is used in the graphic interface and is a program running in the graphic interface. Everything in the graphic interface is started and run by the startx program. This is the root cause of the problem.

Locate the startx location:
Code:

[Root @ gucuiwen ~] # Which startx
/Usr/x11r6/bin/startx
 

Check whether startx is a script or binary file:
Code:

[Root @ gucuiwen ~] # File/usr/x11r6/bin/startx
/Usr/x11r6/bin/startx: Bourne shell script text executable
 

Startx is a shell script, so I opened it for analysis and reading to see if I could find some clues about the input method startup process and related variables:
Code:

[Root @ gucuiwen ~] # Vi/usr/x11r6/bin/startx
 

I found the information of other scripts and configuration files called by the script during running:
Code:

Userclientrc = $ home/. xinitrc
Userserverrc = $ home/. xserverrc
Sysclientrc =/etc/X11/xinit/xinitrc
Sysserverrc =/etc/X11/xinit/xserverrc
 

It is also known that startx aims to find the available desktop system X server system and user-defined parameters in the system, and finally calls xinit to initialize the X graphical interface. I did not find the Code directly related to the start Input Method in the startx script, so I can be sure that the Code related to the input method is in the script called by startx. So I came
In the/etc/X11/xinit/directory, read and analyze the scripts in this directory. Some of these scripts are directly called by startx, and some are called by the scripts called by startx, there is a multi-level nesting relationship, and it's hard to figure out without patience. Finally, I found the Code related to the input method in the xinput. Sh script in the/etc/X11/xinit/xinitrc. d directory:
Code:

Lang_region = $ (echo $ tmplang | sed-E's //..*//')
Lang_region = "zh_cn" # This line is added after modification.
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
 

Through the analysis script, I know that when the GUI is started, the script determines whether to enable the Input Method Based on the Lang variable, and which language the input method is enabled. The problem is: We haven't changed the Lang variable to English, and the Lang variable obtained by the system is Chinese. Therefore, it knows that the Chinese input method needs to be enabled during the GUI startup process, however, after the Lang variable is changed to English, the system knows that the system is in English based on the Lang variable, so it does not start the Chinese input method or set or export the relevant variables, resulting in the unavailability of the Chinese input method. Therefore, as long as the "cheat" system in this script makes the input script "think" that the system is Chinese, does it run the Chinese Input Method and export the relevant variables? Therefore, through the analysis script, in xinput. sh:
Code:

Lang_region = $ (echo $ tmplang | sed-E's //..*//')

Added

Lang_region = "zh_cn"
 

Change lang_region = $ (echo $ tmplang | sed-E's // .. */') to lang_region = "zh_cn ".
You can add more rows for convenience in the future. Simply delete the added row.

Of course, set/etc/X11/xinit/xinput. d/$ {lang_region} in the for loop
/Etc/X11/xinit/xinput. d/ZH _ Cn can also be used.

Of course there are other ways to change the syntax. The premise is that you must understand the shell script syntax and understand the meaning of the script. After this modification, even if the system is in English, xinput. the sh script also reads/etc/X11/xinit/xinput. d/ZH _ Cn file, export the content, set input variables such as xmodifers, and run the iiimx input method program.

So why not directly run the iiimx input method program after the GUI is started? This is not the case. Because the input method program is a software that needs to run with the input application, many variables need to be exported during the operation. Directly running iiimx only runs the main program without relevant variables, and cannot work with the application to complete the input.

After the modification is completed, save the script file. Enter the startx command to start the graphic interface. Then you can use the system interface in full English and the Chinese input method. Note that the system is full of English, and the default input method is English, the application started from gnome or KDE menu cannot be switched to Chinese by pressing CTRL + space when entering Chinese for the first time. You need to click the input method icon on the taskbar to switch, after the first switch, you can use the CTRL + space shortcut to switch between Chinese and English input methods.

Iv. follow-up questions

Some software, such as open office, can be started through gnome or KDE menus. Even if you switch to the Chinese Input Method, you cannot enter Chinese. This is because the entire desktop system environment is in English, software "inherit" The English environment related variables, these software is "identification", is not to allow the input of Chinese, this time can open a gnome terminal, the Lang variable temporary set to zh_CN.UTF-8:

[Root @ gucuiwen ~] # Lang = "zh_CN.UTF-8"

Then, on this gnome terminal, run the following command to open office:

[Root @ gucuiwen ~] # Oowriter &

In this way, the open office inherits the Lang variable of the gnome terminal. After the tool bar and menu are started, they are both Chinese and can enter Chinese. By extension, any software can use this method to open the software on the Chinese interface and the software on the English interface as needed. To run software on the English interface, you only need to open the gnome or KDE menu and modify the Lang variable in the terminal when running the software on the Chinese interface, run the command from the terminal that modified the Lang variable. Of course, if you have installed fonts in other languages, you can run programs on interfaces of other languages. For example, Japanese:

[Root @ gucuiwen ~] # Lang = "ja_JP.UTF-8"
[Root @ gucuiwen ~] # Gedit &

The gedit editor opened with the above two commands is full Japanese interface, but can enter Chinese and English and display Japanese. In this way, a system, multiple languages and texts coexist.

Of course, the premise is that the Japanese font and locale are installed, otherwise all the text will be displayed as a series of question marks. In short, you must first understand the principles and then enjoy the fun of using Linux.

5. Quick setup steps:

1. Modify the/etc/sysconf/i18n file
Lang = "zh_CN.UTF-8"
Modify:
Lang = "en_US.UTF-8"

2. Modify the/etc/X11/xinit/xinitrc. d/xinput. Sh file and put one line in it:

Lang_region = $ (echo $ tmplang | sed-E's //..*//')

Modify:

Lang_region = "zh_cn"

3. Restart the graphic interface to display Chinese characters and enter Chinese characters on the English interface.

This article is from the csdn blog. For more information, see the source:Http://blog.csdn.net/s_kuang/archive/2009/12/21/5046696.aspx

The solution to Linux Garbled text is to specify the encoding format when storing the file in Linux during the seek stream when it is retrieved from the database. The Code is as follows:
Fileoutputstream Fos = new fileoutputstream (new file (filepath), true );
Writer out = new outputstreamwriter (FOS, "UTF-8 ");
Out. Write (s );
Out. Write ("/N ");
Out. Flush ();
FOS. Close ();
Out. Close ();
**********************
Vi. bash_profile
Export lang = zh_cn
VI/etc/sysconfig/i18n
Lang = "en_US.UTF-8"
Supported = "en_US.UTF-8: en_us: EN: zh_cn.gb18030: zh_cn: Zh: zh_tw.big5: zh_tw: Zh: ja_JP.UTF-8: ja_jp: Ja: ko_kr.euckr: ko_kr: Ko"
Sysfont = "latarcyrheb-sun16"
Only change the first one. It seems that the second one is especially important and must be changed.
*******************
1. garbled console Terminal

Add the following content to the last line of the/etc/profile file:

Export lc_all = "zh_cn.gb18030"

2. garbled XWindow Terminal

Add the following content to the last line of the/etc/sysconfig/i18n file:

Export lc_all = "zh_cn.gb18030"
**************************

Garbled characters can be divided into two situations:
1. Terminal garbled characters (pure Shell Interface)
VI/etc/profile
Export lc_all = "zh_cn.gb18030: zh_cn.gb2312: zh_cn.gbk: zh_cn: en_US.UTF-8: en_us: EN: Zh: zh_tw: zh_cn.big5"
Save and exit, reboot system ..

2. garbled characters in X-window (graphic interface)
VI/etc/sysconfig/i18n
Lang = "zh_cn.gb18030: zh_cn.gb2312: zh_cn.gbk: zh_cn: en_US.UTF-8: en_us: EN: Zh: zh_tw: zh_cn.big5"
Language = "zh_cn.gb18030: zh_cn.gb2312: zh_cn.gbk: zh_cn: en_US.UTF-8: en_us: EN: Zh: zh_tw: zh_cn.big5"
Save reboot...
*********************

A new Linux virtual machine was created, and a Chinese garbled problem occurred with vim. The solution was as follows:

VI/etc/sysconfig/i18n

Change content

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"

In this way, the Chinese characters are displayed on the SSH terminal.

The main modification content is zh_cn.gb18030. Note that the content of the VI personality is under the root directory, and pay attention to permissions.
**************************
Every time Linux is installed, SSH connections are used. garbled characters are displayed in Chinese. 51testing Software Testing Network

Work und: edit/etc/sysconfig/i18n and change lang = "zh_CN.UTF-8" to Lang = "zh_cn.gb2312 ".
& M ^ % k7x6x7i q051testing software testing network K! W |, r8c V & r5n
Disconnect and reconnect.
****************************
Solution to Chinese garbled characters in Linux (12:55:45)
Tags: linux windows garbled Chinese display it classification: Linux & MySQL

Copying files from windows to Linux is garbled. What should we do if we want to display Chinese Characters in Linux? First, let's test whether Chinese Characters in Linux can be properly displayed? A: Yes. The problem is obvious. The copied files in Windows cannot be displayed, indicating that the formats supported in Windows and Linux are different.
In Linux, UTF-8 is generally used, while in windows, gb2312 is used for file editing. Therefore, Chinese encoding is garbled. To correct this problem, it is actually very easy to convert the file into UTF-8 encoding format and then import it again.

Run the following command to convert the data:
Iconv-F gb2312-T UTF-8 test.txt> testutf8.tzt
(-F indicates the source code.-T indicates the conversion target, test.txtindicates the source file, and testutf8.txt generates the target encoding file)
Note: Use iconv-L to view the supported encoding formats. You can also add the encoding format:

The default value is utf8.

Command to manually change the configuration file:

Shell> VI/etc/sysconfig/i18n

Change lang = "zh_CN.UTF-8":

Lang = "zh_cn.gbk"

Save and close the configuration. Run the following command to make the configuration take effect:

Shell> source/etc/sysconfig/i18n

Display the terminal character encoding in simplified Chinese:

Shell> VI/etc/profile. d/Chinese. Sh

Add the following line: Export lc_all = zh_cn.gbk

Shell> source/etc/profile. d/Chinese. Sh
*******************************
Solve Java Chinese garbled characters in Linux

Release date: 2008.03.20 Source: CCID Author: skid

After JDK 15 ~ Create a fallback directory under/JRE/lib/fonts/and bake the font you want to use in Java to this directory.

The following method is passed in fc6. Assume that the user's JRE path is/usr/Java/jdk1.6.0 _ 03/JRE/

CD/usr/Java/jdk1.6.0 _ 03/JRE/lib/Fonts

Sudo mkdir fallback

Copy C:/Windows/fonts/simsun. TTC to the/usr/Java/jdk1.6.0 _ 03/JRE/lib/fonts/fallback folder.

OK!

The above method is garbled.

Ultimate method:

Apt-spy update // see my other article to quickly obtain source. List

Apt-Get update

Apt-Get install zhcon

2 -----------> install zhcon
 

Related Article

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.