命令行界面¶
Babel包括用于处理消息目录的命令行界面,类似于各种GNU gettext
Linux/Unix系统上常用的工具。
正确安装后,Babel提供一个名为 pybabel
::
$ pybabel --help
Usage: pybabel command [options] [args]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
--list-locales print all known locales and exit
-v, --verbose print as much as possible
-q, --quiet print as little as possible
commands:
compile compile message catalogs to MO files
extract extract messages from source files and generate a POT file
init create new message catalogs from a POT file
update update existing message catalogs from a POT file
这个 pybabel
脚本提供了许多执行实际工作的子命令。这些子命令如下所述。
编译¶
这个 compile
子命令可用于将翻译目录编译为二进制MO文件:
$ pybabel compile --help
Usage: pybabel compile [options]
compile message catalogs to MO files
Options:
-h, --help show this help message and exit
-D DOMAIN, --domain=DOMAIN
domains of PO files (space separated list, default
'messages')
-d DIRECTORY, --directory=DIRECTORY
path to base directory containing the catalogs
-i INPUT_FILE, --input-file=INPUT_FILE
name of the input file
-o OUTPUT_FILE, --output-file=OUTPUT_FILE
name of the output file (default
'<output_dir>/<locale>/LC_MESSAGES/<domain>.mo')
-l LOCALE, --locale=LOCALE
locale of the catalog to compile
-f, --use-fuzzy also include fuzzy translations
--statistics print statistics about translations
如果 directory
已指定,但 output-file
不是,则输出文件的默认文件名将为::
<directory>/<locale>/LC_MESSAGES/<domain>.mo
如果既不是 input_file
也不 locale
选项时,此命令将在基目录中查找与给定域匹配的所有编录文件,并将每个编录文件编译为同一目录中的MO文件。
提取液¶
这个 extract
子命令可用于从源文件集合中提取可本地化消息::
$ pybabel extract --help
Usage: pybabel extract [options] <input-paths>
extract messages from source files and generate a POT file
Options:
-h, --help show this help message and exit
--charset=CHARSET charset to use in the output file (default "utf-8")
-k KEYWORDS, --keywords=KEYWORDS, --keyword=KEYWORDS
space-separated list of keywords to look for in
addition to the defaults (may be repeated multiple
times)
--no-default-keywords
do not include the default keywords
-F MAPPING_FILE, --mapping-file=MAPPING_FILE, --mapping=MAPPING_FILE
path to the mapping configuration file
--no-location do not include location comments with filename and
line number
--add-location=ADD_LOCATION
location lines format. If it is not given or "full",
it generates the lines with both file name and line
number. If it is "file", the line number part is
omitted. If it is "never", it completely suppresses
the lines (same as --no-location).
--omit-header do not include msgid "" entry in header
-o OUTPUT_FILE, --output-file=OUTPUT_FILE, --output=OUTPUT_FILE
name of the output file
-w WIDTH, --width=WIDTH
set output line width (default 76)
--no-wrap do not break long message lines, longer than the
output line width, into several lines
--sort-output generate sorted output (default False)
--sort-by-file sort output by file location (default False)
--msgid-bugs-address=MSGID_BUGS_ADDRESS
set report address for msgid
--copyright-holder=COPYRIGHT_HOLDER
set copyright holder in output
--project=PROJECT set project name in output
--version=VERSION set project version in output
-c ADD_COMMENTS, --add-comments=ADD_COMMENTS
place comment block with TAG (or those preceding
keyword lines) in output file. Separate multiple TAGs
with commas(,)
-s, --strip-comments, --strip-comment-tags
strip the comment TAGs from the comments.
--input-dirs=INPUT_DIRS
alias for input-paths (does allow files as well as
directories).
--ignore-dirs=IGNORE_DIRS
Patterns for directories to ignore when scanning for
messages. Separate multiple patterns with spaces
(default ".* ._")
--header-comment=HEADER_COMMENT
header comment for the catalog
的含义 --keyword
取值如下:
传递一个简单的标识符,如下所示
_
提取所有函数调用的第一个(且仅第一个)参数_
,要提取第一个参数以外的其他参数,请添加冒号和用逗号分隔的参数索引。例如,
dngettext
函数通常将可翻译的字符串作为第二个和第三个参数,因此可以将dngettext:2,3
。某些参数不应解释为可翻译的字符串,而应解释为上下文字符串。为此,请将“c”附加到参数索引。例如:
pgettext:1c,2
。在C++和Python中,您的函数可能会根据它们接受的参数数量而表现出不同的行为。对于这个用例,您可以在冒号后面添加一个后面跟“t”的整数。在这种情况下,只有当关键字具有指定的参数总数时,该关键字才会与函数调用匹配。例如,如果您有一个函数
foo
它的行为就像gettext
(参数是一条消息)或pgettext
(参数是上下文和消息)根据它接受的是一个参数还是两个参数,您可以传递--keyword=foo:1,1t --keyword=foo:1c,2,2t
。
默认关键字等效于传递::
--keyword=_
--keyword=gettext
--keyword=ngettext:1,2
--keyword=ugettext
--keyword=ungettext:1,2
--keyword=dgettext:2
--keyword=dngettext:2,3
--keyword=N_
--keyword=pgettext:1c,2
--keyword=npgettext:1c,2,3
初始化¶
这个 init 子命令根据PO模板文件创建新的翻译目录:
$ pybabel init --help
Usage: pybabel init [options]
create new message catalogs from a POT file
Options:
-h, --help show this help message and exit
-D DOMAIN, --domain=DOMAIN
domain of PO file (default 'messages')
-i INPUT_FILE, --input-file=INPUT_FILE
name of the input file
-d OUTPUT_DIR, --output-dir=OUTPUT_DIR
path to output directory
-o OUTPUT_FILE, --output-file=OUTPUT_FILE
name of the output file (default
'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')
-l LOCALE, --locale=LOCALE
locale for the new localized catalog
-w WIDTH, --width=WIDTH
set output line width (default 76)
--no-wrap do not break long message lines, longer than the
output line width, into several lines
更新¶
这个 update 子命令根据PO模板文件更新现有的新翻译目录:
$ pybabel update --help
Usage: pybabel update [options]
update existing message catalogs from a POT file
Options:
-h, --help show this help message and exit
-D DOMAIN, --domain=DOMAIN
domain of PO file (default 'messages')
-i INPUT_FILE, --input-file=INPUT_FILE
name of the input file
-d OUTPUT_DIR, --output-dir=OUTPUT_DIR
path to base directory containing the catalogs
-o OUTPUT_FILE, --output-file=OUTPUT_FILE
name of the output file (default
'<output_dir>/<locale>/LC_MESSAGES/<domain>.po')
--omit-header do not include msgid entry in header
-l LOCALE, --locale=LOCALE
locale of the catalog to compile
-w WIDTH, --width=WIDTH
set output line width (default 76)
--no-wrap do not break long message lines, longer than the
output line width, into several lines
--ignore-obsolete whether to omit obsolete messages from the output
--init-missing if any output files are missing, initialize them first
-N, --no-fuzzy-matching
do not use fuzzy matching
--update-header-comment
update target header comment
--previous keep previous msgids of translated messages
如果 output_dir
已指定,但 output-file
不是,则输出文件的默认文件名将为::
<directory>/<locale>/LC_MESSAGES/<domain>.mo
如果既不是 output_file
也不 locale
选项时,此命令将在基目录中查找与给定域匹配的所有编录文件,并更新每个编录文件。