目录



>>> from helper import info; info()
页面更新时间: 2020-02-22 18:04:05
操作系统/OS: Linux-4.19.0-8-amd64-x86_64-with-debian-10.3 ;Python: 3.7.3

5.4. 使用 polib 处理翻译文件

本文档涵盖了polib的最新版本。

polib是一个用于操作、创建、修改gettext文件(pot、po和mo文件)的库。您可以加载现有文件、迭代其条目、添加、修改条目、注释或元数据等,或者从头创建新的采购订单文件。

polib支持现成的任何版本的python,范围从2.4到最新的3.x版本。

polib现在相当稳定,被许多开源项目使用。

polib是完全免费和开源的,使用的许可证是MIT许可证。它是由大卫让-路易斯于2006年开发的,目前仍在积极维护。

要快速启动和运行,请参阅快速启动指南,其中介绍了安装和使用POLIB所需的所有步骤。有关如何安装和使用polib的详细信息,请阅读下面列出的文档。

5.5. 快速入门指南

安装polib

polib需要python 2.5或更高版本。

安装polib有几种方法,这在安装部分中进行了介绍。

对于不耐烦的人,最简单的方法是通过pip安装polib,只需键入:

pip install polib

关于gettext目录的一些基本知识

gettext目录由许多条目组成,每个条目包含原始未翻译字符串与其对应翻译之间的关系。

给定目录中的所有条目通常属于一个项目,所有翻译都用一种目标语言表示。其中一个PO文件条目的结构示意图如下:

#  translator-comments
#. extracted-comments
#: reference...
#, flag...
msgid untranslated-string
msgstr translated-string

一个简单的条目可以是这样的:

#: lib/error.c:116
msgid "Unknown system error"
msgstr "Error desconegut del sistema"

polib有两个使用gettext目录的主要入口点:

  • pofile()mofile()函数加载现有的po或mo文件,

  • 创建新的po或mo文件的POFileMOFile类。

参考文献 * Gettext手册* PO文件格式* MO文件格式

加载现有目录

加载目录并检测其编码

在这里,po文件的编码由polib自动检测(polib通过解析pofile头部的字符集来检测):

>>> import polib
>>> po = polib.pofile('catalog.po')

加载目录并显式指定编码

由于某些原因,您可能希望显式地指定文件编码(例如,因为在po文件头中没有指定字符集),这样做:

>>> import polib
>>> po = polib.pofile(
>>>     'catalog.po',
>>>     encoding='iso-8859-15'
>>> )

加载mo文件

在某些情况下,您可能被迫加载一个mo文件(例如,因为po文件不可用),polib处理这种情况:

>>> import polib
>>> mo = polib.mofile('catalog.mo')
>>> print(mo)
msgid ""
msgstr ""
"Project-Id-Version: MapServer-docn"
"Report-Msgid-Bugs-To: n"
"POT-Creation-Date: 2014-03-25 12:19n"
"PO-Revision-Date: 2017-09-22 12:51+0000n"
"Last-Translator: Jacolin <yjacolin@free.fr>n"
"Language-Team: Chinese (http://www.transifex.com/mapserver/mapserver-doc/language/zh/)n"
"Language: zhn"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"Plural-Forms: nplurals=1; plural=0;n"

msgid "2014-01-03 - MapCache 1.2.1 is released"
msgstr "2014-01-03 - MapCache 1.2.1 发布"

msgid "MapCache 1.2.1 has been released. Head to download to obtain a copy."
msgstr "MapCache 1.2.1 现已发布。请到下载以获取副本。"

msgid "MapServer Announcements"
msgstr "MapServer 公告"

对于po文件,mofile还允许显式地指定编码。

从头创建po目录

polib允许您从头创建目录,这可以通过POFile类来完成,例如,创建一个简单的目录,您可以这样做:

>>> import polib
>>>
>>> po = polib.POFile()
>>> po.metadata = {
>>>     'Project-Id-Version': '1.0',
>>>     'Report-Msgid-Bugs-To': 'you@example.com',
>>>     'POT-Creation-Date': '2007-10-18 14:00+0100',
>>>     'PO-Revision-Date': '2007-10-18 14:00+0100',
>>>     'Last-Translator': 'you <you@example.com>',
>>>     'Language-Team': 'English <yourteam@example.com>',
>>>     'MIME-Version': '1.0',
>>>     'Content-Type': 'text/plain; charset=utf-8',
>>>     'Content-Transfer-Encoding': '8bit',
>>> }

这段代码创建了一个空的pofile及其元数据,现在可以像这样将条目添加到po文件中:

>>> entry = polib.POEntry(
>>>     msgid=u'Welcome',
>>>     msgstr=u'Bienvenue',
>>>     occurrences=[('welcome.py', '12'), ('anotherfile.py', '34')]
>>> )
>>> po.append(entry)

要将文件保存到磁盘,只需:

>>> po.save('newfile.po')

并编译相应的mo文件:

>>> po.save_as_mofile('newfile.mo')

More examples Iterating over entries

Iterating over all entries (by default POFiles contains all catalog entries, even obsolete and fuzzy entries):

更多的例子

遍历条目

遍历所有条目(默认情况下pofile包含所有目录条目,甚至是过时的和模糊的条目):

>>> import polib
>>>
>>> po = polib.pofile('catalog.po')
>>> for entry in po:
>>>     print(entry.msgid, entry.msgstr)
2014-01-03 - MapCache 1.2.1 is released 2014-01-03 - MapCache 1.2.1 发布
MapCache 1.2.1 has been released. Head to download to obtain a copy. MapCache 1.2.1 现已发布。请到下载以获取副本。
MapServer Announcements MapServer 公告

遍历除过时项目外的所有项目:

>>> import polib
>>>
>>> po = polib.pofile('catalog.po')
>>> valid_entries = [e for e in po if not e.obsolete]
>>> for entry in valid_entries:
>>>     print (entry.msgid, entry.msgstr)
2014-01-03 - MapCache 1.2.1 is released 2014-01-03 - MapCache 1.2.1 发布
MapCache 1.2.1 has been released. Head to download to obtain a copy. MapCache 1.2.1 现已发布。请到下载以获取副本。
MapServer Announcements MapServer 公告

只迭代已翻译的条目:

>>> import polib
>>>
>>> po = polib.pofile('catalog.po')
>>> for entry in po.translated_entries():
>>>     print (entry.msgid, entry.msgstr)
2014-01-03 - MapCache 1.2.1 is released 2014-01-03 - MapCache 1.2.1 发布
MapCache 1.2.1 has been released. Head to download to obtain a copy. MapCache 1.2.1 现已发布。请到下载以获取副本。
MapServer Announcements MapServer 公告

等等……您还可以遍历由以下POFile方法返回的POEntry对象列表:

  • untranslated_entries()

  • fuzzy_entries()

获取翻译条目的百分比

>>> import polib
>>>
>>> po = polib.pofile('catalog.po')
>>> print (po.percent_translated())
100

将po文件编译为mo文件,并将mo文件转换为po文件

编译po文件:

>>> import polib
>>>
>>> po = polib.pofile('catalog.po')
>>> # to get the binary representation in a variable:
>>> modata = po.to_binary()
>>> # or to save the po file as an mo file
>>> po.save_as_mofile('catalog.mo')

将mo文件反向为po文件:

>>> mo = polib.mofile('catalog.mo')
>>> # to get the unicode representation in a variable, just do:
>>> podata = u"%s" % mo
>>> # or to save the mo file as an po file
>>> mo.save_as_pofile('catalog.po')

欢迎使用Polib的文档!