在python包中包含本地化的正确方法是什么?

时间:2023-01-22 10:50:35

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.

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

Questions

  • 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...
  • 在使用setup.py创建源包之前,是否有可能自动将* .po编译为* .mo?目前我必须手动为每种语言编译所有内容...

  • 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

    我应该在哪里包含那些* .mo文件?几个Linux发行版对这些文件有不同的位置。在我看来,我在setup.py中这样排列

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

    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...

    没有多大意义。当使用pip安装时,它可能在特定的Linux机器上工作。但它可能会破坏bsd或windows ...

1 个解决方案

#1


1  

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.

对于第一个:有用于处理Python中的po文件的工具,建议你查看Babel,然后你获得了setup.py的额外命令。

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.

应该可以将compile_catalogs挂钩作为源代码构建的必备条件,但是我还没有找到它的需要,因为如果你不先编译就不能测试与python setup.py develop链接的包中的消息。此外,如果您需要Babel,也许您的源代码构建根本不需要.mo。

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

对于第二个:我们的应用程序使用金字塔Web框架,惯例是使用package_data:

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

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

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

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

will point to the location of translations.

将指向翻译的位置。

#1


1  

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.

对于第一个:有用于处理Python中的po文件的工具,建议你查看Babel,然后你获得了setup.py的额外命令。

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.

应该可以将compile_catalogs挂钩作为源代码构建的必备条件,但是我还没有找到它的需要,因为如果你不先编译就不能测试与python setup.py develop链接的包中的消息。此外,如果您需要Babel,也许您的源代码构建根本不需要.mo。

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

对于第二个:我们的应用程序使用金字塔Web框架,惯例是使用package_data:

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

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

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

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

will point to the location of translations.

将指向翻译的位置。