i18n_tool-7
-----------

This article describes how to establish the zh_TW.Big5 and zh_CN.GB2312
locale environments in GNU/Linux system using glibc-2.0.7 and XFree86-3.3.4
and previous versions (including XFree86-3.3.4).

Please note that because glibc-2.0.7 and XFree86-3.3.4 are completely out of
date from the current point of view, and the I18N environment is not mature
in these packages, so they are not suitable to establish the locales of
Asian languages (especially for our zh_TW.Big5 locale). Although we could
overcome these through some techeniques discussed in this article, but there
still have a lot of invisible problems. If you are still using these packages,
we strongly recommand to upgrade them at least to glibc-2.1.3 and XFree86-3.3.6
and refer to the descriptions in README.En to establish the locale environment.
Unless you are very insisted, otherwise please do not read the following
contents of this article.

For the versions earlier then glibc-2.0.7, they are completely not suitable
for Asian locale environments. So we do not discuss them at all.


A. zh_TW.Big5:
---------------

Assuming that your system is GNU/Linux with glibc-2.0.7, XFree86 3.3.2.X -
3.3.4, you can follow the steps below to establish the zh_TW.Big5 locale
environment. Please note: Before you starting, you should make sure that if
all the packages or files mentioned in the steps are already in your system
or not. If yes, you can just bypass the corresponding step, or you have to
finish them one after one.

If you are using CLE (Chinese Linux Extension) distribution, then CLE should
already have prepared all of these, so you don't have to do anything at all.


1. Install libwcsmbs. This lib is used to fix glibc-2.0.7 such that it 
   can support to zh_TW.Big5 and other Asian locales. You can find it in

        ftp://xcin.linux.org.tw/pub/xcin/i18n/old/libwcsmbs-0.0.4.tar.gz

   The steps to install it are

	tar -xvzf libwcsmbs-0.0.4.tar.gz
	cd libwcsmbs-0.0.4
	make
	make DESTDIR=/usr/local install

2. Install wcsmbs-locale package. Please note that you have to install the
   version which is modified by CLE, since in that modification the zh_TW.Big5
   locale data has been added. You can find it in

        ftp://xcin.linux.org.tw/pub/xcin/i18n/old/wcsmbs-locale-0.4.7.tar.gz

   The steps to install it are

	tar -xvzf wcsmbs-locale-0.4.7.tar.gz
	cd wcsmbs-locale-0.4.7
	make map
	make
	make install

3. If in your system there already has a zh_TW.Big5/ directory in
   /usr/share/locale/, and there also has a LC_CTYPE file in that directory,
   then you can skip this step. If not, please go to the directory
   i18n_tool/localedata/zh_TW.Big5/ and run the command:

        localedef -i zh_TW.Big5 -f BIG5_1984 zh_TW.Big5

   After that, there will be a zh_TW.big5 directory in the /usr/share/locale/.
   But in the convention of CLE, the locale name is zh_TW.Big5. Here we will
   follow this convention So we should change the directory name "zh_TW.big5"
   into "zh_TW.Big5".


4. Please check the /usr/X11R6/lib/X11/locale/ directory to see if there
   exists the zh_TW.Big5/ directory, and in that directory there
   is a file named XLC_LOCALE. If not, please put the file
   i18n_tool/localedata/zh_TW.Big5/XLC_LOCALE into the directory

	/usr/X11R6/lib/X11/locale/zh_TW.Big5

   (you might need to create this directory). Then please modify the file

	/usr/X11R6/lib/X11/locale/locale.dir

   to add the line in the end of the file:

zh_TW.Big5/XLC_LOCALE   zh_TW.Big5

   Besides, please check the contents of 

	/usr/X11R6/lib/X11/locale/zh_TW.Big5/XLC_LOCALE

   to see the following two lines

use_stdc_env            True
force_convert_to_mb     True

   Please make sure that their values are both "True".


