从各种源中提取字符串、折叠空白、支持上下文(msgctxt)和合并.pot文件。

tower的Python项目详细描述


Tower为Jinja提供了一些附加功能,它是i18n扩展, 还有巴别塔图书馆。

细节:

  • 从各种来源提取字符串:python、javascript和.lhtml文件。
  • 折叠所有字符串中的空白以防止出现笨拙的msgid。
  • 在所有gettext和ngettext调用中支持gettext context(msgctxt)。
  • 支持合并php和python.pot文件。这是暂时的。如果你愿意 为了支持这一点,请查看Translate Toolkit中的phppo2pypo。

要求

  • Django
  • 巴贝尔
  • Jinja2和Jingo
  • 翻译工具包

requirements.txt 详细情况。

安装

从pypi安装:

pip install tower

源代码位于http://github.com/clouserw/tower

从github安装,使用:

pip install -e git://github.com/clouserw/tower.git#egg=tower

配置

添加到django设置文件中的INSTALLED_APPS

INSTALLED_APPS = (
    # ...
    'tower',
    # ...
)

然后配置。

django.conf.settings.DOMAIN_METHODS

Default: None–you must set this.

The value is a dict of domain to file spec and extraction method tuples.

For example, this creates a domain “messages” and in that domain extracts all the l10n strings from .py and .html files:

DOMAIN_METHODS = {
    'messages': [
        ('fjord/**.py', 'tower.tools.extract_tower_python'),
        ('fjord/**.html', 'tower.tools.extract_tower_template'),
    ]
}

Use ^{tt3}$ for Python files and ^{tt4}$ for Jinja2 templates.

The l10n strings will be saved in a .pot file with the name of the domain. In the above example, it’d be ^{tt5}$.

django.conf.settings.STANDALONE_DOMAINS

Default: ^{tt7}$

By default, all domains specified in ^{tt8}$ get merged into one big .pot file. If you don’t want that, you can specify which domains get their own .pot file with ^{tt9}$.

django.conf.settings.TOWER_KEYWORDS

TODO: Document this.

django.conf.settings.TOWER_ADD_HEADERS

Default: False

If you have trouble extracting strings with Tower, try setting this to True.

django.conf.settings.ROOT

This points to the source code directory where you want your ^{tt13}$ directory to be.

django.conf.settings.path

This is a function that takes arbitrary set of args and combines them with ^{tt15}$ to form a new path.

Example:

import os

# The settings file is in fjord/fjord/settings/base.py. From
# base, up two directories is the initial fjord directory
# which is where all the source code and the fjord Django
# project are.
ROOT = os.path.dirname(os.path.dirname(__file__))

path = lambda *args: os.path.abspath(os.path.join(ROOT, *args))

用法

摘录:

./manage.py extract

合并:

./manage.py merge

关于空白的注释

当tower提取字符串时,它将折叠空白。这样就更容易了 对于定位仪。这也意味着你需要使用ugettext,ungetext,ugettext\u lazy 从塔上取下来。否则传入的msgid将没有 它们的空白被去掉,因此与.mo文件中的任何内容都不匹配。

关于safe-ness

l10n字符串自动标记为jinja2的“安全”,因此它们不会 HTML转义:

{{ _('Hello <strong>World</strong>') }}

这和预期的一样。但是,当插入l10n字符串时,它将 被标记为“不安全”并转义,除非您使用jingo’s|fe()helper(它将转义 参数,但不是插入到其中的字符串)。像这样:

{{ _('Hello <strong>{0}</strong>')|fe(user.nickname) }}

运行测试

运行:

python run_tests.py

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaCenter是JavaFX2中不可执行的窗格   java Docker将容器连接到本地数据库   java无法通过Spring从Redis获得正确的值   java为什么要将数组转换为列表再转换为数组   Java泛型在方法中放入字符串或整数参数   在Cloud Bigtable SDK中找不到java类   java Mavensiteplugin无法加载生成的源(Jaxb)   java GWT序列化和Appengine通道Api   PrintWriter out=new PrintWriter(sWriter)和PrintWriter out=response之间的java差异。getWriter()   空手道DSL中的javascript,在java参数调用中传递变量时如何转义单引号   windows 64位java可以与32位tomcat一起使用   为for循环| Java输入参数   java您可以更改SWT选项卡项的背景和/或前景颜色吗?   用java生成6位pin码   java如何从另一个通量中排除通量中的所有元素   java无法调用“javafx.scene.control.ComboBox.getItems()”,因为“Controller.getMyBox()”的返回值为null   将Java字符串与数组匹配时出现问题   java如何使用HttpsUrlConnection对SSL连接使用代理身份验证?   java如何检查XML元素是否包含CDATA?