如何在Django应用的setup.py中编译gettext翻译
我想知道怎么才能确保 setup.py
在创建 sdist
的时候,能自动编译项目的 PO 文件并把它们包含进去。这是一个 Django 应用,手动生成 MO 文件的步骤是要在应用的根目录下运行以下命令:
django-admin compilemessages
(这意味着要比 setup.py
深一层目录)
我希望能避免每次都手动编译 MO 文件。而且我根本不想把它们存储在代码库里。
2 个回答
6
我简单的解决方案(从Trac那里得了一些灵感):
#!/usr/bin/env python
from setuptools import setup, find_packages
from setuptools.command.install_lib import install_lib as _install_lib
from distutils.command.build import build as _build
from distutils.cmd import Command
class compile_translations(Command):
description = 'compile message catalogs to MO files via django compilemessages'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import os
import sys
from django.core.management.commands.compilemessages import \
compile_messages
curdir = os.getcwd()
os.chdir(os.path.realpath('app_name'))
compile_messages(stderr=sys.stderr)
os.chdir(curdir)
class build(_build):
sub_commands = [('compile_translations', None)] + _build.sub_commands
class install_lib(_install_lib):
def run(self):
self.run_command('compile_translations')
_install_lib.run(self)
setup(name='app',
packages=find_packages(),
include_package_data=True,
setup_requires=['django'],
...
cmdclass={'build': build, 'install_lib': install_lib,
'compile_translations': compile_translations}
)
这个方法可以帮助你在构建egg或安装包的时候编译po文件。
3
from django.core.management.commands.compilemessages import compile_messages
在你运行 setup
之前,先在你的 setup.py
脚本中使用它,然后把生成的文件包含在 setup
方法里。