ya2 · news · projects · code · about

first commit
[pmachines.git] / lib / build / lang.py
1 '''Tools for l10n.'''
2 from os import system, makedirs, remove
3 from os.path import exists
4 from shutil import move, copy
5 from lib.build.build import files
6
7
8 class LanguageBuilder:
9 '''Tools for building files for l10n.'''
10
11 @staticmethod
12 def mo(tgt, lng_dir_code, appname):
13 '''Builds the mo file in the lng_dir_code directory.'''
14 lng_code = tgt[len(lng_dir_code):].split('/')[0]
15 lng_dir = lng_dir_code + lng_code + '/LC_MESSAGES/'
16 cmd = 'msgfmt -o {lng_dir}{appname}.mo assets/po/{lng_code}.po'
17 system(cmd.format(lng_dir=lng_dir, appname=appname, lng_code=lng_code))
18
19 @staticmethod
20 def pot(appname, pot_path):
21 '''Builds the pot file in the lng_dir_code directory.'''
22 src_files = ' '.join(files(['py']))
23 cmd_tmpl = 'xgettext -ci18n -d {appname} -L python ' + \
24 '-o {pot_path}{appname}.pot '
25 system(cmd_tmpl.format(appname=appname, pot_path=pot_path) + src_files)
26
27 @staticmethod
28 def merge(lng_code, tgt_path, lng_dir, appname):
29 '''Merges the new strings with the previous ones.'''
30 lng_base_dir = LanguageBuilder.__prepare(lng_dir, lng_code, appname)
31 LanguageBuilder.__merge(lng_base_dir, lng_code, appname, tgt_path)
32 LanguageBuilder.__postprocess(lng_code)
33
34 @staticmethod
35 def __prepare(lng_base_dir, lng, appname):
36 '''Prepares a directory for working with languages.'''
37 makedirs(lng_base_dir + lng + '/LC_MESSAGES', exist_ok=True)
38 lng_dir = lng_base_dir + lng + '/LC_MESSAGES/'
39 if not exists('assets/po/' + lng + '.po'):
40 lines_to_fix = ['CHARSET/UTF-8', 'ENCODING/8bit']
41 [LanguageBuilder.__fix_line(line, lng_dir, appname) for line in lines_to_fix]
42 copy(lng_dir + appname + '.pot', lng_dir + appname + '.po')
43 return lng_dir
44
45 @staticmethod
46 def __fix_line(line, lng_dir, appname):
47 '''Fixes po files (misaligned entries).'''
48 cmd_tmpl = "sed 's/{line}/' {lng_dir}{appname}.pot > " + \
49 "{lng_dir}{appname}tmp.po"
50 system(cmd_tmpl.format(line=line, lng_dir=lng_dir, appname=appname))
51 move(lng_dir + appname + 'tmp.po', lng_dir + appname + '.pot')
52
53 @staticmethod
54 def __merge(lng_dir, lng_code, appname, tgt_path):
55 '''Manages the msgmerge's invokation.'''
56 print('merge', lng_dir)
57 cmd = 'msgmerge -o {lng_dir}{appname}merge.po ' + \
58 '{tgt_path}{lng_code}.po {tgt_path}{appname}.pot'
59 cmd = cmd.format(lng_dir=lng_dir, lng_code=lng_code, appname=appname,
60 tgt_path=tgt_path)
61 system(cmd)
62 copy(lng_dir + appname + 'merge.po', 'assets/po/%s.po' % lng_code)
63 poname_tmpl = '{lng_dir}{appname}merge.po'
64 remove(poname_tmpl.format(lng_dir=lng_dir, appname=appname))
65
66 @staticmethod
67 def __postprocess(lng_code):
68 '''Fixes po files at the end of the building process.'''
69 lines = open('assets/po/%s.po' % lng_code, 'r').readlines()
70 with open('assets/po/%s.po' % lng_code, 'w') as outf:
71 for line in lines:
72 po_str = '"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\\n"\n'
73 outf.write(po_str if line.startswith(po_str[:20]) else line)