What is the correct way to include localisation in python packages?

落爺英雄遲暮 提交于 2019-12-31 03:08:07

问题


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.

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...
  • 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'])
    

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


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

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.

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' ]},
    # ...

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')

will point to the location of translations.



来源:https://stackoverflow.com/questions/22958245/what-is-the-correct-way-to-include-localisation-in-python-packages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!