当前位置:   article > 正文

gettext:Python本地/国际化模块_pygettext如何关闭国际化

pygettext如何关闭国际化

简介

gettext是兼容GNU gettext的国际化模块,使用纯Pyhton实现。

简单的示例:

first_gettext.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import gettext

# 一些设置
t = gettext.translation(
    'first', 'locale',
    fallback=True,
    )
_ = t.gettext  # 一般使用_,简单方便

print(_('This message is we want to translate.'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行:

deepin@deepin-PC:~/source/demo/python$ python3 first_gettext.py 
This message is we want to translate.
  • 1
  • 2
  • 3

我们还没有添加其他的语言支持,在这种情况下如果fallback为True,那么直接使用内置_中的字符串,如果fallback为False,返回“找不到翻译文件”异常:

deepin@deepin-PC:~/source/demo/python$ python3 first_gettext.py 
Traceback (most recent call last):
  File "first_gettext.py", line 9, in <module>
    fallback=False,
  File "/usr/lib/python3.5/gettext.py", line 529, in translation
    raise OSError(ENOENT, 'No translation file found for domain', domain)
FileNotFoundError: [Errno 2] No translation file found for domain: 'first'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

下面我们来创建翻译文件

提取源文件中使用的字符串(使用pygettext.py,或使用GUN gettext中的工具):

xgettext -o first.po first_gettext.py --from-code utf-8
  • 1

生成的first.po,文件名和代码中translation第一个参数对应:

# 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: 2017-11-30 14:51+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"

#: first_gettext.py:13
msgid "This message is we want to translate."
msgstr ""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

修改文件中信息,姓名、邮件、版本等等;charset改为UTF-8;然后翻译:

# 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: 2017-11-30 14:19+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: zh_CN\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: first_gettext.py:13
msgid "This message is we want to translate."
msgstr "这是我们要翻译的消息"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

源码中translation第二个参数代表翻译文件保存路径,创建目录结构:

  mkdir -p locale/zh_CN/LC_MESSAGES
  mv first.po locale/zh_CN/LC_MESSAGES
  cd locale/zh_CN/LC_MESSAGES
  • 1
  • 2
  • 3

创建mo文件:

msgfmt -o first.mo first.po
  • 1

在中文环境运行我们程序进行测试:

deepin@deepin-PC:~/source/demo/python$ python3 first_gettext.py
这是我们要翻译的消息
  • 1
  • 2

文章引用:

http://blog.topspeedsnail.com/archives/9539

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/475261
推荐阅读
相关标签
  

闽ICP备14008679号