5. You have to prepare suitable Chinese (Big5) fonts. From the definition
   of XLC_LOCALE in step 4, the font to display Chinese in zh_TW.Big5 locale
   is named BIG5-0. Therefore your system should have these fonts. Take my
   system as an example, since I have installed the twmoe fonts (you can get
   them from: ftp://linux.cis.nctu.edu.tw/packages/chinese ftp site), which
   has the font name ended with "big5-1", so I have to alias these font names
   to end with "big5-0":

THH:thhpc $ xlsfonts | grep big5
-twmoe-kai-medium-r-normal--16-160-75-75-c-160-big5-1
-twmoe-kai-medium-r-normal--24-240-75-75-c-240-big5-1
-twmoe-kai-medium-r-normal-fs-16-160-75-75-c-160-big5-0
-twmoe-kai-medium-r-normal-fs-24-240-75-75-c-240-big5-0

THH:thhpc $ more /usr/X11R6/lib/X11/fonts/chinese/fonts.alias 
-twmoe-kai-medium-r-normal-fs-16-160-75-75-c-160-big5-0 -twmoe-kai-medium-r-normal--16-160-75-75-c-160-big5-1
-twmoe-kai-medium-r-normal-fs-24-240-75-75-c-240-big5-0 -twmoe-kai-medium-r-normal--24-240-75-75-c-240-big5-1


6. In most releases of XFree86, there is a problem in Xlib such that it 
   cannot handle Big5 encoding correctly. In some systems (such as CLE)
   this problem has been fixed, but in others it does not. You can do the
   following tests: 

   Please add the following lines into your $HOME/.Xdefaults:

xtDefaultFontSet: -*-big5-0,-adobe-*-iso8859-1
xedit*international: True
xedit*fontSet: -*-big5-0,-adobe-*-iso8859-1

   and run the commands

	xrdb merge $HOME/.Xdefaults
	export LC_CTYPE=zh_TW.Big5
	xedit &

   Now you can try to use mouse to cut & copy the following Big5 Chinese
   texts into xedit:

	您好,這是一段 Big5 編碼的文字。

   If you can see Chinese characters in xedit, then your Xlib is OK,
   otherwise you should fix your Xlib.

   There are two methods to fix this problem:

   a. patch Xlib, rebuild Xlib:

      This might be a terrible work, but it is also the most completed
      way. Please download XFree86 source code first (it would be better
      to get the version of 3.3.2 or above), then apply the 
      "i18n_tool/lcGen/lcGenConv.c.patch" patch into "lib/X11/lcGenConv.c"
      of XFree86's source tree:

	cd <path_of_XFree86>/lib/X11
	patch -p0 < <path_of_i18n_tool>/lcGen/lcGenConv.c.patch

      Finally you can follow the documents of XFree86 to rebuild libX11.
      Please note that the patch described above comes from the modification
      of XFree86-3.3.2.3, but it should be appliable to XFree86-3.3.2 and
      its later releases. If there are error messages when you applying
      this patch, you probably have to modify the code by yourself. It 
      should not be difficult :-))

   b. You can also go to i18n_tool/lcGen directory and run make. After that
      you will get a lcGen.so shared library. Every time when you want to 
      run Xi18n related programs, please use this command:

	LD_PRELOAD=<path_of>/lcGen.so <run Xi18n_program>

      That is, we use lcGen.so to replace the problem function of Xlib.



B. zh_CN.GB2312:
-----------------

The method to establish this locale is very similar to that of zh_TW.Big5. 
The steps are also the same. Hence I only mention the differences of them
in detail:

1. Install the libwcsmbs-0.0.4.tar.gz

2. Install the wcsmbs-locale-0.4.7.tar.gz

3. If there is already a directory zh_CN.GB2312 in /usr/share/locale/ of
   your system and in that directory there is a LC_CTYPE file, then you
   can skip this step. Otherwise, please go to the directory
   i18n_tool/localedata/zh_CN.GB2312/ and run

        localedef -i zh_CN.GB2312 -f GB2312 zh_CN.GB2312

   After that, there will be a directory zh_CN.gb2312 appeared in
   /usr/share/locale/ diretory. Please change its name to be zh_CN.GB2312.

4. XFree86 already has the XLC_LOCALE file for GB encoding, so you don't
   have to install it. However, it is placed in

	/usr/X11R6/lib/X11/locale/zh/

   Therefore you have to modify the file locale.dir to add this line:

zh/XLC_LOCALE   zh_CN.GB2312

5. You should install the GB fonts, with the font names ended with
   "GB2312.1980-0". You can try to find the GB font tar file in XFree86
   packages, or search for them in the ftp sites.


6. There is no problem in Xlib to handle the GB encoding like the problem
   in Big5 encoding, so you don't need to do any fixes in Xlib.


C. Run I18N applications:
--------------------------

In GNU/Linux with packages glibc-2.0.7 and XFree86-3.3.4 and previous versions,
to run the I18N applications you not only have to set locale, but also have
to do LD_PRELOAD works. For the details about how to set locale, please refer
to the discussion in README.En. For LD_PRELOAD, we should discuss it for
several cases:

1. If your program is not a X Window application, then no matter in zh_TW.Big5
   or zh_CN.GB2312 locale, to excute any I18N program you should use:

   LD_PRELOAD="<dir of libwcsmbs.so>/libwcsmbs.so" <run i18n_program>

   where libwcsmbs.so is the libwcsmbs package you installed in the 1st step.
   Please note that this LD_PRELOAD is special for glibc-2.0.7.

2. If your program is a X Window application, then in zh_CN.GB2312 locale,
   to excute the Xi18n program the command is the same as 1.

3. If your program is a X Window application, then in zh_TW.Big5 locale and
   your Xlib is already patched and rebuilt, then to excute the Xi18n program
   the command is the same as 1.

4. If your program is a X Window application, then in zh_TW.Big5 locale and
   your Xlib is not patched and rebuilt, then you should use the following
   command:

   LD_PRELOAD="<dir of libwcsmbs.so>/libwcsmbs.so <dir of lcGen.so>/lcGen.so" \
	<run i18n_program>

   That is, besides the special need "libwcsmbs.so" for glibc-2.0.7, you have
   to add a "lcGen.so". This is used to fix the Big5 character handling
   problem for Xlib.



Author: Tung-Han Hsieh
Email:  thhsieh@linux.org.tw