-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (127 loc) · 4.9 KB
/
main.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
144
145
146
import gc
from os import (
getcwd,
makedirs,
path
)
import sys
from time import sleep
from google.cloud import translate
from google.api_core.exceptions import BadRequest, Forbidden
import polib
from parser import Translatable
from list_of_translateables import ListOfTranslateAbles
from common_util import (
chunks,
extract_files_and_translate_folder,
validate_and_translate_file
)
reload(sys)
sys.setdefaultencoding('utf8')
# Instantiates a client
translate_client = translate.Client()
RATE_LIMIT_WAITING = 101
def translate_file(**params):
"""
It uses google api to translate whole .po file
Args:
languages (list): Language code list i.e en for english
source_file_path (str): The source folder containing .po files
destination_folder (str): Destination folder path. I hard coded it to dest folder
destination_name (str): Destination file name path, same as source file
"""
source_file_path = params.get('file_path', None)
destination_folder = params.get('dest_path', None)
destination_name = params.get('file_path', None)
languages = params.get('languages', [])
if not translate_client:
return
translate_ables = ListOfTranslateAbles()
po = polib.pofile(source_file_path, encoding='UTF-8')
for entry in po:
translate_ables.append(Translatable(entry.msgid, entry.occurrences))
for lang in languages:
print("Translating in language: {}".format(lang))
lang_destination_folder = path.join(destination_folder, lang, 'LC_MESSAGES')
if not path.isdir(lang_destination_folder):
makedirs(lang_destination_folder)
destination_file = path.join(lang_destination_folder, destination_name)
po_dest = polib.POFile()
po_dest.metadata = {
'Project-Id-Version': '1.0',
'Report-Msgid-Bugs-To': 'amir.qayyum.khan@gmail.com',
'MIME-Version': '1.0',
'Content-Type': 'text/plain; charset=utf-8',
'Content-Transfer-Encoding': '8bit',
'Language': lang
}
# check_count = 0
for array in chunks(translate_ables.keys()):
try:
translations = translate_client.translate(
array,
target_language=lang.strip()
)
except (BadRequest, Forbidden) as ex:
print(
"Waiting for a {} seconds after exception: {}".format(
RATE_LIMIT_WAITING,
ex.message
)
)
sleep(RATE_LIMIT_WAITING)
translations = translate_client.translate(
array,
target_language=lang.strip()
)
for translation in translations:
input_entered = translation.get("input")
translate_able = translate_ables.get_translate_able(input_entered)
entry = polib.POEntry(
msgid=translate_able.original_msgid.strip(),
msgstr=translate_able.construct_translated_msgid(
translation.get('translatedText').strip()
),
occurrences=translate_able.occurrences
)
po_dest.append(entry)
# check_count += 1
# print for debugging purposes. This is to check if data chunk translated.
# This is to avoid rate limit errors
# print "Chunk count: {}".format(check_count)
po_dest.save(destination_file)
# compile it to an mo file
file_base_name, __ = path.splitext(destination_name)
destination_mo_file = path.join(lang_destination_folder, "{}.mo".format(file_base_name))
po_dest.save_as_mofile(destination_mo_file)
gc.collect()
if __name__ == '__main__':
file_or_folder = raw_input("Please input file or folder full path: ")
languages = raw_input("Please input target language(s) comma separated i.e en for english: ")
if not file_or_folder:
sys.exit(0)
file_or_folder = str(file_or_folder)
languages = str(languages)
if "," in languages:
languages = languages.split(",")
else:
languages = [languages]
# static destination folder pointed to dest folder in this code.
# you can change it anytime.
destination_folder_path = path.join(getcwd(), 'dest')
if path.isfile(file_or_folder):
validate_and_translate_file(
languages=languages,
file_path=file_or_folder,
dest_path=destination_folder_path,
action=translate_file
)
elif path.isdir(file_or_folder):
extract_files_and_translate_folder(
languages=languages,
folder_path=file_or_folder,
dest_path=destination_folder_path,
action=translate_file
)
else:
sys.exit(-1)