forked from spx01/gte2-translation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for BQU Quest translations (#3)
* Add bqu builder
- Loading branch information
Showing
7 changed files
with
143 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
ftbquests/ | ||
ftbquests-tl/ | ||
ftbquests-new/ | ||
out/ | ||
DefaultQuests.json | ||
bqu/ | ||
bqu-tl/ | ||
bqu-new/ | ||
out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import deepl | ||
import re | ||
import os | ||
|
||
source_path = "ftbquests" | ||
target_path = "ftbquests-tl-" + os.environ["TARGET_LANG_CODE"] | ||
|
||
auth_key = os.environ["DEEPL_AUTH_KEY"] | ||
|
||
# Language code can be known in https://www.deepl.com/docs-api/general/get-languages | ||
target_lang_code = os.environ["TARGET_LANG_CODE"] | ||
|
||
# In some area on this earth, you should set a proper api server site at first. | ||
server_url = "https://api-free.deepl.com" | ||
|
||
tl = translator = deepl.Translator(auth_key, server_url=server_url) | ||
|
||
# this matches every "" sequence that contains a non-ascii unicode character | ||
# also accounts for escaped \" groups inside the double quotes | ||
match_jap = r'"((?:\\.|[^\\"\n\r])*)[\u0080-\uffff]((?:\\.|[^\\"\n\r])*)"' | ||
|
||
|
||
# replace matches of the r regex in s with f(match) | ||
def regex_replace(r, s, f): | ||
new = "" | ||
start = 0 | ||
for m in re.finditer(r, s): | ||
end, newstart = m.span() | ||
new += s[start:end] | ||
new += f(m.group()) | ||
start = newstart | ||
new += s[start:] | ||
return new | ||
|
||
|
||
def copy_structure(src, dst): | ||
for dirpath, dirnames, _ in os.walk(src): | ||
for d in dirnames: | ||
os.makedirs( | ||
os.path.join(dst, *dirpath.split(os.path.sep)[1:], d), | ||
exist_ok=True, | ||
) | ||
|
||
|
||
def translate(s): | ||
# un-escape the string so that it doesnt confuse deepl | ||
res = s[1:-1].replace('\\"', '"') | ||
res = tl.translate_text( | ||
res, source_lang="JA", target_lang=target_lang_code , preserve_formatting=True | ||
).text | ||
return '"' + res.replace('"', '\\"') + '"' | ||
|
||
|
||
def create_file_set(path, f=lambda s: s.endswith(".snbt")): | ||
res = set() | ||
for dirpath, _, filenames in os.walk(path): | ||
for filename in filenames: | ||
if not f(filename): | ||
continue | ||
file = os.path.join(dirpath, filename) | ||
if os.path.isfile(file): | ||
res.add(os.path.join(*file.split(os.path.sep)[1:])) | ||
return res | ||
|
||
|
||
def main(): | ||
copy_structure(source_path, target_path) | ||
ofiles = create_file_set(source_path) | ||
tlfiles = create_file_set(target_path) | ||
to_translate = ofiles - tlfiles | ||
if not to_translate: | ||
print("translation seems up to date") | ||
return | ||
for fp in to_translate: | ||
with open(os.path.join(source_path, fp), "r", encoding='utf-8') as f: | ||
s = f.read() | ||
print(f"translating {fp}...") | ||
translated = regex_replace(match_jap, s, translate) | ||
with open(os.path.join(target_path, fp), "x", encoding='utf-8') as tf: | ||
tf.write(translated) | ||
print("done") | ||
|
||
|
||
main() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
deepl | ||
deepl==1.18.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,44 @@ | ||
import deepl | ||
import re | ||
import os | ||
|
||
source_path = "ftbquests" | ||
target_path = "ftbquests-tl-" + os.environ["TARGET_LANG_CODE"] | ||
|
||
auth_key = os.environ["DEEPL_AUTH_KEY"] | ||
import json | ||
import deepl | ||
|
||
# Language code can be known in https://www.deepl.com/docs-api/general/get-languages | ||
target_lang_code = os.environ["TARGET_LANG_CODE"] | ||
|
||
# You should set your deepl auth key in your environment variable. | ||
auth_key = os.environ["DEEPL_AUTH_KEY"] | ||
|
||
# In some area on this earth, you should set a proper api server site at first. | ||
server_url = "https://api-free.deepl.com" | ||
|
||
tl = translator = deepl.Translator(auth_key, server_url=server_url) | ||
|
||
# this matches every "" sequence that contains a non-ascii unicode character | ||
# also accounts for escaped \" groups inside the double quotes | ||
match_jap = r'"((?:\\.|[^\\"\n\r])*)[\u0080-\uffff]((?:\\.|[^\\"\n\r])*)"' | ||
|
||
|
||
# replace matches of the r regex in s with f(match) | ||
def regex_replace(r, s, f): | ||
new = "" | ||
start = 0 | ||
for m in re.finditer(r, s): | ||
end, newstart = m.span() | ||
new += s[start:end] | ||
new += f(m.group()) | ||
start = newstart | ||
new += s[start:] | ||
return new | ||
|
||
|
||
def copy_structure(src, dst): | ||
for dirpath, dirnames, _ in os.walk(src): | ||
for d in dirnames: | ||
os.makedirs( | ||
os.path.join(dst, *dirpath.split(os.path.sep)[1:], d), | ||
exist_ok=True, | ||
) | ||
|
||
|
||
def translate(s): | ||
# un-escape the string so that it doesnt confuse deepl | ||
res = s[1:-1].replace('\\"', '"') | ||
res = tl.translate_text( | ||
res, source_lang="JA", target_lang=target_lang_code , preserve_formatting=True | ||
).text | ||
return '"' + res.replace('"', '\\"') + '"' | ||
|
||
|
||
def create_file_set(path, f=lambda s: s.endswith(".snbt")): | ||
res = set() | ||
for dirpath, _, filenames in os.walk(path): | ||
for filename in filenames: | ||
if not f(filename): | ||
continue | ||
file = os.path.join(dirpath, filename) | ||
if os.path.isfile(file): | ||
res.add(os.path.join(*file.split(os.path.sep)[1:])) | ||
return res | ||
|
||
def load_json_file(file_path): | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
return json.load(file) | ||
|
||
def translate_text(text, translator, source_lang='JA', target_lang=target_lang_code): | ||
if not text.strip(): | ||
return text | ||
return translator.translate_text(text, source_lang=source_lang, target_lang=target_lang).text | ||
|
||
def translate_json_values(json_data, translator, keys_to_translate=['name:8', 'desc:8']): | ||
for quest_id, quest_data in json_data.get('questDatabase:9', {}).items(): | ||
for key in keys_to_translate: | ||
if key in quest_data.get('properties:10', {}).get('betterquesting:10', {}): | ||
original_text = quest_data['properties:10']['betterquesting:10'][key] | ||
if original_text: | ||
translated_text = translate_text(original_text, translator) | ||
quest_data['properties:10']['betterquesting:10'][key] = translated_text | ||
return json_data | ||
|
||
def save_json_file(file_path, data): | ||
with open(file_path, 'w', encoding='utf-8') as file: | ||
json.dump(data, file, ensure_ascii=False, indent=2) | ||
|
||
def main(): | ||
copy_structure(source_path, target_path) | ||
ofiles = create_file_set(source_path) | ||
tlfiles = create_file_set(target_path) | ||
to_translate = ofiles - tlfiles | ||
if not to_translate: | ||
print("translation seems up to date") | ||
return | ||
for fp in to_translate: | ||
with open(os.path.join(source_path, fp), "r", encoding='utf-8') as f: | ||
s = f.read() | ||
print(f"translating {fp}...") | ||
translated = regex_replace(match_jap, s, translate) | ||
with open(os.path.join(target_path, fp), "x", encoding='utf-8') as tf: | ||
tf.write(translated) | ||
print("done") | ||
|
||
translator = deepl.Translator(auth_key, server_url=server_url) | ||
json_data = load_json_file("./DefaultQuests.json") | ||
translated_json_data = translate_json_values(json_data, translator) | ||
save_json_file("./bqu/DefaultQuests_" + target_lang_code + ".json", translated_json_data) | ||
|
||
main() | ||
if __name__ == "__main__": | ||
main() |