Commit | Line | Data |
---|---|---|
8ee66edd FC |
1 | '''Tools for l10n.''' |
2 | from os import system, makedirs, remove | |
3 | from os.path import exists | |
4 | from shutil import move, copy | |
d3ef798c | 5 | from ya2.build.build import find_file_names, FindFileNamesInfo |
8ee66edd FC |
6 | |
7 | ||
8 | class LanguageBuilder: | |
9 | '''Tools for building files for l10n.''' | |
10 | ||
4e980d83 FC |
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): | |
8ee66edd | 17 | '''Builds the mo file in the lng_dir_code directory.''' |
4e980d83 FC |
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/' | |
58c1093d | 22 | cmd = 'msgfmt -o {lng_dir}{appname}.mo assets/locale/po/{lng_code}.po' |
4e980d83 | 23 | system(cmd.format(lng_dir=lng_dir, appname=self.__app_name, lng_code=lng_code)) |
8ee66edd | 24 | |
4e980d83 | 25 | def build_pot(self): |
8ee66edd | 26 | '''Builds the pot file in the lng_dir_code directory.''' |
d3ef798c FC |
27 | find_info = FindFileNamesInfo(['py']) |
28 | src_files = ' '.join(find_file_names(find_info)) | |
8ee66edd FC |
29 | cmd_tmpl = 'xgettext -ci18n -d {appname} -L python ' + \ |
30 | '-o {pot_path}{appname}.pot ' | |
4e980d83 | 31 | system(cmd_tmpl.format(appname=self.__app_name, pot_path=self.__po_path) + src_files) |
8ee66edd | 32 | |
4e980d83 | 33 | def merge(self, lng_code): |
8ee66edd | 34 | '''Merges the new strings with the previous ones.''' |
4e980d83 FC |
35 | lng_base_dir = self.__prepare(lng_code) |
36 | self.__merge(lng_base_dir, lng_code) | |
37 | self.__postprocess(lng_code) | |
8ee66edd | 38 | |
4e980d83 | 39 | def __prepare(self, lng_code): |
8ee66edd | 40 | '''Prepares a directory for working with languages.''' |
4e980d83 FC |
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'): | |
8ee66edd | 44 | lines_to_fix = ['CHARSET/UTF-8', 'ENCODING/8bit'] |
4e980d83 | 45 | [self.__fix_line(line, lng_dir) |
e65a09cf | 46 | for line in lines_to_fix] |
4e980d83 | 47 | copy(lng_dir + self.__app_name + '.pot', lng_dir + self.__app_name + '.po') |
8ee66edd FC |
48 | return lng_dir |
49 | ||
4e980d83 | 50 | def __fix_line(self, line, lng_dir): |
8ee66edd FC |
51 | '''Fixes po files (misaligned entries).''' |
52 | cmd_tmpl = "sed 's/{line}/' {lng_dir}{appname}.pot > " + \ | |
53 | "{lng_dir}{appname}tmp.po" | |
4e980d83 FC |
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') | |
8ee66edd | 56 | |
4e980d83 | 57 | def __merge(self, lng_dir, lng_code): |
8ee66edd FC |
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' | |
4e980d83 FC |
62 | cmd = cmd.format(lng_dir=lng_dir, lng_code=lng_code, appname=self.__app_name, |
63 | tgt_path=self.__po_path) | |
8ee66edd | 64 | system(cmd) |
4e980d83 | 65 | copy(lng_dir + self.__app_name + 'merge.po', |
e65a09cf | 66 | 'assets/locale/po/%s.po' % lng_code) |
8ee66edd | 67 | poname_tmpl = '{lng_dir}{appname}merge.po' |
4e980d83 | 68 | remove(poname_tmpl.format(lng_dir=lng_dir, appname=self.__app_name)) |
8ee66edd | 69 | |
4e980d83 | 70 | def __postprocess(self, lng_code): |
8ee66edd | 71 | '''Fixes po files at the end of the building process.''' |
58c1093d FC |
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: | |
8ee66edd FC |
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) |