-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate.py
executable file
·143 lines (121 loc) · 5.44 KB
/
translate.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
"""
Helper file to manage translations for the Meerkat project
We have two types of translations, general and implementation specific
The general translations are extracted from the python, jijna2 and js files.
The implementation specific text that needs to be translated is stored in a csv file.
"""
from csv import DictReader
import argparse
import os
import shutil
import datetime
from babel.messages.pofile import read_po, write_po
from babel.messages.catalog import Catalog, Message
from babel._compat import BytesIO
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["update-po",
"initialise",
"insert-translations",
"compile"
],
help="Choose action" )
parser.add_argument("-l", type=str,
help="Two letter langauge code")
parser.add_argument("-f", type=str,
help="Country Folder")
def create_pot_file_csv(in_file, out_file):
"""
Create a .po file from csv
"""
with open(out_file, "wb") as out:
catalog = Catalog(project="Meerkat",
version="0.1",
copyright_holder="Meerkat Developers",
charset="utf-8")
with open(in_file, "r") as csv_file:
csv = DictReader(csv_file)
for line in csv:
message = line["text"] #Message(line["text"])
catalog.add(message, None, [(line["source"], 1)], context=None)
write_po(out, catalog)
def find_languages(folders):
""" Find the languages in the translations folders.
Langauges are stored as subfolders with a two letter langauge code
Args:
folders: folders to loo through
Returns:
langauges
"""
languages = []
for folder in folders:
subfolder = os.listdir(folder)
for potential_lang in subfolder:
if len(potential_lang) == 2:
languages.append(potential_lang)
return languages
def insert_translation(pofile, csv_file, language):
tmp_file = pofile + datetime.datetime.now().isoformat()
shutil.copy(pofile, tmp_file)
translation_dict = {}
with open(csv_file, encoding="UTF-8") as csv_f:
csv = DictReader(csv_f)
if language not in csv.fieldnames:
return
for line in csv:
if line["text"] and line[language]:
translation_dict[line["text"]] = line[language]
change = False
with open(tmp_file) as original:
catalog = read_po(original)
for message in catalog:
if message.id in translation_dict:
if message.id == "" or message.id != translation_dict[message.id]:
message.string = translation_dict[message.id]
change = True
if change:
tmp_object = BytesIO()
write_po(tmp_object, catalog)
with open(pofile, "w") as out:
out.write(tmp_object.getvalue().decode("utf8"))
else:
os.remove(tmp_file)
if __name__ == "__main__":
args = parser.parse_args()
if args.f:
implementation_dir = args.f
else:
implementation_dir = os.environ.get("COUNTRY_FOLDER", "country_config")
implementation_csv = implementation_dir + "/translation.csv"
if args.action == "update-po":
shutil.copy("../meerkat_api/meerkat_api/resources/reports.py",
"meerkat_frontend/reports.py")
os.system("pybabel extract -F babel.cfg -o messages.pot .")
os.system("pybabel update -i messages.pot -d translations")
os.remove("meerkat_frontend/reports.py")
tmp_file = implementation_dir + "/messages.pot"
create_pot_file_csv(implementation_csv, tmp_file)
os.system("pybabel update -i {} -d {}/translations".format(tmp_file, implementation_dir))
elif args.action == "insert-translations":
if args.l and len(args.l) == 2:
insert_translation("{}/translations/{}/LC_MESSAGES/messages.po".format(implementation_dir,
args.l),
implementation_csv,args.l)
else:
print("Need to specify a two letter language code")
elif args.action == "initialise":
if args.l and len(args.l) == 2:
#os.system("pybabel init -i messages.pot -d translations -l {}".format(args.l))
os.system("pybabel init -i messages.pot -d {}/translations -l {}".format(implementation_dir,
args.l))
else:
print("Need to specify a two letter language code")
elif args.action == "compile":
if os.path.exists("{}/translations".format(implementation_dir)):
for lang in find_languages(["translations", "{}/translations".format(implementation_dir)]):
os.system("msgcat --use-first -o meerkat_frontend/translations/{}/LC_MESSAGES/messages.po translations/{}/LC_MESSAGES/messages.po {}/translations/{}/LC_MESSAGES/messages.po".format(lang, lang, implementation_dir, lang))
os.system("pybabel compile -d meerkat_frontend/translations")
os.system("pybabel compile -d {}".format(
implementation_dir + "/translations"))
else:
print("No Language folder")