7

这里说明Babel 可以为 Python 和 Javascript 文件提取 gettext 消息。

Babel 带有一些内置的提取器:python(从 Python 源文件中提取消息)、javascript 和 ignore(不提取任何内容)。

此处记录了命令行提取器- 但没有使用示例。

同样在上面的同一个指针中,提到了一个用于提取的配置文件,但没有太多扩展。

当我在带有 js 文件的目录上运行提取器的基本命令时,我只生成了 .PO 标头,但没有消息。

$ 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]));
    }
}

I would appreciate it if someone can provide exact CLI options and config settings to make the extraction work, or a pointer to such.

4

4 回答 4

6

Create a file (babel.cfg) with the following content:

[javascript:*.js]
encoding = utf-8

Then, do:

pybabel extract -F babel.cfg /path/to/js-dir

That should be enough for you to have some message strings.

BTW, you can consult the help for the extract command by doing:

pybabel extract --help
于 2012-05-16T23:37:02.080 回答
2

I had a similar issue and was able to get around it by disabling default keywords with babel.

pybabel extract -k __ -F babel.cfg --no-default-keywords /path/to/js-dir 

You must specify at least one keyword in the command when you disable the defaults (-k [keyword]). I chose -k __ because "__" was a pattern I was looking for.

Just use this command and replace the "__" after -k with one from your babel.cfg file.

Edit: this allows you to use your own keywords rather than gettext()

于 2012-06-15T17:28:08.160 回答
0

You can create an object in as flask global and translate it with gettext

g.i18n = {
    'Casa' : lazy_gettext('Home'),
    'Auto' : lazy_gettext('Car'),
    'Persona' : lazy_gettext('Person')
}

Then add it as a variable

<script>
    var i18n = {{ g.i18n | tojson }}
</script>

and use it in JS:

var labelTranslate = {
                    Casa: i18n.Casa,
                    Persona: i18n.Persona,
                    Auto: i18n.Auto
                };
于 2016-07-11T09:36:00.190 回答
0

You can actually use gettext directly in Javascript.

See: jsgettext. It allows you to use the standard *gettext functions, including the one using contexts and/or plural forms.

It can read PO/MO files or you can import custom made JSON files instead.

See this file of this project for a complete example.

于 2016-07-14T13:19:28.203 回答