从各种源中提取字符串、折叠空白、支持上下文(msgctxt)和合并.pot文件。
tower的Python项目详细描述
Tower为Jinja提供了一些附加功能,它是i18n扩展, 还有巴别塔图书馆。
- 作者:wil clouser和contributors
- 许可证:BSD
- 兼容性:python 2.6和2.7、django 1.4、1.5和1.6
- 要求:Django、Babel、Jinja2、Jingo和Translate工具包
- 项目URL:https://github.com/clouserw/tower
- 文档:http://tower.readthedocs.org/en/latest/
细节:
- 从各种来源提取字符串:python、javascript和.lhtml文件。
- 折叠所有字符串中的空白以防止出现笨拙的msgid。
- 在所有gettext和ngettext调用中支持gettext context(msgctxt)。
- 支持合并php和python.pot文件。这是暂时的。如果你愿意 为了支持这一点,请查看Translate Toolkit中的phppo2pypo。
安装
从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