-
Notifications
You must be signed in to change notification settings - Fork 0
/
glossary_maker.py
158 lines (134 loc) · 6.49 KB
/
glossary_maker.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
147
148
149
150
151
152
153
154
155
156
157
158
import csv
import math
import os
from os import path
import copy
from googletrans import Translator
from httpcore import SyncHTTPProxy
Special_Marks_in_XAML_Content = ['&', '<', '>', '\r', '\n']
Special_Characters_in_XAML_Content = ['&', '<', '>', ' ', ' ']
Forbidden_Characters_in_XAML_Key = ['"', "'", *Special_Marks_in_XAML_Content]
Forbidden_Characters_in_XAML_Key_Values = ['"', ''', *Special_Characters_in_XAML_Content]
def Special_Marks_to_Characters_in_XAML(string: str) -> str:
for i in range(len(Special_Marks_in_XAML_Content)):
string = string.replace(Special_Characters_in_XAML_Content[i], Special_Marks_in_XAML_Content[i])
return string
def Characters_to_Special_Marks_in_XAML(string: str) -> str:
for i in range(len(Special_Marks_in_XAML_Content)):
string = string.replace(Special_Marks_in_XAML_Content[i], Special_Characters_in_XAML_Content[i])
return string
def Forbidden_Characters_in_XAML_Key_convert(string: str) -> str:
for i in range(len(Forbidden_Characters_in_XAML_Key)):
string = string.replace(Forbidden_Characters_in_XAML_Key[i], Forbidden_Characters_in_XAML_Key_Values[i])
return string
class glossary:
def __init__(self):
self.keys = []
self.english_words = []
self.columns = []
self.ROW_TITLE = 0
self.ROW_LANG_CODE_ISO = 1
self.ROW_LANG_CODE_GOOGLE = 2
self.ROW_LANG_NAME = 3
def load_csv(self, csv_file_name: str, encoding: str = 'utf-8'):
lines = [] # [[key, english_word, a, b, c, d], [key1, w1, ...], ...]
with open(csv_file_name, mode='r', encoding=encoding)as f:
reader = csv.reader(f)
for row in reader:
lines.append(row)
key_column_index = 0
en_column_index = 1
self.keys = [Forbidden_Characters_in_XAML_Key_convert(line[key_column_index]) for line in lines]
self.english_words = [Special_Marks_to_Characters_in_XAML(line[en_column_index]) for line in lines]
for col in range(len(lines[0])):
if col == key_column_index or col == en_column_index:
continue
self.columns.append([Special_Marks_to_Characters_in_XAML(line[col]) for line in lines])
def save_csv(self, csv_file_name: str, encoding: str = 'utf-8'):
lines = []
for row in range(len(self.keys)):
line = [column[row] for column in self.columns]
lines.append([self.keys[row], self.english_words[row], *line])
with open(csv_file_name, 'w', encoding=encoding, newline='') as f:
writer = csv.writer(f)
writer.writerows(lines)
def do_translate(self, translator: Translator):
for column in self.columns:
print('processing ', column[0])
for row in range(len(column)):
if row <= self.ROW_LANG_NAME:
continue
# 没有内容时才进行翻译
if column[row] == '':
print('translating to [', column[0], ']: ', self.english_words[row])
txt = translator.translate(Special_Marks_to_Characters_in_XAML(self.english_words[row]), dest=column[self.ROW_LANG_CODE_GOOGLE]).text
column[row] = txt
def merge_columns(self, src):
self_languages = [column[self.ROW_TITLE] for column in self.columns]
src_languages = [column[self.ROW_TITLE] for column in src.columns]
for src_col in range(len(src.columns)):
if src_languages[src_col] not in self_languages:
continue
self_col = self_languages.index(src_languages[src_col])
for src_row in range(len(src.columns[src_col])):
if src.columns[src_col][src_row] == '':
continue
if src.keys[src_row] not in self.keys:
continue
self_row = self.keys.index(src.keys[src_row])
if self.columns[self_col][self_row] == '':
self.columns[self_col][self_row] = src.columns[src_col][src_row]
def clear_content_rows(self):
for column in self.columns:
for row in range(len(column)):
if row > self.ROW_LANG_NAME:
column[row] = ""
def save_to_xaml(self, encoding: str = 'utf-8'):
langs = [self.english_words, *self.columns]
file_list = []
for lang in langs:
code = lang[self.ROW_LANG_CODE_ISO]
name = lang[self.ROW_LANG_NAME]
xaml_file_name = (code + '.xaml').lower()
with open(xaml_file_name, 'w', encoding=encoding, newline='') as f:
f.write('<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">\r\n')
if "language_name" not in self.keys:
f.write(' <s:String x:Key="language_name">' + name + '</s:String>\r\n')
for row in range(len(self.keys)):
f.write(' <s:String x:Key="' + self.keys[row] + '">' + Characters_to_Special_Marks_in_XAML(lang[row]) + '</s:String>\r\n')
f.write('</ResourceDictionary>')
file_list.append(xaml_file_name)
pass
with open("LanguagesList.cs", 'w', encoding=encoding, newline='') as f:
f.write('''
public static class LanguagesResources
{
public static readonly string[] Files = new string[]
{
#REPLACEMENT#
};
}
'''.replace('#REPLACEMENT#', ' "' + '",\r\n "'.join(file_list) + '"'))
def clone(self):
return copy.deepcopy(self)
if __name__ == '__main__':
CSV_FILE_NAME = 'glossary_for_PRemoteM.csv'
CSV_FILE_NAME = os.path.basename(CSV_FILE_NAME).split('.')[0]
g = glossary()
g.load_csv(CSV_FILE_NAME + '.csv')
g.save_csv(CSV_FILE_NAME + '.csv')
http_proxy = SyncHTTPProxy((b'http', b'127.0.0.1', 1080, b''))
proxies = {'http': http_proxy, 'https': http_proxy}
translator = Translator(proxies=proxies)
# print(translator.translate('hi\r\nsun rise', dest='de').text)
# translate
t = g.clone()
if path.exists(CSV_FILE_NAME + '_translated_by_google.csv'):
google_translated = glossary()
google_translated.load_csv(CSV_FILE_NAME + '_translated_by_google.csv')
t.merge_columns(google_translated)
t.do_translate(translator)
t.save_csv(CSV_FILE_NAME + '_translated_by_google.csv')
g.merge_columns(t)
g.save_to_xaml()
# save xaml