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 | ||
11 | @staticmethod | |
288c0ffb | 12 | def build_mo(tgt, lng_dir_code, appname): |
8ee66edd FC |
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/' | |
58c1093d | 16 | cmd = 'msgfmt -o {lng_dir}{appname}.mo assets/locale/po/{lng_code}.po' |
8ee66edd FC |
17 | system(cmd.format(lng_dir=lng_dir, appname=appname, lng_code=lng_code)) |
18 | ||
19 | @staticmethod | |
288c0ffb | 20 | def build_pot(appname, pot_path): |
8ee66edd | 21 | '''Builds the pot file in the lng_dir_code directory.''' |
d3ef798c FC |
22 | find_info = FindFileNamesInfo(['py']) |
23 | src_files = ' '.join(find_file_names(find_info)) | |
8ee66edd FC |
24 | cmd_tmpl = 'xgettext -ci18n -d {appname} -L python ' + \ |
25 | '-o {pot_path}{appname}.pot ' | |
26 | system(cmd_tmpl.format(appname=appname, pot_path=pot_path) + src_files) | |
27 | ||
28 | @staticmethod | |
29 | def merge(lng_code, tgt_path, lng_dir, appname): | |
30 | '''Merges the new strings with the previous ones.''' | |
31 | lng_base_dir = LanguageBuilder.__prepare(lng_dir, lng_code, appname) | |
32 | LanguageBuilder.__merge(lng_base_dir, lng_code, appname, tgt_path) | |
33 | LanguageBuilder.__postprocess(lng_code) | |
34 | ||
35 | @staticmethod | |
36 | def __prepare(lng_base_dir, lng, appname): | |
37 | '''Prepares a directory for working with languages.''' | |
38 | makedirs(lng_base_dir + lng + '/LC_MESSAGES', exist_ok=True) | |
39 | lng_dir = lng_base_dir + lng + '/LC_MESSAGES/' | |
58c1093d | 40 | if not exists('assets/locale/po/' + lng + '.po'): |
8ee66edd | 41 | lines_to_fix = ['CHARSET/UTF-8', 'ENCODING/8bit'] |
e65a09cf FC |
42 | [LanguageBuilder.__fix_line(line, lng_dir, appname) |
43 | for line in lines_to_fix] | |
8ee66edd FC |
44 | copy(lng_dir + appname + '.pot', lng_dir + appname + '.po') |
45 | return lng_dir | |
46 | ||
47 | @staticmethod | |
48 | def __fix_line(line, lng_dir, appname): | |
49 | '''Fixes po files (misaligned entries).''' | |
50 | cmd_tmpl = "sed 's/{line}/' {lng_dir}{appname}.pot > " + \ | |
51 | "{lng_dir}{appname}tmp.po" | |
52 | system(cmd_tmpl.format(line=line, lng_dir=lng_dir, appname=appname)) | |
53 | move(lng_dir + appname + 'tmp.po', lng_dir + appname + '.pot') | |
54 | ||
55 | @staticmethod | |
56 | def __merge(lng_dir, lng_code, appname, tgt_path): | |
57 | '''Manages the msgmerge's invokation.''' | |
58 | print('merge', lng_dir) | |
59 | cmd = 'msgmerge -o {lng_dir}{appname}merge.po ' + \ | |
60 | '{tgt_path}{lng_code}.po {tgt_path}{appname}.pot' | |
61 | cmd = cmd.format(lng_dir=lng_dir, lng_code=lng_code, appname=appname, | |
62 | tgt_path=tgt_path) | |
63 | system(cmd) | |
e65a09cf FC |
64 | copy(lng_dir + appname + 'merge.po', |
65 | 'assets/locale/po/%s.po' % lng_code) | |
8ee66edd FC |
66 | poname_tmpl = '{lng_dir}{appname}merge.po' |
67 | remove(poname_tmpl.format(lng_dir=lng_dir, appname=appname)) | |
68 | ||
69 | @staticmethod | |
70 | def __postprocess(lng_code): | |
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) |