Skip to content

Commit

Permalink
fix: Fixed the bug where special characters prevented custom engine t…
Browse files Browse the repository at this point in the history
…ranslation.
  • Loading branch information
bookfere committed Oct 8, 2023
1 parent 8dc8d06 commit db916d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions engines/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ChatgptTranslate(Base):

concurrency_limit = 1
request_interval = 20
request_timeout = 30.0

prompt = (
'You are a meticulous translator who translates any given content. '
Expand Down
2 changes: 1 addition & 1 deletion engines/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def translate(self, text):
# to ensure pure Latin-1 (compliance with ISO-8859-1).
data = data.replace('<source>', self._get_source_code()) \
.replace('<target>', self._get_target_code()) \
.replace('<text>', text.replace('"', '\\"')).encode('utf-8')
.replace('<text>', json.dumps(text).strip('"')).encode('utf-8')
is_json = headers and 'application/json' in headers.values()
if need_restore and not is_json:
data = json.loads(data)
Expand Down
27 changes: 22 additions & 5 deletions tests/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,19 @@ def setUp(self):
engine_data = """{
"name": "New Engine",
"languages": {
"source": {"English": "en"},
"target": {"Chinese": "zh"}
"source": {
"English": "en"
},
"target": {
"Chinese": "zh"
}
},
"request": {
"url": "https://example.api",
"method": "POST",
"headers": {"Content-Type": "application/json"},
"headers": {
"Content-Type": "application/json"
},
"data": {
"source": "<source>",
"target": "<target>",
Expand All @@ -129,13 +135,24 @@ def setUp(self):
CustomTranslate.set_engine_data(engine_data)

@patch('calibre_plugins.ebook_translator.engines.base.Browser')
def test_translate(self, mock_browser):
def test_translate_json(self, mock_browser):
translator = CustomTranslate()
translator.set_source_lang('English')
translator.set_target_lang('Chinese')
mock_browser.return_value.response.return_value.read.return_value \
.decode.return_value = '{"text": "你好世界!"}'
self.assertEqual('你好世界!', translator.translate('Hello World!'))
self.assertEqual('你好世界!', translator.translate('HelloWorld!'))

@patch('calibre_plugins.ebook_translator.engines.base.Browser')
def test_translate_urlencoded(self, mock_browser):
translator = CustomTranslate()
# Mock content type: application/x-www-form-urlencoded
del translator.engine_data['request']['headers']
translator.set_source_lang('English')
translator.set_target_lang('Chinese')
mock_browser.return_value.response.return_value.read.return_value \
.decode.return_value = '{"text": "你好\\"\\n世界!"}'
self.assertEqual('你好\"\n世界!', translator.translate('Hello\"\nWorld!'))

def test_parse(self):
translator = CustomTranslate()
Expand Down

0 comments on commit db916d5

Please sign in to comment.