Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encode settings to display Chinese in Linux system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to encode and display Chinese in Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, the introduction of related variables

We know that most Linux systems are available in Chinese or English. Take FC4 Linux as an example, when the system is released, it is the same all over the world. Whether the system is in Chinese or English depends on the language pack you choose. People from different countries choose their own language packs when they install and use them, and the language in the application is not written dead, it invokes the relevant language according to the settings of the system, so an application is written without modification, and users in different countries around the world can use it in their mother tongue interface. This is the so-called internationalization, i18n for short. This is also the development trend of future software.

So, if I install different language packs and different fonts in the system, how does the system determine the language interface I want and call the relevant fonts? Which files and variables in the system control this?

On redHat and FC series Linux systems, the file that records the language used by the system by default is / etc/sysconfig/i18n. If the Chinese system is installed by default, the content of i18n is as follows:

Code:

The code is as follows:

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

LANG variable is the abbreviation of language. Users with a little English foundation can see that this variable determines the default language of the system, that is, the menu of the system, the toolbar language of the program, the default language of input method, and so on. SYSFONT is short for system font, which determines which font the system uses by default. The SUPPORTED variable determines the language that the system supports, that is, the language that the system can display. It is important to note that since computers originated in English-speaking countries, English is always supported by default no matter what you set these variables to, and English fonts are always included no matter what font you use.

Among these variables, the LANG variable is used in both character mode and graphical interface, and will be read and take effect after you log on to the system. I believe that many people will often encounter the error message displayed when entering Linux commands under the character interface. It is necessary to install Chinese software in character mode such as zhcon or cce in order to display Chinese error information normally. If I don't want him to display Chinese garbled codes, and I don't want to use zhcon to see a simple error message, what should I do? A simple 00:00 solution is to set the LANG variable:

Code:

The code is as follows:

[root@gucuiwen ~] # LANG= "en_US.UTF-8"

That is, temporarily set the language of the system to English, or, more simply, you can do this directly:

Code:

The code is as follows:

[root@gucuiwen ~] # LANG= ""

That is, clear the LANG variable, because English is supported in any case, after the LANG variable is cleared, the system defaults to English. After this setting, the error messages output in character mode are all in English. But this setting is temporary and only temporarily changes the bash variable LANG. It is invalid when you log out and log back in or switch to another character terminal.

By now, readers should have thought that this problem can be solved permanently by setting the LANG variable in the i18n file to the English "en_US.UTF-8". The modified file is as follows:

Code:

The code is as follows:

# 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 the character mode, but also in the graphical interface. Simply emptying in the character mode does not have a problem, but in the graphical interface, it will cause the Chinese language can not be displayed normally. In the past versions of the Re d ha t series, there is a variable called LANGUAGE in the i18n file, which specifically controls the language settings in the graphical interface. These two variables have been integrated into one variable in the current FC series.

After changing this variable and restarting the graphical interface, you can see that the interface is completely in English. However, pressing the ctrl+ space cannot bring up the Chinese input method, and the Chinese input method cannot be added to the input method menu. We simply modify the LANG variable to change the system language settings, of course, this step can also be modified with the graphical interface tools, rather than personally modify the configuration file.

Second, about the level of operation

This question seems to have nothing to do with the theme of this article, but now more and more novice linux users encounter linux graphical interface problems, and these problems are also involved in the process of setting up Chinese input methods, so I would like to mention them by the way.

After the current linux installation is complete, it runs at the fifth system run level by default. In a SYSTEM V-style UNIX system, the system is divided into different running levels, which is different from the UNIX of the BSD branch, which is commonly used in seven levels:

0 shutdown

1 single user

2 multi-user without network

3 multi-user with network

4 reserved, users can be self-defined

5 multi-user of graphical interface

6 restart the system

Because the current Linux system runs at the fifth level after installation, that is, after the system starts, it directly enters the graphical interface, instead of using startx or xinit to start the graphical interface after logging in in character mode. This looks convenient. But what's the harm? Once you change some settings and show problems, the system keeps hovering between graphics and characters, and beginners don't know how to deal with it, which is very troublesome, and for people who learn to study Linux, this is not conducive to understanding and learning some things at the bottom of Linux. Users who have used Linux for a long time know that in the past, Linux, such as redhat6.0, used to run with a default connection of 3, and even later RedHat9.0 can choose whether to log in by default or graphics during installation. But now the FC series and most other versions directly help users choose a direct graphical interface to log in. Although Linux is getting easier and easier for most rookies, it's a lot of fun that beginners can't experience.

