Posted by: kezhong | April 3, 2009

V0.8 Release – Migrate aNd Manage

In past two weeks, I have improved my program to make it strong and practical. I tested it in the Fedora 10 of the simplified Chinese, traditional Chines and English environment. It worked perfectly.

The primary function of my program is to check the locale environment. If it is a simplified Chinese version, it will display the simplified Chinese word, and so on. The follow is my program and its specification.

My program

#!/usr/bin/python

############################################################
# Author:    Kezhong Liang
# Purpose:    Test I18N
# Date:        Apr 3, 2009
############################################################

#################### Part of Import ##########################
import gettext
import locale, copy, os, re, struct, sys
from errno import ENOENT

############## Part of Function _expand_lang #################
def _expand_lang(locale):
from locale import normalize
locale = normalize(locale)
COMPONENT_CODESET   = 1 << 0
COMPONENT_TERRITORY = 1 << 1
COMPONENT_MODIFIER  = 1 << 2
# split up the locale into its base components
mask = 0
pos = locale.find(‘@’)
if pos >= 0:
modifier = locale[pos:]
locale = locale[:pos]
mask |= COMPONENT_MODIFIER
else:
modifier = ”
pos = locale.find(‘.’)
if pos >= 0:
codeset = locale[pos:]
locale = locale[:pos]
mask |= COMPONENT_CODESET
else:
codeset = ”
pos = locale.find(‘_’)
if pos >= 0:
territory = locale[pos:]
locale = locale[:pos]
mask |= COMPONENT_TERRITORY
else:
territory = ”
language = locale
ret = []
for i in range(mask+1):
if not (i & ~mask):  # if all components for this combo exist …
val = language
if i & COMPONENT_TERRITORY: val += territory
if i & COMPONENT_CODESET:   val += codeset
if i & COMPONENT_MODIFIER:  val += modifier
ret.append(val)
ret.reverse()
return ret

#################### Part of Getting Locale #####################
def get_locale(domain):
localedir = os.path.join(sys.prefix, ‘share’, ‘locale’)

languages = []
for envar in (‘LANGUAGE’, ‘LC_ALL’, ‘LC_MESSAGES’, ‘LANG’):
val = os.environ.get(envar)
if val:
languages = val.split(‘:’)
break
if ‘C’ not in languages:
languages.append(‘C’)
return val
#################### Part of Config ##########################
############ .mo filename ############
domain_name = “mnm”
############ The range of dealing with languages ##############
## If you want to extand the other language, ##
## just add it to the list of lang_range     ##
lang_range = [‘zh_CN.UTF-8’, ‘zh_TW.UTF-8’]
if get_locale(domain_name) in lang_range:
lang = get_locale(domain_name)
else:
lang = ‘en_US.UTF-8’

cat = gettext.translation(domain_name,”/usr/share/locale”, languages=[lang])
_ = cat.gettext

#################### Part of Test #######################
## The above can be used in your program              ##
## The below can be removed                           ##
print _(“Hello World!”)
print _(“How are you!”)

Specification

1.Marked the messages which want to display with “_()” in the program.
For example, if there is a string “Hello World!” want to display, you
should marked it to “_(“Hello World!”)”

2. Produce a .po file using pygettext.py program which you can find in
the Python Tools directory. Suppose the name of your python program is
test1.py

# ./pygettext.py -k _ -d mnm -o mnm.po test1.py

3. Edit cnmnm.po file the above produced and change “charset=CHARSET” to
“charset=UTF-8”. Then you can begin to translate as below.

——————————————————————
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid “”
msgstr “”
“Project-Id-Version: PACKAGE VERSION\n”
“POT-Creation-Date: 2009-03-17 17:05+EDT\n”
“PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n”
“Last-Translator: FULL NAME <EMAIL@ADDRESS>\n”
“Language-Team: LANGUAGE <LL@li.org>\n”
“MIME-Version: 1.0\n”
“Content-Type: text/plain; charset=UTF-8\n”    ##Change CHARSET to UTF-8
“Content-Transfer-Encoding: ENCODING\n”
“Generated-By: pygettext.py 1.5\n”

# No. 1
msgid “Hello World!”
msgstr “世界您好!”                        ####The translation of the above

# No. 2
msgid “How are you!”
msgstr “您好吗!”                          ####The translation of the above
——————————————————————

4. Produce a mnm.mo file using msgfmt.py program
#./msgfmt mnm.po

5. Change the name of file mnm.po to the name you easily recognize.
#mv mnm.mo cnmnm.mo

6. Repeat the step 3 to 5, if you want to support other language.

7. Copy .mo file into the appropriate directory.
#cp cnmnm.mo /usr/share/locale/zh_CN/LC_MESSAGES/mnm.mo
#cp twmnm.mo /usr/share/locale/zh_TW/LC_MESSAGES/mnm.mo
#cp usmnm.mo /usr/share/locale/en_US/LC_MESSAGES/mnm.mo


Responses

  1. I can tell that this is not the first time you mention this topic. Why have you chosen it again?

  2. Yes Indeed advertise your extra home. You unwisely obtained well over you can afford
    else a lender might have given you a mortgage.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: