[ English | русский | 日本語 | 中文 (简体, 中国) | English (United Kingdom) | Deutsch | Indonesia | 한국어 (대한민국) ]

工具

本页面涵盖了i18n行动的各种操作。

Zanata CLI

OpenStack使用Zanata作为翻译平台。 虽然翻译平台的大多数操作都是自动化的,但如果您想手动与翻译平台进行通信,可以使用 Zanata CLI

用户配置

您需要在 $HOME/.config/zanata.ini 中创建一个包含用户特定配置的配置文件。 有关如何创建配置文件的信息,请参阅 Zanata CLI配置

项目配置

要与翻译平台进行通信,您需要在您感兴趣的项目的顶层目录中准备一个名为 zanata.xml 的项目配置文件。OpenStack项目的git仓库中不包含 zanata.xml ,因此您需要手动创建它。

以下是 zanata.xml 的示例。 在大多数情况下,您只需要编辑 projectproject-version

<config xmlns="http://zanata.org/namespace/config/">
  <url>https://translate.openstack.org/</url>
  <project>horizon</project>
  <project-version>master</project-version>
  <project-type>gettext</project-type>
  <src-dir>.</src-dir>
  <trans-dir>.</trans-dir>
  <rules>
    <rule pattern="**/*.pot">{path}/{locale_with_underscore}/LC_MESSAGES/{filename}.po</rule>
  </rules>
  <excludes>.tox/**</excludes>
</config>

从Zanata中拉取翻译

要从Zanata下载翻译,请在进入项目目录后运行以下命令。 您通常只对几种语言感兴趣,所以``–locales``选项会比较有用。 有关更多选项,请参阅``zanata pull –help``的输出。

$ zanata-cli pull --locales ja,ko-KR,zh-CN

处理文档项目

备注

本文是关于openstack-manuals项目的。 截至Pike开发周期结束,`社区范围内的文档迁移工作<https://specs.openstack.org/openstack/docs-specs/specs/pike/os-manuals-migration.html>`__正在进行中。 此处记录的流程可能会在不久的将来发生变化。

OpenStack文档使用RST格式。 翻译RST文档的步骤包括:

  • 分片:从RST文档生成PO模板

  • 上传:将翻译资源上传到Zanata

  • 翻译:使用Zanata管理翻译,包括翻译记忆库和词汇表管理

  • 下载:用自动化脚本下载翻译结果。

  • 构建:从RST文档和翻译结果构建HTML。

Sphinx是一种将RST源文件转换为各种输出格式(包括POT和HTML)的工具。 在进行以下步骤之前,您需要安装Sphinx。 几乎所有项目都在其仓库中包含了 test-requirements.txt 文件,您可以通过检查此文件来检查所需的Sphinx版本。

$ pip install Sphinx

或者,更方便的方法是:

$ pip install -r test-requirements.txt

分片

我们使用sphinx-build将RST文件转换为POT文件。 因为我们希望每个文档只有一个POT文件,所以我们在sphinx-build之后使用msgcat合并这些POT。

$ sphinx-build -b gettext doc/[docname]/source/ doc/[docname]/source/locale/
$ msgcat doc/[docname]/source/locale/*.pot > doc/[docname]/source/locale/[docname].pot

上传

我们使用 Zanata CLI 来把POT文件上传到翻译平台。

下载

我们使用 Zanata CLI 从翻译平台下载翻译好的PO文件。

构建

在使用sphinx-build构建HTML文件之前,我们需要将单个大PO文件中的翻译填充到那些小的PO文件。 例如:

$ msgmerge -o doc/[docname]/source/locale/zh_CN/LC_MESSAGES/A.po \
    doc/[docname]/source/locale/zh_CN/LC_MESSAGES/[docname].po \
    doc/[docname]/source/locale/A.pot

然后,对于每个PO文件,我们应该执行以下命令来构建MO文件:

$ msgfmt doc/[docname]/source/locale/zh_CN/LC_MESSAGES/A.po \
   -o doc/[docname]/source/locale/zh_CN/LC_MESSAGES/A.mo

最后,我们可以生成HTML文件:

$ sphinx-build -D "language='zh_CN' doc/[docname]/source/ \
    doc/[docname]/build/html

处理python项目

对于大多数Python项目,I18N的首选工具是gettext和babel。 gettext模块为您的Python模块和应用程序提供国际化(I18N)和本地化(L10N)服务。 Babel是用于国际化Python应用程序的工具集合。

提取

您可以使用pybabel将代码中的消息提取到PO模板中(POT),其中** PROJECT 是项目名称,例如``nova``, VERSION **是版本号。 请注意,如果您只是在本地使用它们,则可以省略``–project``和``–version``选项,因为它们只是在POT文件头中使用。

$ pybabel extract \
    --add-comments Translators: \
    -k "_C:1c,2" -k "_P:1,2" \
    --project=${PROJECT} --version=${VERSION} \
    -o ${modulename}/locale/${modulename}.pot \
    ${modulename}

例如, 假设是nova,

$ pybabel extract \
    --add-comments Translators: \
    -k "_C:1c,2" -k "_P:1,2" \
    --project=nova --version=${VERSION} \
    -o nova/locale/nova.pot nova/

上传

对于OpenStack中的每个Python项目,都有一个自动化作业来提取消息,生成PO模板并上传到Zanata,这是由“commit”事件触发的。 请参阅 here

下载

对于OpenStack中的每个Python项目,每天都有一个自动化作业将PO文件中的翻译下载到每个项目的源文件夹下的“locale”文件夹中。 请参阅:doc:here <infra>。 它会在Gerrit中生成审核请求。 在:doc:`review <reviewing-translation-import>`之后,PO文件中的翻译将会被合并。

使用翻译

要在python服务器项目中显示已翻译的消息,您需要编译消息目录,还需要按照 `oslo.i18n documentation <https://docs.openstack.org/oslo.i18n/latest/user/usage.html#displaying-translated-messages>`__中所述的说明配置服务器服务。

处理Horizon项目

For horizon related projects, Django, a framework which horizon is built on, provides integrated translation support.

备注

Unlike documentations and python projects, horizon and plugins use zh-hans and zh-hant for Chinese locales instead of zh-cn and zh-tw respectively since Wallaby release. This follows the Django recommendation which happened in Django 1.7. The details are found in the mailing list post.

提取

horizon provides a command to extract the messages in code to PO template (POT). Run the following command in your repository.

$ python manage.py extract_messages -m ${MODULE_NAME}

where MODULE_NAME is a python module name of horizon or its plugin.

For example, in case of manila-ui,

$ python manage.py extract_messages -m manila_ui

The above command is a wrapper for pybabel and the translation job uses pybabel directly.

上传

For each horizon related project in OpenStack, there is an automation job to extract the messages , generate PO template and upload to Zanata, which is triggered by the “commit” event. See here.

下载

For each horizon related project in OpenStack, there is an automation job daily to download the translations in PO file to the “locale” folder under the source folder of each project. See here. It will generate a review request in Gerrit. After review, the translation in PO file will be merged.

备注

As noted above, in Wallaby or later releases, zh-hans and zh-hant are used for Chinese locales. On the other hand, zh-cn and zh-tw continues to be used in Zanata. When the job downloads translations from Zanata, the job stores translations for zh-cn and zh-tw to zh-hans and zh-hant directories under locale.

使用翻译

To display translated messages in OpenStack dashboard, you need to compile message catalogs. Django picks up translated messages automatically once they are compiled. The following command compiles messages in your project.

$ python manage.py compilemessages

项目维护

备注

下面的脚本依赖于几个python模块。 要安装这些依赖项,请运行 pip install -e requirements.txt

更方便的方法是使用 tox ,例如 tox -e venv -- python <script-name>.

** tox 可在PyPI上获得,也可在各种Linux发行版中找到。 ``pip install tox``或者``apt-get install python-tox``(以Ubuntu发行版为例)会安装 tox **

与Zanata同步翻译者清单

I18n项目维持着一个语言小组及其成员的列表。 Stackalytics使用该列表来收集翻译统计信息(详见参见:ref:stats-stackalytics)。 它也被以下脚本使用。

列表的文件名是 tools/translation_team.yaml.

这个列表是Zanata上信息的缓存,我们需要让它与Zanata保持同步。

要同步翻译者清单,请运行以下命令:

tox -e zanata-users-sync

上面在内部运行以下内容:

python tools/zanata/zanata_users.py --output-file tools/zanata/translation_team.yaml

获取翻译统计信息

AC status in I18n project is determined based on translation statistics in a specific period.

tools/zanata/zanata_stats.py 脚本用于从Zanata获取翻译统计。

运行脚本:

tox -e venv -- python ./tools/zanata/zanata_stats.py <options>

--help 选项展示了详细用法.

提取Zanata用户信息

At the moment, the I18n SIG Chair needs to maintain the AC list of the I18n project manually around the end of each release cycle. This requires name and e-mail address of individual translators.

脚本``tools/zanata/zanata_userinfo.py``对此有所帮助。 它通过读取YAML文件生成CSV文件,该文件包含具有用户名和电子邮件地址的翻译人员列表(例如,translation_team.yaml),与Zanata API交互。

备注

此脚本需要Zanata管理员权限。