总代理商/安装工具集成¶
巴别塔提供了集成到 setup.py 脚本,基于 distutils 属于Python标准库或第三方的包 setuptools 包裹。
当正确安装了Babel时,这些命令在默认情况下可用,并且 setup.py 正在使用 setuptools 。对于使用普通旧式的项目 distutils ,需要显式注册命令,例如:
from distutils.core import setup
from babel.messages import frontend as babel
setup(
...
cmdclass = {'compile_catalog': babel.compile_catalog,
'extract_messages': babel.extract_messages,
'init_catalog': babel.init_catalog,
'update_catalog': babel.update_catalog}
)
compile_catalog¶
这个 compile_catalog 命令类似于GNU msgfmt 工具,因为它从PO文件中获取消息目录,并将其编译成二进制MO文件。
如果命令已正确安装或注册,则项目的 setup.py 脚本应允许您使用命令::
$ ./setup.py compile_catalog --help
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
Options for 'compile_catalog' command:
...
运行该命令将生成二进制MO文件::
$ ./setup.py compile_catalog --directory foobar/locale --locale pt_BR
running compile_catalog
compiling catalog to foobar/locale/pt_BR/LC_MESSAGES/messages.mo
选项¶
这个 compile_catalog 命令接受以下选项:
期权
描述
--domain采购订单文件的域(默认为小写项目名称)
--directory(-d)基目录的名称
--input-file(-i)输入文件的名称
--output-file(-o)输出文件的名称
--locale(-l)新本地化字符串的区域设置
--use-fuzzy(-f)也包括“模糊”的翻译
--statistics打印有关翻译的统计信息
如果 directory 已指定,但 output-file 不是,则输出文件的默认文件名将为::
<directory>/<locale>/LC_MESSAGES/<domain>.mo
如果既不是 input_file 也不 locale 选项时,此命令将在基目录中查找与给定域匹配的所有编录文件,并将每个编录文件编译为同一目录中的MO文件。
这些选项可以在命令行上指定,也可以在 setup.cfg 文件。
extract_messages¶
这个 extract_messages 命令可与GNU相媲美 xgettext 程序:它可以从各种不同的源文件中提取可本地化的消息,并从收集的消息中生成PO(可移植对象)模板文件。
如果命令已正确安装或注册,则项目的 setup.py 脚本应允许您使用命令::
$ ./setup.py extract_messages --help
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
Options for 'extract_messages' command:
...
运行该命令将生成PO模板文件::
$ ./setup.py extract_messages --output-file foobar/locale/messages.pot
running extract_messages
extracting messages from foobar/__init__.py
extracting messages from foobar/core.py
...
writing PO template file to foobar/locale/messages.pot
方法映射¶
属性指向的配置文件可以指定文件模式到提取方法(和选项)的映射 --mapping-file 选项如上所示。或者,您可以直接在中配置映射 setup.py 使用关键字参数将 setup() 功能:
setup(...
message_extractors = {
'foobar': [
('**.py', 'python', None),
('**/templates/**.html', 'genshi', None),
('**/templates/**.txt', 'genshi', {
'template_class': 'genshi.template:TextTemplate'
})
],
},
...
)
选项¶
这个 extract_messages 命令接受以下选项:
期权
描述
--charset要在输出文件中使用的字符集
--keywords(-k)除缺省值之外还要查找的以空格分隔的关键字列表
--no-default-keywords不包括默认关键字
--mapping-file(-F)映射配置文件的路径
--no-location不要在文件名和行号中包含位置注释
--omit-header请勿在标题中包含msgid“”条目
--output-file(-o)输出文件的名称
--width(-w)设置输出线宽(默认值76)
--no-wrap不要将长于输出行宽度的消息行拆分为几行
--input-dirs应扫描邮件的目录
--sort-output生成排序输出(默认值为false)
--sort-by-file按文件位置对输出进行排序(默认为False)
--msgid-bugs-address设置邮件错误报告的电子邮件地址
--copyright-holder在输出中设置版权所有者
--add-comments (-c)在输出文件中放置带有标签的注释挡路(或前面的关键字行)。用逗号(,)分隔多个标记
这些选项可以在命令行上指定,也可以在 setup.cfg 文件。在后一种情况下,上述选项将成为该部分的条目 [extract_messages] ,并且选项名称将更改为使用下划线字符而不是破折号,例如:
[extract_messages]
keywords = _ gettext ngettext
mapping_file = mapping.cfg
width = 80
这相当于从命令行调用命令,如下所示:
$ setup.py extract_messages -k _ -k gettext -k ngettext -F mapping.cfg -w 80
任何路径名都是相对于 setup.py 文件。对于布尔选项,请使用“true”或“false”值。
init_catalog¶
这个 init_catalog 命令基本上等同于GNU msginit 程序:它基于PO模板文件(POT)创建新的翻译目录。
如果命令已正确安装或注册,则项目的 setup.py 脚本应允许您使用命令::
$ ./setup.py init_catalog --help
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
Options for 'init_catalog' command:
...
运行该命令将生成PO文件::
$ ./setup.py init_catalog -l fr -i foobar/locales/messages.pot \
-o foobar/locales/fr/messages.po
running init_catalog
creating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'
选项¶
这个 init_catalog 命令接受以下选项:
期权
描述
--domain采购订单文件的域(默认为小写项目名称)
--input-file(-i)输入文件的名称
--output-dir(-d)输出目录的名称
--output-file(-o)输出文件的名称
--locale新本地化字符串的区域设置
如果 output-dir 已指定,但 output-file 不是,则输出文件的默认文件名将为::
<output_dir>/<locale>/LC_MESSAGES/<domain>.po
这些选项可以在命令行上指定,也可以在 setup.cfg 文件。
update_catalog¶
这个 update_catalog 命令基本上等同于GNU msgmerge 程序:它基于PO模板文件(POT)更新现有翻译目录。
如果命令已正确安装或注册,则项目的 setup.py 脚本应允许您使用命令::
$ ./setup.py update_catalog --help
Global options:
--verbose (-v) run verbosely (default)
--quiet (-q) run quietly (turns verbosity off)
--dry-run (-n) don't actually do anything
--help (-h) show detailed help message
Options for 'update_catalog' command:
...
运行该命令将更新PO文件::
$ ./setup.py update_catalog -l fr -i foobar/locales/messages.pot \
-o foobar/locales/fr/messages.po
running update_catalog
updating catalog 'foobar/locales/fr/messages.po' based on 'foobar/locales/messages.pot'
选项¶
这个 update_catalog 命令接受以下选项:
期权
描述
--domain采购订单文件的域(默认为小写项目名称)
--input-file(-i)输入文件的名称
--output-dir(-d)输出目录的名称
--output-file(-o)输出文件的名称
--locale新本地化字符串的区域设置
--ignore-obsolete不要在输出中包含过时的消息
--no-fuzzy-matching(-N)不要使用模糊匹配
--previous保留翻译后消息的以前的msgid
如果 output-dir 已指定,但 output-file 不是,则输出文件的默认文件名将为::
<output_dir>/<locale>/LC_MESSAGES/<domain>.po
如果既不是 input_file 也不 locale 选项时,此命令将在基目录中查找与给定域匹配的所有编录文件,并更新每个编录文件。
这些选项可以在命令行上指定,也可以在 setup.cfg 文件。