Skip to content

Commit

Permalink
<feature> support web_brower translate and remember source/target lan…
Browse files Browse the repository at this point in the history
…gauge and support version check

update version to v2.3.8
  • Loading branch information
anonymousException committed Jun 10, 2024
1 parent b368db7 commit c112444
Show file tree
Hide file tree
Showing 46 changed files with 3,756 additions and 2,005 deletions.
200 changes: 168 additions & 32 deletions src/editor_form.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/extract_runtime_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from extraction_runtime import Ui_ExtractionRuntimeDialog
from my_log import log_print
from editor_form import open_directory_and_select_file
from html_util import open_directory_and_select_file
from string_tool import encode_say_string

hook_script = 'hook_extract.rpy'
Expand Down
2 changes: 1 addition & 1 deletion src/extraction_official_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from PySide6.QtWidgets import QDialog, QFileDialog

from my_log import log_print
from editor_form import open_directory_and_select_file
from html_util import open_directory_and_select_file
from extraction_official import Ui_ExtractionOfficialDialog
from call_game_python import get_python_path_from_game_path, get_py_path
import my_log
Expand Down
2 changes: 1 addition & 1 deletion src/font_replace_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def select_directory3(self):
def select_font(self):
file, filetype = QFileDialog.getOpenFileName(self,
QCoreApplication.translate("FontReplaceDialog", "select the file font which supports the translated language", None),
'', # 起始路径
'',
"Font Files (*.ttf || *.otf || *.ttc || *.otc || *.woff || *.woff2);;All Files (*)")
self.selectFontText.setText(file)

Expand Down
2 changes: 1 addition & 1 deletion src/html_converter_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from html_converter import Ui_HtmlConverterDialog
from html_util import plain_text_to_html
from editor_form import open_directory_and_select_file
from html_util import open_directory_and_select_file


class MyHtmlConverterForm(QDialog, Ui_HtmlConverterDialog):
Expand Down
86 changes: 32 additions & 54 deletions src/html_util.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import io
import json
import os.path
import subprocess
import threading

from bs4 import BeautifulSoup, NavigableString

from my_log import log_print
from renpy_translate import get_translated
from string_tool import EncodeBrackets, isAllPunctuations

last_write_html = None
last_translated_dic = None

_write_lock = threading.Lock()


def write_html_with_strings(p, strings, data):
if strings is None:
return
_write_lock.acquire()
if os.path.isfile(p):
_strings, _data = read_strings_from_html(p)
if data is not None:
data = json.loads(data)
_data = json.loads(_data)
for i in _data:
data.append(i)
data = json.dumps(data)
for i in _strings:
strings.append(i)

soup = BeautifulSoup('<html><body></body></html>', 'html.parser')
for s in strings:
b_tag = soup.new_tag("b",)
b_tag = soup.new_tag("h6", )
b_tag.string = s
soup.body.append(b_tag)
soup.body.append(soup.new_tag("br"))
if data is not None:
data_div = soup.new_tag("div", id="data", style="display: none;")
data_div.string = data
Expand All @@ -28,19 +41,20 @@ def write_html_with_strings(p, strings, data):
f.write(str(soup))
global last_write_html
last_write_html = p
_write_lock.release()


def read_strings_from_html(p):
if not os.path.isfile(p):
return None
return None, None
with open(p, "r", encoding="utf-8") as f:
soup = BeautifulSoup(f, 'html.parser')
b_tags = soup.find_all('b')
h6_tags = soup.find_all('h6')
data_div = soup.find(id='data')
data = None
if data_div is not None:
data = data_div.string
strings = [tag.get_text() for tag in b_tags]
strings = [tag.get_text() for tag in h6_tags]
return strings, data


Expand All @@ -57,53 +71,8 @@ def read_strings_from_translated(p):
return l


def get_translated_dic(html_path, translated_path):
dic = dict()
ori_strings, data = read_strings_from_html(html_path)
if data is not None:
data = json.loads(data)
global last_translated_dic
if ori_strings is None or len(ori_strings) == 0:
last_translated_dic = None
return None
translated_strings = read_strings_from_translated(translated_path)
if translated_strings is None or len(translated_strings) == 0:
last_translated_dic = None
return None
if len(ori_strings) != len(translated_strings):
log_print('Error:translated file does not match the html file')
last_translated_dic = None
return None
if data is not None:
for i, e in enumerate(data):
translated_dic = dict()
target = e['target']
line = e['line']
if 'd' not in e:
dic[ori_strings[i]] = translated_strings[i]
continue
d = e['d']
translated = translated_strings[i]
translated_dic[target] = translated
translated = get_translated(translated_dic, d)
if translated is None:
translated = ''
encoded = d['encoded'].strip('"')
if encoded in translated_dic:
translated = translated_dic[encoded]
log_print(
f'{translated_path} Error in line:{str(i + 1)} row:{line}\n{target}\n{encoded}\n{translated}\nError')
dic[ori_strings[i]] = translated
else:
for i, e in enumerate(ori_strings):
dic[e] = translated_strings[i]
last_translated_dic = dic
return dic, data is not None


def plain_text_to_html(p, output_p, is_replace_special_symbols):
def plain_text_to_html_from_list(l, output_p, is_replace_special_symbols):
ret = []
l = read_strings_from_translated(p)
for i, e in enumerate(l):
dic = dict()
target = e
Expand All @@ -123,3 +92,12 @@ def plain_text_to_html(p, output_p, is_replace_special_symbols):
if not is_replace_special_symbols:
data = None
write_html_with_strings(output_p, l, data)


def plain_text_to_html(p, output_p, is_replace_special_symbols):
l = read_strings_from_translated(p)
return plain_text_to_html_from_list(l, output_p, is_replace_special_symbols)


def open_directory_and_select_file(file_path):
subprocess.run(["explorer", "/select,", os.path.normpath(file_path)])
5 changes: 3 additions & 2 deletions src/import_html_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from PySide6.QtWidgets import QDialog, QFileDialog, QMessageBox

from import_html import Ui_ImportHtmlDialog
import html_util
from renpy_translate import get_translated_dic


class MyImportHtmlForm(QDialog, Ui_ImportHtmlDialog):
def __init__(self, parent=None):
Expand All @@ -26,7 +27,7 @@ def on_import_button_clicked(self):
translated_file = translated_file.replace('file:///', '')
if len(translated_file) == 0:
return
dic, is_replace_special_symbols = html_util.get_translated_dic(html_file, translated_file)
dic, is_replace_special_symbols = get_translated_dic(html_file, translated_file)
self.dic = dic
self.is_replace_special_symbols = is_replace_special_symbols
if self.dic is None:
Expand Down
Loading

0 comments on commit c112444

Please sign in to comment.