-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtemplatetranslator.py
63 lines (55 loc) · 1.76 KB
/
templatetranslator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Template Translator v0.2
# Regex-based automatic translator for common strings in gettext files.
#
# Copyright 2016-2017 roptat <julien@lepiller.eu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
import sys
import re
import polib
import os.path
class TemplateTranslator:
def __init__(self, files):
self.regexps = []
self.files = files
def append(self, regexp, translation):
self.regexps.append([regexp, translation])
def convert(self, entry, regexp, template):
m = regexp.match(entry.msgid)
# do not modify anything if the translation is already correct
#if m and ("fuzzy" in entry.flags or not entry.msgstr):
if m:
msgstr = template
g = m.groups()
for i in range(len(g)):
if g[i] is not None:
msgstr = msgstr.replace('#{}'.format(i + 1), g[i])
entry.msgstr = msgstr
if "fuzzy" in entry.flags:
entry.flags.remove("fuzzy")
def translate_one(self, filename, language):
realname = language + '/' + filename
if not os.path.exists(realname):
print(realname, 'does not exist.')
return
po = polib.pofile(realname, wrapwidth = 76)
for entry in po:
for reg in self.regexps:
if language in reg[1]:
self.convert(entry, reg[0], reg[1][language])
po.save()
def translate(self, languages):
number = len(self.files)
current = 1
for filename in self.files:
print('Traitement du fichier ', current, '/', number, ' \r',
end="", flush=True),
current = current + 1
for language in languages:
self.translate_one(filename, language)
print('')