Skip to content

Commit

Permalink
bug fixes:
Browse files Browse the repository at this point in the history
- issue #10
- other
  • Loading branch information
infrost committed Nov 19, 2024
1 parent dbcfdac commit 9fcfd6c
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 131 deletions.
5 changes: 4 additions & 1 deletion Lib/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def update_shared_strings_in_pptx(file_path, strings):

for t_element in t_elements:
if string_index < len(strings):
t_element.text = strings[string_index]
if config.get("save_original", False):
t_element.text = (t_element.text or '') + strings[string_index]
else:
t_element.text = strings[string_index]
string_index += 1
else:
break
Expand Down
9 changes: 4 additions & 5 deletions Lib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
# 定义默认配置
default_config = {
"save_original": False, #保留原文件值
"version": "0.5.7",
"version": "0.5.9",
"dev": False,
#"direct_mode": False,
#"playwright_mode": False,
"i18n": "ZH",
"playwright_headless": False,
"playwright_path": "./Lib/Webkit/Playwright.exe", # Mac "./Lib/Webkit/pw_run.sh", windows "./Lib/Webkit/Playwright.exe"
"browser_login": False,
"force_lang_select": False,
"translation_mode": "deeplx",
"translation_mode": "playwright",
"disable_user_profile": False,
"enhance_mode": False,
"deepl_token": "",
"deeplx_core_version": "0.9.8"
"deeplx_core_version": "0.9.8.3",
"deeplx_server": ""
}

# 定义配置文件的路径
Expand Down
24 changes: 16 additions & 8 deletions Lib/data_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ def count_words(line):
# 统计一行中的词数,以空格分隔
return len(line.split())

def process_file(file_path, source_lang, target_lang, deepl_token):
def process_file(file_path, source_lang, target_lang, deepl_token, mode = 'w'):
if deepl_token:
print("deeplx方法:Pro模式,将会消耗token")
deeplx_api = "http://127.0.0.1:1188/v1/translate?token=deepl_token"
if config.get("deeplx_server"):
deeplx_api = f"http://{deeplx_server}/v1/translate?token=deepl_token"
else:
deeplx_api = "http://127.0.0.1:1188/v1/translate?token=deepl_token"
else:
print("deeplx方法:免费模式")
deeplx_api = "http://127.0.0.1:1188/translate"
if config.get("deeplx_server"):
deeplx_api = f"http://{deeplx_server}/translate"
else:
deeplx_api = "http://127.0.0.1:1188/translate"

with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()

Expand All @@ -42,7 +49,7 @@ def process_file(file_path, source_lang, target_lang, deepl_token):
if current_string:
strings_array.append('\n'.join(current_string))
alternative_index = 0 # 用于命名 alternatives 文件
with open('./out/translated_result.txt', 'w', encoding='utf-8') as result_file:
with open('./out/translated_result.txt', mode, encoding='utf-8') as result_file:
process_block_count = 1
for s in strings_array:
print(f"正在处理第{process_block_count}个块...")
Expand All @@ -54,7 +61,8 @@ def process_file(file_path, source_lang, target_lang, deepl_token):
"target_lang": target_lang
}
post_data = json.dumps(data)

print(post_data)
input()
retry_count = 0
max_retries = 1
success = False
Expand All @@ -75,14 +83,14 @@ def process_file(file_path, source_lang, target_lang, deepl_token):
result_file.write(response_data['data'] + '\n')
result_file.flush()
success = True
#print(f"收到数据 {response_data}")
print(f"收到数据 {response_data}")

# 如果存在 alternatives,保存每个替代到不同的文件
if "alternatives" in response_data and response_data["alternatives"] is not None:
alternatives = response_data["alternatives"]
print(alternatives)
#print(alternatives)
for alternative in alternatives:
with open(f'./out/alternatives({alternative_index}).txt', 'w', encoding='utf-8') as alt_file:
with open(f'./out/alternatives({alternative_index}).txt', mode, encoding='utf-8') as alt_file:
alt_file.write(alternative + '\n')
alternative_index += 1
break # 成功处理后退出重试循环
Expand Down
67 changes: 14 additions & 53 deletions Lib/playwright_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,67 +152,28 @@ def translate_text(page, text, source_lang, target_lang, force_lang_select):
output_element = page.wait_for_selector('section[aria-labelledby="translation-target-heading"] div[contenteditable="true"][role="textbox"][aria-multiline="true"]')

if output_element:
original_lines = len(text.split('\n'))+1
original_lines = len([line for line in text.split('\n') if line.strip()])
retry_count = 0
while True:
time.sleep(1) # 每秒检查一次
time.sleep(2) # 每秒检查一次
translated_text = output_element.inner_text()
translated_lines = [line for line in translated_text.splitlines() if line.strip()]
#print(f"原译文{translated_text}")

#edge浏览器得到的返回内容和webkit不一样,搞不懂。
if make_edge_happy:
translated_lines = translated_text.splitlines()
print(f"已翻译{len(translated_lines)}/{original_lines}行")
if len(translated_lines) == original_lines:
# 创建一个新列表用于合并翻译结果和原始输入的空行
processed_result = []
empty_line_count = 0
for line in translated_lines:
if line.strip() == "":
empty_line_count += 1
if empty_line_count == 3:
processed_result.append("") # 添加一个空行
translated_index = 0 # 用于跟踪翻译文本的索引
for line in text.split('\n'):
if line.strip() == "": # 如果原始行是空行
processed_result.append("") # 添加一个空行
else:
empty_line_count = 0
processed_result.append(line) # 添加非空行

if translated_index < len(translated_lines):
processed_result.append(translated_lines[translated_index]) # 添加对应的翻译文本
translated_index += 1

# 将处理后的结果合并成字符串
translated_text = "\n".join(processed_result) + "\n"
#print(f"处理后译文{translated_text}")
translated_lines = len(translated_text.split('\n'))
else:
if os.name == 'posix': # make MacOS happy
translated_lines = translated_text.splitlines()
processed_result = []
empty_line_count = 0
for line in translated_lines:
if line.strip() == "":
empty_line_count += 1
if empty_line_count == 3:
processed_result.append("") # 添加一个空行
else:
empty_line_count = 0
processed_result.append(line) # 添加非空行

# 将处理后的结果合并成字符串
translated_text = "\n".join(processed_result) + "\n"
translated_lines = len(translated_text.split('\n'))
if (translated_lines+1)/2 == original_lines:
translated_lines = translated_text.splitlines()
processed_result = []
empty_line_count = 0
for line in translated_lines:
if line.strip() == "":
empty_line_count += 1
if empty_line_count == 3:
processed_result.append("") # 添加一个空行
else:
empty_line_count = 0
processed_result.append(line) # 添加非空行

# 将处理后的结果合并成字符串
translated_text = "\n".join(processed_result) + "\n"
translated_lines = len(translated_text.split('\n'))

print(f"已翻译{translated_lines}/{original_lines}行")
if translated_lines == original_lines:
return translated_text
retry_count += 1
if retry_count >= 6:
Expand Down
7 changes: 4 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"save_original": false,
"version": "0.5.7",
"version": "0.5.9",
"dev": false,
"i18n": "ZH",
"playwright_headless": false,
Expand All @@ -9,7 +9,8 @@
"force_lang_select": false,
"translation_mode": "playwright",
"disable_user_profile": false,
"enhance_mode": false,
"enhance_mode": true,
"deepl_token": "",
"deeplx_core_version": "0.9.8"
"deeplx_core_version": "0.9.8",
"deeplx_server": ""
}
Loading

0 comments on commit 9fcfd6c

Please sign in to comment.