As a Linux and Solaris system administrator with 6 years of experience in using Linux, I strongly recommend setting the default running level of the system at level 3 after the system is installed, and then manually enter the startx command to start the graphical interface after logging in at the character terminal. You can modify it in the following ways:

Modify the / etc/inittab file with a text editor to set the

Code:

The code is as follows:

Id:5:initdefault:

This line is modified to

Code:

The code is as follows:

Id:3:initdefault:

After saving and restarting, the system starts to the character interface by default. The difference between different runlevels lies in the service that the system starts by default, for example, runlevel 3 does not start the X graphical interface service by default, while runlevel 5 starts by default. In essence, there is no difference, let alone the question of functional strength between different levels. Users can define different levels of default services on their own. At any runlevel, users can use the init command to switch to another runlevel.

Third, call up the Chinese input method:

The reason why I have to spend so much ink on the above to talk about the running level of the system is because the understanding of the system starts from the bottom up.

First change the default run level to level 3, and of course, if you really don't want to change it, temporarily use the init 3 command to switch to level 3.

This allows you to start the graphical interface with startx and then exit the graphical interface with ctrl+alt+backspace. Please note that I said "exit" the graphical interface, not press ctrl+alt+F2 to cut to a character terminal.

All right, it all starts with startx. When you need to set up something or configure a service in a Linux system, the most important thing is to know how it all started. To know it, you must know why. If you have time to read through the scripts that run when the system starts up in the / etc/rc.d directory, you will know exactly what the various configuration files under / etc are used for, how to modify them, what the effect of the changes will be, and so on. When playing with the system, you can change it as much as you want. This is what I have always stressed. To know it, we must know why. Be sure to go deep into the system, read scripts, and learn to modify system configuration files with commands and manually. Only in this way can we have a thorough understanding of the system. Using graphical interface tools all day can not help you to have a thorough and in-depth understanding of the system. Different linux systems provide different graphical interface configuration procedures, but commands and configuration files are the same. The more underlying things are, the more versatile they are. Therefore, you should first learn to manually configure and modify the system configuration file, and then modify it with graphical interface tools in order to reduce the workload.

The above mentioned my idea of solving the problem. I started with this line of thinking:

The Chinese input method is used under the graphical interface and is a program running under the graphical interface. Everything in the graphical interface is opened and run by the startx program. This is the root of the problem.

Find out where the startx is:

Code:

The code is as follows:

[root@gucuiwen ~] # which startx / usr/X11R6/bin/startx

See if startx is a script or a binary file:

Code:

The code is as follows:

[root@gucuiwen ~] # file / usr/X11R6/bin/startx / usr/X11R6/bin/startx: Bourne shell script text executable

I found that startx was a shell script, so I opened it to analyze it and read it to see if I could find some clues about the input method startup process and related variables:

Code:

The code is as follows:

[root@gucuiwen ~] # vi / usr/X11R6/bin/startx

I found information about other scripts and configuration files that the script called while it was running:

Code:

The code is as follows:

Userclientrc=$HOME/.xinitrc userserverrc=$HOME/.xserverrc sysclientrc=/etc/X11/xinit/xinitrc sysserverrc=/etc/X11/xinit/xserverrc

And know that the purpose of startx is to find the available desktop system X server system, as well as user-defined parameters, and finally call xinit to initialize the X graphical interface. I didn't find the code directly related to the startup input method in the startx script, so I can be sure that the input method-related code is in the script invoked by startx. So I came to

/ etc/X11/xinit/ directory, read and analyze the scripts in this directory. Some of these scripts are called directly by startx, and some are called again by scripts called by startx. There is a multi-level nesting relationship. It is really difficult to understand if you don't have any 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:

The code is as follows:

Lang_region=$ (echo $tmplang | sed-e's / ".. * / /') lang_region=" zh_CN "# this line is modified to add 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 of the script, I know that when the graphical interface starts, the script is based on the LANG variable to decide whether to enable the input method, and which language to enable the input method and so on. The problem is: we have not changed the LANG variable into English, the LANG variable obtained by the system is in Chinese, therefore, it knows that the Chinese input method needs to be enabled during the startup of the graphical interface, but after changing the LANG variable into English, the system knows that the system is in English according to the LANG variable, it no longer starts the Chinese input method, nor sets and exports related variables, resulting in the Chinese input method not available. Therefore, as long as in this script, "deceive" the system and make the input method script "think" that the system is Chinese, won't it run the Chinese input method and export the relevant variables? So, by analyzing the script, what I did in xinput.sh:

Code:

The code is as follows:

Lang_region=$ (echo $tmplang | sed-e's / ".. * / /') followed by lang_region=" zh_CN "

It is also possible to directly modify lang_region=$ (echo $tmplang | sed-e's / ".. * / /') to lang_region=" zh_CN "

To add an extra line is to make it easier to change it later. Just delete the added line.

Of course, the / etc/X11/xinit/xinput.d/$ {lang_region} in the for loop should be

/ etc/X11/xinit/xinput.d/zh _ CN is also fine.

Of course, there are other changes, as long as you understand the syntax of the shell script and 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 its contents, set input method variables such as XMODIFERS, and run the iiimx input method program.

So why not run the iiimx input program directly after the graphical interface starts? As you can see from the experiment, it doesn't work at all. Because the input method program is the software that needs to run in conjunction with the input application, many variables need to be exported in the process of running. Running iiimx directly only runs the main program without related variables, so there is no way to cooperate with the application to complete the input work.

When the modification is complete, save the script file. Enter the startx command to start the graphical interface, and you can use the all-English system interface and Chinese input method. But it should be noted that: because the system is all English, the default input method is also English, the application started through the GNOME or KDE menu can not press the ctrl+ space to switch to Chinese for the first time, you need to use the mouse to click the input method icon on the taskbar to switch, after the first switch, you can use ctrl+ space shortcuts to switch between Chinese and English input methods.

Fourth, some follow-up questions.

Some software, such as Open Office, launched through the GNOME or KDE menu, cannot input Chinese even if you switch to the Chinese input method. This is because the environment of the entire desktop system is English, and the software "inherits" the relevant variables of the English environment. These software are "dead", that is, they are not allowed to enter Chinese. At this time, you can open a gnome terminal and temporarily set the LANG variable to zh_CN.UTF-8:

The code is as follows:

[root@gucuiwen ~] # LANG= "zh_CN.UTF-8"

Then in the gnome terminal, open open office with the command:

The code is as follows:

[root@gucuiwen ~] # oowriter &

In this way, Open Office "inherits" the LANG variable of the gnome terminal. After startup, toolbars and menus are all in Chinese and can be entered in Chinese. By extension, any software can use this method to open Chinese interface software and English interface software as needed. When you want to run the software in the English interface, just open it from the GNOME or KDE menu, and when you want to run the software in the Chinese interface, modify the LANG variable in the terminal and run it through the command from the terminal that has modified the LANG variable. Of course, if you also install fonts in other languages, you can also run the program in the interface of other languages. Such as Japanese:

The code is as follows:

[root@gucuiwen ~] # LANG= "ja_JP.UTF-8"

[root@gucuiwen ~] # gedit &

The gedit editor I opened with the above two commands has an all-Japanese interface, but can enter Chinese and English and display Japanese. In order to achieve the goal of coexistence of multiple languages and characters in one system.

Of course, the premise is that Japanese fonts and Japanese locale are installed, otherwise all the places with text will be displayed as a series of question marks. In short, first understand the principle, and then play how you want to play, do whatever you want, completely unrestricted, and fully enjoy the fun of using Linux.

5. Quick setup steps:

1. Modify the / etc/sysconf/i18n file to put

The code is as follows:

LANG= "zh_CN.UTF-8"

Modified to:

The code is as follows:

LANG= "en_US.UTF-8"

two。 Modify the / etc/X11/xinit/xinitrc.d/xinput.sh file to put one line in it:

The code is as follows:

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

Modified to:

The code is as follows:

Lang_region= "zh_CN"

3, restart the graphical interface, you can use the English interface and correctly display and enter Chinese.

This is the end of the content of "how to encode and set up to display Chinese in Linux system". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report