Babel:在调用setup.py install时编译翻译文件 [英] Babel: compile translation files when calling setup.py install

查看:90
本文介绍了Babel:在调用setup.py install时编译翻译文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Babel开发Flask应用程序.感谢 Distutils/Setuptools Integration ,所有参数compile/extract/. .函数存储在setup.cfg中,并且编译i18n文件就像

I'm developing a Flask application using Babel. Thanks to Distutils/Setuptools Integration, all the parameters of compile/extract/... functions are stored in setup.cfg and compiling the i18n files is as easy as

./setup.py compile_catalog

太好了.现在,我希望在运行

Great. Now I would like this to be done automatically when running

./setup.py install

make的话来说,这会让install目标取决于compile_catalog目标.

In make's words, that would be letting install target depend on compile_catalog target.

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

We store only translation (.po) files in the code repository. .gitignore excludes .mo and .pot files from being tracked.

当开发人员提取代码的新修订版时,他将运行

When a developer pulls a new revision of the code, he runs

pip install -r requirements.txt

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

to update dependencies and install the project in development mode. Then, using above command line, he compiles the translation binary (.mo) files.

是否存在一种简单且推荐的方法来修改setup.py以一步完成两个操作?还是我想滥用setuptools?

Is there a simple and recommended way to modify setup.py to do both operations in one step? Or am I trying to misuse setuptools?

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

Using a script that like this would work for development purposes:

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

但是我希望有一种解决方案,当按照通常的setup.py安装说明安装软件包时,也可以使用该解决方案,例如从PyPi安装.

but I would like a solution that also works when the package is installed with usual setup.py install instructions, like if installed from PyPi.

我是否应该理解setuptools并不是要这样使用,并且分发软件的人们在创建归档文件时可以手动或使用自定义脚本来编译其翻译文件,而不是依赖setup.py来在安装时对其进行编译时间?

Should I understand that setuptools are not meant to be used like this, and people distributing software compile their translation files either manually or using custom scripts when creating their archives, rather than relying on setup.py to compile them at installation time?

我在互联网上找不到很多解决此问题的帖子.我发现这些问题涉及从setup.py中的功能运行pybabel命令行界面,这听起来很可惜,因为它错过了setuptools集成的重点.

I didn't find many posts on the Internet addressing this. The ones I found involved running pybabel command line interface from a function in setup.py, which sounds like a shame as it misses the point of setuptools integration.

推荐答案

我认为您的要求是完全有效的,而且我很惊讶似乎没有关于如何实现此目标的官方指南.

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:

  • setup.cfg中,进行适当的输入,以使compile_catalog可以不带选项地运行.

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

setup.py中,子类化来自setuptools的安装命令:

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

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

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

由于在安装过程中使用了babel,您应该将其添加为安装依赖项:

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.

这篇关于Babel:在调用setup.py install时编译翻译文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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