在python包中包含本地化的正确方法是什么? [英] What is the correct way to include localisation in python packages?

查看:155
本文介绍了在python包中包含本地化的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的python应用程序,并且想知道将本地化包含在源代码发行版中的正确方法是什么.我在 setuptools 的文档中苦苦挣扎;本地化甚至没有提到.我使用 pypabel 提取我的消息目录并进行编译.

I am writing my own python application and I am wondering what is the correct way to include localisation in source distributions. I struggled with the documentation of setuptools; localisation is not even mentioned there. I use pypabel to extract my message catalogues and to compile them.

问题

  • 是否可以在使用setup.py创建源软件包之前将*.po编译为*.mo?目前,我必须先针对每种语言手动编译所有内容...
  • 我应该在哪里包括这些*.mo文件?几个linux发行版中这些文件的位置不同.我认为我在setup.py

  • Is there a possibility to compile *.po to *.mo automatically before creating a source package with setup.py? Currently I have to compile everything before manually for each language...
  • Where should I include those *.mo-files? Several linux distributions have different places for those files. In my opinion I line like this in setup.py

data_files=[('share/locale/de/LC_MESSAGES', ['locale/de/LC_MESSAGES/project.mo'])

完全没有道理.使用pip安装时,它可能在特定的Linux机器上运行.但是它可能会在bsd或Windows上崩溃...

does not really make sense. It may work on a particular linux machine when installing with pip. But it may break on bsd or windows...

推荐答案

首先:有一些用于处理Python中po文件的工具,建议您研究其他命令setup.py .

For the first: there are tools for handling po-files in Python, suggest you look into Babel, then you get the extra commands for setup.py.

应该也可以将compile_catalogs挂钩为源代码构建的必要条件,但是我没有发现需要它的原因,因为如果您不这样做,则无法测试与python setup.py develop链接的程序包中的消息t首先编译.另外,如果您需要Babel,也许您的源代码版本根本不需要.mo.

It should be possible to also hook the compile_catalogs as requisite for source build, but I haven't found need for it, as you can't test the messages from a package linked with python setup.py develop if you don't compile first. Also, maybe your source build does not need the .mo at all if you require Babel.

第二个:我们的应用程序使用金字塔Web框架,约定使用package_data:

For the second: Our apps use the pyramid web framework, the convention there is to use package_data:

setup(
    # ...
    package_data={'yourpackage': ['i18n/*/LC_MESSAGES/*.mo' ]},
    # ...

然后,如果您的本地化位于yourpackage/i18n/中,则可以使用pkg_resources查找路径,例如:

then if your localizations are in yourpackage/i18n/ you can use pkg_resources to find the path, for example:

pkg_resources.resource_filename('yourpackage', 'i18n')

将指向翻译的位置.

这篇关于在python包中包含本地化的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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