-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmo_compiler.py
66 lines (53 loc) · 1.81 KB
/
mo_compiler.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
from os import (
path,
remove,
)
import polib
import sys
from common_util import (
extract_files_and_translate_folder,
validate_and_translate_file
)
reload(sys)
sys.setdefaultencoding('utf8')
def compile_file(**params):
"""
It uses google api to translate whole .po file
Args:
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)
po = polib.pofile(source_file_path, encoding='UTF-8')
if not path.isdir(destination_folder):
print("Folder not found")
sys.exit(-1)
# compile it to an mo file
file_base_name, __ = path.splitext(destination_name)
destination_mo_file = path.join(destination_folder, "{}.mo".format(file_base_name))
if path.exists(destination_mo_file):
remove(destination_mo_file)
print("Compiling file ", destination_mo_file)
po.save_as_mofile(destination_mo_file)
if __name__ == '__main__':
file_or_folder = raw_input("Please input file or folder full path: ")
if not file_or_folder:
sys.exit(0)
file_or_folder = str(file_or_folder)
if path.isfile(file_or_folder):
validate_and_translate_file(
file_path=file_or_folder,
dest_path=path.dirname(file_or_folder),
action=compile_file
)
elif path.isdir(file_or_folder):
extract_files_and_translate_folder(
folder_path=file_or_folder,
dest_path=file_or_folder,
action=compile_file
)
else:
sys.exit(-1)