-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathTranslation.py
201 lines (153 loc) · 6.34 KB
/
Translation.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright (C) 2024 Loren Eteval <[email protected]>
#
# This file is part of Furious.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from Furious.Library import *
from Furious.Utility import *
from Furious.Externals import *
import os
import re
import copy
# import deepl
import logging
import argparse
import functools
import Furious
logging.basicConfig(
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
level=logging.INFO,
)
logging.raiseExceptions = False
logger = logging.getLogger('Translation')
@functools.lru_cache(None)
def getAppSourceCodePath(path):
# Walk through the directory tree
for dirpath, dirnames, filenames in os.walk(path):
# Check if __init__.py exists in the current directory
if '__init__.py' in filenames:
for filename in filenames:
yield os.path.join(dirpath, filename)
@functools.lru_cache(None)
def getMagicNameFromPath(path):
return os.path.relpath(path, ROOT_DIR).removesuffix('.py').replace(os.sep, '.')
@functools.lru_cache(None)
def getAppConstantsByName(name):
return getattr(Furious.Utility.Constants, name)
APPLICATION_SOURCE_CODE_PATH = getAppSourceCodePath(PACKAGE_DIR)
def main():
parser = argparse.ArgumentParser()
# parser.add_argument('-k', '--key', help='DeepL auth key', required=True)
parser.add_argument(
'-t', '--target', help='Target translation language', required=True
)
parser.add_argument(
'-i', '--ignore', action='store_true', help='If provided, ignore review value'
)
parser.add_argument('-p', '--proxy', help='Proxy server used in API')
args = parser.parse_args()
target, proxy = args.target, args.proxy
logger.info(f'target translation language: {target}')
logger.info(f'use proxy: {proxy}')
translation = copy.deepcopy(TRANSLATION)
for key in translation.keys():
# Reset source
translation[key]['source'] = []
for sourceCodePath in APPLICATION_SOURCE_CODE_PATH:
with open(sourceCodePath, 'r', encoding='utf-8') as file:
content = file.read()
magicName = getMagicNameFromPath(sourceCodePath)
pattern = r"(?<![a-zA-Z])_\(\s*f?\s*(?:'([^']*)'|\"([^\"]*)\")\s*\)"
matches = re.findall(pattern, content)
if matches:
match = [m[0] or m[1] for m in matches]
for source in match:
foundBraces = True
while foundBraces:
lBraceIndex = source.find('{')
rBraceIndex = source.find('}')
if lBraceIndex >= 0 and rBraceIndex >= 0:
parsed = source[lBraceIndex + 1 : rBraceIndex]
source = source.replace(
source[lBraceIndex : rBraceIndex + 1],
getAppConstantsByName(parsed),
)
else:
foundBraces = False
if source not in translation:
translation[source] = {'source': [magicName]}
else:
if magicName not in translation[source]['source']:
translation[source]['source'].append(magicName)
nonexist = []
for key in translation.keys():
if len(translation[key]['source']) == 0:
# No source, add to nonexist
nonexist.append(key)
for key in nonexist:
# Key with no source, remove
translation.pop(key, None)
# translator = deepl.Translator(args.key, send_platform_info=False, proxy=proxy)
for text in translation.keys():
# Remove redundant EN translation
translation[text].pop('EN', '')
targetText = translation[text].get(target, '')
isReviewed = translation[text].get('isReviewed', 'False')
if targetText and isReviewed == 'True' and not args.ignore:
# Translation already reviewed. Skip
logger.info(
f'skip reviewed translation: \'{text}\' --{target}--> \'{targetText}\''
)
else:
# result = translator.translate_text(
# text,
# source_lang='EN',
# target_lang=target,
# context=(
# f'\'{APPLICATION_NAME}\' is application name. '
# 'Please do not translate this word'
# ),
# )
#
# logger.info(
# f'query translation: \'{text}\' --{target}--> \'{result.text}\''
# )
#
# translation[text][target] = result.text
if not targetText or translation[text].get('isReviewed') is None:
# Target translation does not exist, or does not have 'isReviewed' field.
# Set 'isReviewed' field to "False"
translation[text]['isReviewed'] = 'False'
try:
# Write back to file
with open(GEN_TRANSLATION_FILE, 'w', encoding='utf-8') as file:
file.write(f'TRANSLATION = {UJSONEncoder.encode(translation, indent=4)}\n')
except Exception as ex:
# Any non-exit exceptions
logger.error(f'flush result to \'{GEN_TRANSLATION_FILE}\' failed. {ex}')
else:
logger.info(f'flush result to \'{GEN_TRANSLATION_FILE}\' success')
unreviewed = 0
for text in translation.keys():
isReviewed = translation[text].get('isReviewed', 'False')
if isReviewed == 'False':
unreviewed += 1
logger.warning(f'have unreviewed translation \'{text}\'')
if unreviewed > 0:
logger.error(f'have {unreviewed} unreviewed translation(s)')
else:
logger.info(f'all {len(translation)} translation(s) have been reviewed')
if __name__ == '__main__':
main()