Babel: compile translation files when calling setup.py install

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

 ./setup.py compile_catalog 

Great. Now I would like this to be done automatically at startup

 ./setup.py install 

In the words make that allowed the install target to depend on compile_catalog target.

Context

We only save translation files ( .po ) in the code repository. .gitignore excludes monitored .mo and .pot .

When a developer pulls out a new version of code, he runs

 pip install -r requirements.txt 

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

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

Using a script, how this will work for development purposes:

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

but I would like to get a solution that also works when the package is installed with the usual installation.py installation instructions, for example, if it is installed from PyPi.

Do I have to understand that setuptools not intended to be used in this way, and people who distribute the software will compile their translation files manually or using their own scripts when creating their archives, instead of relying on setup.py to compile them during installation time ?

I have not found many posts on the Internet about this. I found that I used the pybabel command line pybabel from the function in setup.py , which seems like a shame as it skips the setuptools integration point.

+5
source share
1 answer

I think your requirement is fully justified, and I am very surprised that there seems to be no official guide on how to do this.

The project that I was working on now also became multilingual, and I did this:

  • In setup.cfg enter the appropriate entries so that compile_catalog can be run without parameters.

  • In setup.py subclass installs the setuptools command:

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 * .mo files are included in the package:

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

Since babel is used during setup, you must add it as an installation dependent:

 setup_requires=[ 'babel', ], 

Note that the package (here, babel) displayed in both setup_requires and install_requires will not install correctly using python setup.py install due to issue in setuptools , but it works fine with pip install .

+5
source

Source: https://habr.com/ru/post/1258228/


All Articles