【问题标题】:compile translation files when calling setup.py install调用 setup.py install 时编译翻译文件
【发布时间】:2016-10-14 19:57:50
【问题描述】:

我正在使用 Babel 开发一个 Flask 应用程序。感谢Distutils/Setuptools Integration,编译/提取/...函数的所有参数都存储在setup.cfg中,编译i18n文件就像

一样简单
./setup.py compile_catalog

太好了。现在我希望在运行时自动完成

./setup.py install

make 的话来说,就是让install 目标依赖于compile_catalog 目标。

上下文

我们在代码库中仅存储翻译 (.po) 文件。 .gitignore 排除 .mo.pot 文件被跟踪。

当开发人员拉取代码的新版本时,他会运行

pip install -r requirements.txt

更新依赖项并在开发模式下安装项目。然后,使用上面的命令行,他编译翻译二进制(.mo)文件。

是否有一种简单且推荐的方法来修改setup.py 以一步完成这两项操作?还是我试图滥用setuptools

使用这样的脚本可用于开发目的:

#!/bin/sh
./setup.py compile_catalog
pip install -r requirements.txt

但我想要一个在使用通常的 setup.py 安装说明安装软件包时也可以使用的解决方案,例如从 PyPi 安装时。

我是否应该理解setuptools 不是这样使用的,分发软件的人在创建档案时手动或使用自定义脚本编译他们的翻译文件,而不是依赖setup.py 在安装时编译它们时间?

我在 Internet 上没有找到很多解决此问题的帖子。我发现其中涉及从setup.py 中的函数运行pybabel 命令行界面,这听起来很可惜,因为它错过了setuptools 集成的要点。

【问题讨论】:

    标签: python setuptools python-babel


    【解决方案1】:

    我认为您的要求是完全有效的,我很惊讶似乎没有关于如何实现这一点的官方指南。

    我现在从事的项目也变成了多语言,这就是我所做的:

    • setup.cfg 中输入适当的条目,以便compile_catalog 可以在没有选项的情况下运行。

    • setup.py 中,子类化来自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()
    

    然后,在调用 setup() 时,使用名称“install”注册我们的 InstallWithCompile 命令,并确保 *.mo 文件将包含在包中:

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

    由于在设置过程中使用了 babel,因此您应该将其添加为设置依赖项:

    setup_requires=[
        'babel',
    ],
    

    请注意,由于setuptools 中的issue,使用python setup.py install 将无法正确安装出现在setup_requiresinstall_requires 中的包(此处为babel),但它与@987654333 一起工作正常@。

    【讨论】:

    • 谢谢。很抱歉反馈迟了。我应用了您的解决方案,效果很好。我编辑了您的答案以添加一些信息。
    猜你喜欢
    • 2016-01-24
    • 2016-03-08
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多