-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (52 loc) · 2.49 KB
/
app.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
import os
import short_codes
import json
from googletrans import Translator
print("""
##############################################
# Welcome to Laravel Application Translation #
# Created by Douglas Hozea #
##############################################
""")
translator = Translator()
while True:
directory_location = input("Please enter location of lang folder in your project e.g "
"/var/www/html/project/resources/lang: \n")
if not os.path.isdir(directory_location + "/en"):
print("Wrong directory entered")
break
translate_to = input("Please enter the language short code to translate to: \n")
if not short_codes.is_short_code_valid(translate_to):
print("Language short code specified does not exist")
break
file_to_translate = input("Please enter the name of the file you wish to translate e.g file.php: \n")
if not os.path.isfile(directory_location + "/en/" + file_to_translate):
print("File name not found")
break
dict_from_en_file = json.loads(os.popen("php -r \"echo json_encode(include '" +
directory_location + "/en/" + file_to_translate +
"');\"").read())
number_of_translation_keys = len(dict_from_en_file)
current_key_being_translated = 1
if not os.path.isdir(directory_location + "/" + translate_to):
os.mkdir(directory_location + "/" + translate_to)
f = open(directory_location + "/" + translate_to + "/" + file_to_translate, "w+")
f.write("<?php \n\n")
f.write("return [\n")
for key, value in dict_from_en_file.items():
print(current_key_being_translated, "/", number_of_translation_keys)
try:
translated_value = translator.translate(value, dest=translate_to, src='en').text
# check if translated value is still destination lang and use lower case
if translated_value is None or translator.detect(translated_value).lang == 'en':
translated_value = translator.translate(value.lower(), dest=translate_to, src='en').text
print(key, "=>", translated_value)
f.write(" \"" + key + "\" => \"" + translated_value + "\", \n")
except:
print("Failed to translate:", key, "=>", value)
f.write(" \"" + key + "\" => \"" + value + "\", \n")
print("")
current_key_being_translated += 1
f.write("];")
f.close()
break