python i18n实现

作者: songleo | 来源:发表于2016-12-16 22:20 被阅读330次

    本文简单介绍python实现i18n的方法。i18n来源于英文单词internationalization的首末字符i和n,18为中间的字符数,是“国际化”的简称。指让产品(出版物,软件和硬件等)无需做大的改变就能够适应不同的语言和地区的需要,对软件来说,表示在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面,本文以中文和英文作为例子,当系统默认语言是中文时,输出中文,当默认语言是英文时,输出英文。

    1)打印英文且支持i18n的python代码i18n_demo.py

    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import gettext
    import os
    
    appName = 'i18n_demo'
    languageDir = os.path.abspath('locale')
    gettext.bindtextdomain(appName, languageDir)
    gettext.textdomain(appName)
    
    _ = gettext.gettext
    
    print _('This is a translatable string.')
    

    若不需要支持i81n,python代码如下:

    
     #!/usr/bin/env python
     # -*- coding: utf-8 -*-
    
    print 'This is a translatable string.'
    
    

    2)创建pot文件,使用linux下xgettext命令

    xgettext -k_ -o i18n_demo.pot --from-code=UTF8 i18n_demo.py
    

    3)生成的i18n_demo.pot文件如下:

    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the PACKAGE package.
    # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    #
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2016-03-04 14:29+0800\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"
    "Language: \n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=CHARSET\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: i18n_demo.py:14
    
    msgid "This is a translatable string."
    msgstr ""
    

    4)根据不同语言创建po文件,cn代表中文,en表示英文,使用linux下的msginit命令,中间有停顿,回车即可,依次生成cn.po和en.po文件。

    创建中文po文件: msginit -l cn -i i18n_demo.pot
    
    创建英文po文件: msginit -l en -i i18n_demo.pot
    

    5)编辑上一步生成的po文件,切记将charset=CHARSET改成charset=utf-8,将Content-Transfer-Encoding的值改为8bit,然后将对应的中文翻译赋值给msgstr,即只需要编辑3个地方。如下:

    5.1)编辑中文cn.po文件

    # Language cn translations for py_i package.
    # Copyright (C) 2016 THE py_i'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the py_i package.
    # root <root@leo>, 2016.
    #
    msgid ""
    msgstr ""
    "Project-Id-Version: py_i 18n\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2016-03-04 14:29+0800\n"
    "PO-Revision-Date: 2016-03-04 14:38+0800\n"
    "Last-Translator: root <root@leo>\n"
    "Language-Team: Language cn\n" //zh_CN
    "Language: cn\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: i18n_demo.py:14
    
    msgid "This is a translatable string."
    msgstr "这是一个可译的字符串。"
    

    5.2)编辑英文en.po文件

    # English translations for py_i package.
    # Copyright (C) 2016 THE py_i'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the py_i package.
    # root <root@leo>, 2016.
    #
    msgid ""
    msgstr ""
    "Project-Id-Version: py_i 18n\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2016-03-04 14:29+0800\n"
    "PO-Revision-Date: 2016-03-04 14:38+0800\n"
    "Last-Translator: root <root@leo>\n"
    "Language-Team: English\n"
    "Language: en\n"
    "MIME-Version: 1.0\n"
    "Content-Type: text/plain; charset=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    "Plural-Forms: nplurals=2; plural=(n != 1);\n"
    
    #: i18n_demo.py:14
    
    msgid "This is a translatable string."
    msgstr "This is a translatable string."
    

    6)创建mo二进制文件,使用python的i18n工具msgfmt.py,且需在代码所在目录下提前创建以下目录:

    locale/cn/LC_MESSAGES/
    locale/en/LC_MESSAGES/
    

    指定中文mo文件输出路径到locale/cn/LC_MESSAGES/,英文的mo文件路径为locale/en/LC_MESSAGES/,mo文件名称必须相同,都是i18n_demo.mo。

    创建中文mo文件: python msgfmt.py -o locale/cn/LC_MESSAGES/i18n_demo.mo cn.po
    
    创建英文mo文件: python msgfmt.py -o locale/en/LC_MESSAGES/i18n_demo.mo en.po
    

    7)验证

    7.1)设置系统语言环境变量为中文,输出中文:

    root@leo:py_i18n# export LANG=cn && python i18n_demo.py
    
    这是一个可译的字符串。
    

    7.2)设置系统语言环境变量为英文,输出英文:

    root@leo:py_i18n# export LANG=en && python i18n_demo.py
    
    This is a translatable string.
    

    8)参考

    http://underthehood.blog.51cto.com/2531780/1663604
    http://www.cnblogs.com/kungfupanda/archive/2012/07/23/2604945.html

    相关文章

      网友评论

        本文标题:python i18n实现

        本文链接:https://www.haomeiwen.com/subject/xyhomttx.html