使用 Python 3 对 Pyramid 的国际化支持的当前状态是什么?
目前,Pyramid 用于此的包 lingua 和 babel 似乎与 Python 3 不兼容。
有https://bitbucket.org/felixschwarz/babel-py3k,但没有正式发布。
同样对于 lingua,我只能找到一个叫做 lingua3k 的东西的影子,但是到它的链接到处都被破坏了,也没有正式发布。
我怎样才能让自己启动并运行 Pyramid i18n 和 Python 3?
使用 Python 3 对 Pyramid 的国际化支持的当前状态是什么?
目前,Pyramid 用于此的包 lingua 和 babel 似乎与 Python 3 不兼容。
有https://bitbucket.org/felixschwarz/babel-py3k,但没有正式发布。
同样对于 lingua,我只能找到一个叫做 lingua3k 的东西的影子,但是到它的链接到处都被破坏了,也没有正式发布。
我怎样才能让自己启动并运行 Pyramid i18n 和 Python 3?
这是对 2013 年 3 月发布的两个答案的更新答案,适用于任何偶然发现此问题的人。
Babel for Python 3 的工作尚未正式发布。维护者有一张未解决的公开票:http: //babel.edgewall.org/ticket/209
然而,一群人拿起了火炬并开始了一个非官方的 BitBucket 回购来存放他们在 Babel3 上的工作:https ://bitbucket.org/babel3_developers/babel3
Pyramid 也使用 lingua。目前如果你尝试easy_install/pip install lingua,将会失败。唯一的原因是 xlwt 尚未正式移植。
如果要安装 lingua,则必须手动修补 xlwt。
目前,Python 3 中没有 Babel 的官方版本。pypi 也没有发布任何内容(即 easy_install babel/babel3 或 pip install babel/babel3 无法正常工作)。但是,您可以使用非官方版本。
Pyramid 的 i18n 依赖于另一个名为 lingua 的 Python 模块。另一个名为 xlwt 的模块依赖项存在问题。它也已在此处进行了非正式修补:https ://github.com/tonyroberts/xlwt
我无法找到有关 python 3(babel 和 lingua)中金字塔 i18n 帮助程序包状态(当前或未来)的任何信息。
为了解决这个不便,我做了4件事:
细节:
我在我的项目中使用 python2.7 创建了一个非常精简的第二虚拟和非常精简的环境。只需将其命名为 env_python_2.7 并将其放在我项目的根目录中。
然后在 setup.py 所在的目录中,我创建了另一个安装文件 setup_i18n.py。该文件仅对 i18n 支持所需的内容提出要求。我的 setup_i18n.py 文件如下所示:
from setuptools import setup, find_packages
requires = [
'Babel',
'lingua',
]
setup(name='my_project',
packages=find_packages(),
install_requires=requires,
message_extractors={'.': [
('**.py', 'lingua_python', None),
('**.pt', 'lingua_xml', None),
]},
)
import argparse
import subprocess
import sys
from pyramid.paster import (
get_appsettings,
setup_logging,
)
description = """\
A script to facilitate the translation of strings in the app.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('ini_file', help='Ini file to setup environment for this script')
parser.add_argument('-a', '--add', metavar='locale',
help='Add locale name of new language for translation (eg: en, de)')
def main(argv=sys.argv):
args = parser.parse_args()
setup_logging(args.ini_file)
settings = get_appsettings(args.ini_file)
# Python 2.7 is needed for translation strings at this time, because the
# available translation string tools are not compatible with Python 3
# at this time
python27 = settings['my_project.python27_dir'] + '/bin/python'
setup_file = settings['my_project.i18n_setup_file']
if args.add:
subprocess.call([python27, setup_file, 'init_catalog', '-l', args.add])
subprocess.call([python27, setup_file, 'extract_messages'])
subprocess.call([python27, setup_file, 'update_catalog'])
subprocess.call([python27, setup_file, 'compile_catalog'])
if __name__ == '__main__':
main()
entry_points="""\
[paste.app_factory]
main = my_project:main
[console_scripts]
my_project_db = my_project.script.initializedb:main
my_project_translate = my_project.script.translate:main
""",
现在我可以通过调用'python3 setup.py my_project_translate'来 i18n 我的应用程序
要以您的方式翻译 Python 应用程序,您通常有两个不同的步骤:
第一部分由 Babel 和 lingua 处理,第二部分是 gettext (或任何围绕它的包装,例如translationstring。
简短回答:下载 Poedit 进行目录工作并使用常规 Pyramid 库进行翻译。
因此,如果 Babel 和 lingua 不适合您,您可以自己创建消息目录。然后,使用现有.pot
文件,您可以.po
为每种语言创建文件。如果你使用Poedit它可以自动将你的.po
文件编译成.mo
文件。只需使用 Python 的标准库gettext就可以处理这些,而无需任何依赖。
所以这就是你要做的:使用 Poedit 创建一个消息目录 ( File->New Catalog
)。仔细应用所有设置。Poedit 甚至可以提取字符串,这里可能不需要手动操作,只要看看Catalog 属性中的Sources 路径和Sources 关键字即可。
然后翻译所有(提取或手动添加的)字符串。Pyramid 将期望以下目录结构
myproject/
locale/
en/
LC_MESSAGES/
myproject.po
myproject.mo
myproject.pot
该pot
文件是您的目录。该en
文件夹用于翻译成英文。对于此处设置的任何其他语言:de
德语、es
西班牙语等。您可以将.po
文件创建为空并让 Poedit 从.pot
文件中更新。
完成翻译后,请使用 Pyramid 的有关使用这些文件的文档。如果您可以通过其他方式提取消息,则不需要 Babel。
作为旁注:您用于翻译过程的所有内容都围绕 Python 的 gettext 进行,因此如果您遇到任何问题,您甚至可以使用该库。如果您有有效的目录,这将在任何情况下都有效。