dmMaze / BallonsTranslator

深度学习辅助漫画翻译工具, 支持一键机翻和简单的图像/文本编辑 | Yet another computer-aided comic/manga translation tool powered by deeplearning
GNU General Public License v3.0
2.5k stars 172 forks source link

Custom Offline Translator when installed from Releases #101

Closed max14354 closed 1 year ago

max14354 commented 1 year ago

I tried to follow #39 instructions and the provided guide but I can't seem to find the file where the translators are defined or its equivalent classes. I supposed the reason was that I downloaded from the releases tab instead of cloning the github.

My question is if its possible to setup a custom translator in my case and where should I define it. Specifically its a simple request to a local server that will return the translated text.

Thanks in advance, this tool is truly extraordinary

dmMaze commented 1 year ago

I supposed the reason was that I downloaded from the releases tab instead of cloning the github.

Yes, the released packages have the source code compiled into an executable file. Maybe I'll add some kind of extension system supporting installing scripts from external sources in the future.

max14354 commented 1 year ago

For anyone who is interested, a workaround for this is to run Ballon with translation turned off and use your program to edit the project.json, extracting the text, translate it and send it back to the .json.

Example:

import requests
import json

def send_message_to_server(content, message):
    url = "http://localhost/"
    headers = {'Content-Type': 'application/json'}
    data = {'content': content, 'message': message}
    response = requests.post(url, headers=headers, data=json.dumps(data))
    response_json = response.json()
    print(response_json)
    return response_json

with open('imgtrans_manga.json', 'r', encoding= "UTF-8") as f:
    data = json.load(f)

for page in data["pages"]:
    for textnumber in range(len(data["pages"][page])):
        data["pages"][page][textnumber]["translation"] = send_message_to_server(data["pages"][page][textnumber]["text"], "translate sentences").upper()

with open("imgtrans_manga.json", "w") as json_file:
    json.dump(data, json_file)