| 1 |
|
|---|
| 2 |
"""build_mo |
|---|
| 3 |
|
|---|
| 4 |
Generate .mo files from po files. |
|---|
| 5 |
""" |
|---|
| 6 |
|
|---|
| 7 |
from distutils.core import Command |
|---|
| 8 |
from distutils.dep_util import newer |
|---|
| 9 |
from distutils.command.build import build as _build |
|---|
| 10 |
import os.path |
|---|
| 11 |
import msgfmt |
|---|
| 12 |
|
|---|
| 13 |
class build(_build): |
|---|
| 14 |
|
|---|
| 15 |
def initialize_options(self): |
|---|
| 16 |
_build.initialize_options(self) |
|---|
| 17 |
self.build_locales = None |
|---|
| 18 |
|
|---|
| 19 |
def finalize_options(self): |
|---|
| 20 |
_build.finalize_options(self) |
|---|
| 21 |
if not self.build_locales: |
|---|
| 22 |
self.build_locales = os.path.join(self.build_base, 'locale') |
|---|
| 23 |
|
|---|
| 24 |
build.sub_commands.append(('build_mo', None)) |
|---|
| 25 |
|
|---|
| 26 |
class build_mo(Command): |
|---|
| 27 |
|
|---|
| 28 |
description = 'Create .mo files from .po files' |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
user_options = [('build-dir=', None, |
|---|
| 33 |
'Directory to build locale files'), |
|---|
| 34 |
('force', 'f', 'Force creation of .mo files'), |
|---|
| 35 |
] |
|---|
| 36 |
|
|---|
| 37 |
boolean_options = ['force'] |
|---|
| 38 |
|
|---|
| 39 |
def initialize_options (self): |
|---|
| 40 |
self.build_dir = None |
|---|
| 41 |
self.force = None |
|---|
| 42 |
self.all_linguas = None |
|---|
| 43 |
|
|---|
| 44 |
def finalize_options (self): |
|---|
| 45 |
self.set_undefined_options('build', |
|---|
| 46 |
('build_locales', 'build_dir'), |
|---|
| 47 |
('force', 'force')) |
|---|
| 48 |
try: |
|---|
| 49 |
self.all_linguas = self.distribution.get_all_linguas() |
|---|
| 50 |
except: |
|---|
| 51 |
pass |
|---|
| 52 |
|
|---|
| 53 |
def run (self): |
|---|
| 54 |
"""Run msgfmt.make() on all_linguas.""" |
|---|
| 55 |
if not self.all_linguas: |
|---|
| 56 |
return |
|---|
| 57 |
|
|---|
| 58 |
self.mkpath(self.build_dir) |
|---|
| 59 |
for lingua in self.all_linguas: |
|---|
| 60 |
pofile = os.path.join('po', lingua + '.po') |
|---|
| 61 |
outfile = os.path.join(self.build_dir, lingua + '.mo') |
|---|
| 62 |
if self.force or newer(pofile, outfile): |
|---|
| 63 |
print 'converting %s -> %s' % (pofile, outfile) |
|---|
| 64 |
msgfmt.make(pofile, outfile) |
|---|
| 65 |
else: |
|---|
| 66 |
print 'not converting %s (output up-to-date)' % pofile |
|---|
| 67 |
|
|---|