Extracting Javascript gettext messages using Babel CLI extractor

旧时模样 提交于 2019-12-03 16:01:27

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
AssHat_

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()

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
                };

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!