ya2 · news · projects · code · about

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