GAE webapp应用程序与巴贝尔国际化 [英] GAE webapp application internationalization with Babel

查看:171
本文介绍了GAE webapp应用程序与巴贝尔国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用 BABEL 国际化Google App Engine网络应用程序?我在这里寻找所有阶段:


  1. 标记要翻译的字符串。
  2. 提取



  3. 配置您的应用程序以加载浏览器请求的正确语言

$ 1)在代码和模板中使用_()(或gettext())。在模块全局变量或类定义中设置的转换后的字符串应该使用某种形式的lazy gettext(),因为当导入模块时i18n将不可用。

2)使用pybabel提取所有翻译。在这里,我们传递两个要扫描的目录:模板目录和应用程序目录。这将在/ locale目录中创建一个messages.pot文件,并在这些目录中找到所有字符串。 babel.cfg是提取配置,取决于您使用的模板引擎:

  $ pybabel extract -F ./babel。 cfg -o ./locale/messages.pot ./templates/ ./app/ 



<3>初始化每种语言的目录。这只做一次。这里我们初始化三个翻译,en_US,es_ES和pt_BR,并使用第2步创建的messages.pot文件:

  $ pybabel init -l en_US -d ./locale -i ./locale/messages.pot 
$ pybabel init -l es_ES -d ./locale -i ./locale/messages.pot
$ pybabel init - l pt_BR -d ./locale -i ./locale/messages.pot

翻译讯息。他们将在每个翻译目录中的.mo文件中。
翻译所有语言环境后,编译它们:

  $ pybabel compile -f -d ./locale 

稍后,如果添加了新的翻译,请重复步骤2并使用新的.pot文件更新它们:

  $ pybabel更新-l pt_BR -d ./locale/ -i ./locale/messages.pot 

然后翻译新的字符串并重新编译翻译。



4)策略可能会有所不同。对于每个请求,您必须设置要使用的正确翻译,并且可能需要缓存加载的翻译以便在随后的请求中重复使用。


How would you go about internationalizing a Google App Engine webapp application using BABEL? I am looking here for all the stages:

  1. Marking the strings to be translated.
  2. Extracting them.
  3. Traslating
  4. Configuring your app to load the right language requested by the browser

解决方案

1) use _() (or gettext()) in your code and templates. Translated strings set in the module globals or class definitions should use some form of lazy gettext(), because i18n won't be available when the modules are imported.

2) Extract all translations using pybabel. Here we pass two directories to be scanned: the templates dir and the app dir. This will create a messages.pot file in the /locale directory with all strings found in these directories. babel.cfg is the extraction configuration that varies depending on the template engine you use:

$ pybabel extract -F ./babel.cfg -o ./locale/messages.pot ./templates/ ./app/

3) Initialize a directory for each language. This is done only once. Here we initialize three translations, en_US, es_ES and pt_BR, and use the messages.pot file created on step 2:

$ pybabel init -l en_US -d ./locale -i ./locale/messages.pot 
$ pybabel init -l es_ES -d ./locale -i ./locale/messages.pot 
$ pybabel init -l pt_BR -d ./locale -i ./locale/messages.pot

Translate the messages. They will be in .mo files in each translation directory. After all locales are translated, compile them:

$ pybabel compile -f -d ./locale

Later, if new translations are added, repeat step 2 and update them using the new .pot file:

$ pybabel update -l pt_BR -d ./locale/ -i ./locale/messages.pot

Then translate the new strings and compile the translations again.

4) The strategy here may vary. For each request you must set the correct translations to be used, and probably want to cache loaded translations to reuse in subsequent requests.

这篇关于GAE webapp应用程序与巴贝尔国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