Babel: compile translation files when calling setup.py install

我们两清 提交于 2019-12-03 14:35:00
roman

I think your demand is totally valid and I'm quite surprised that there seems to be no official guideline on how to accomplish this.

The project I working on now also went multilingual and this is what I did:

  • In setup.cfg, make appropriate entries so that compile_catalog can be run without options.

  • In setup.py, subclass the install command from setuptools:

setup.py:

from setuptools import setup
from setuptools.command.install import install

class InstallWithCompile(install):
    def run(self):
        from babel.messages.frontend import compile_catalog
        compiler = compile_catalog(self.distribution)
        option_dict = self.distribution.get_option_dict('compile_catalog')
        compiler.domain = [option_dict['domain'][1]]
        compiler.directory = option_dict['directory'][1]
        compiler.run()
        super().run()

Then, when calling setup(), register our InstallWithCompile command with the name "install" and make sure that the *.mo files will be included in the package:

setup(
    ...
    cmdclass={
        'install': InstallWithCompile,
    },
    ...
    package_data={'': ['locale/*/*/*.mo', 'locale/*/*/*.po']},
)

Since babel is used during the setup, you should add it as a setup dependency:

setup_requires=[
    'babel',
],

Note that a package (here, babel) appearing in both setup_requires and install_requires won't be installed correctly using python setup.py install due to an issue in setuptools, but it works fine with pip install.

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