据说here Babel 可以提取 Python 和 Javascript 文件的 gettext 消息。
Babel comes with a few builtin extractors: python (which extracts messages from Python source files), javascript, and ignore (which extracts nothing).
命令行提取器已记录 here - 但没有使用示例。
也在same pointer上面提到了用于提取的配置文件,但没有太多展开。
当我在包含 js 文件的目录上运行提取器的基本命令时,我只生成了 .PO header ,但没有消息。
$ pybabel extract /path/to/js-dir
# Translations template for PROJECT.
# Copyright (C) 2012 ORGANIZATION
# This file is distributed under the same license as the PROJECT project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2012.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2012-04-22 19:39+1000\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"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"
$
这是我试图从 js 文件中提取消息的示例片段:
else if(data.status == "1"){
var follow_html = gettext('Follow');
object.attr("class", 'button follow');
object.html(follow_html);
var fav = getFavoriteNumber();
fav.removeClass("my-favorite-number");
if(data.count === 0){
data.count = '';
fav.text('');
}else{
var fmts = ngettext('%s follower', '%s followers', data.count);
fav.text(interpolate(fmts, [data.count]));
}
}
如果有人可以提供准确的 CLI 选项和配置设置以使提取工作或指向此类的指针,我将不胜感激。
最佳答案
创建一个包含以下内容的文件 (babel.cfg
):
[javascript:*.js]
encoding = utf-8
然后,做:
pybabel extract -F babel.cfg /path/to/js-dir
这应该足以让您拥有一些消息字符串。
顺便说一句,您可以通过以下方式查阅extract
命令的帮助:
pybabel extract --help
关于javascript - 使用 Babel CLI 提取器提取 Javascript gettext 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10267260/