From 5bec0f7bcf86172363349c88490d963381f11b3e Mon Sep 17 00:00:00 2001 From: "gitauto-ai[bot]" <161652217+gitauto-ai[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:07:33 +0000 Subject: [PATCH 1/4] Update src/translation_module.py --- src/translation_module.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/translation_module.py diff --git a/src/translation_module.py b/src/translation_module.py new file mode 100644 index 0000000..2ac4e3a --- /dev/null +++ b/src/translation_module.py @@ -0,0 +1,10 @@ +import requests + +def translate(text, source_lang='zh', target_lang='en'): + # Ensure the text is properly encoded + encoded_text = text.encode('utf-8') + response = requests.post('https://api.translation.service/translate', + data={'text': encoded_text, 'source': source_lang, 'target': target_lang}) + if response.status_code == 200: + return response.json().get('translatedText', '') + return 'Translation failed. Please try again later.' From d26e5676aa20591ad16232a17e3e3e395eaebf0c Mon Sep 17 00:00:00 2001 From: "gitauto-ai[bot]" <161652217+gitauto-ai[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:07:54 +0000 Subject: [PATCH 2/4] Update package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 3a720af..bfd2864 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "gradient-string": "^3.0.0", "groq-sdk": "^0.8.0", "node-fetch": "^3.3.2", + "axios": "^0.21.1", "ora": "^8.1.0", "update-notifier": "^7.3.1" }, From dade2e677da7f65a7537adbe25a3224a7fafdea0 Mon Sep 17 00:00:00 2001 From: "gitauto-ai[bot]" <161652217+gitauto-ai[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:08:05 +0000 Subject: [PATCH 3/4] Update src/translation_module.py --- src/translation_module.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/translation_module.py b/src/translation_module.py index 2ac4e3a..a2dde58 100644 --- a/src/translation_module.py +++ b/src/translation_module.py @@ -7,4 +7,6 @@ def translate(text, source_lang='zh', target_lang='en'): data={'text': encoded_text, 'source': source_lang, 'target': target_lang}) if response.status_code == 200: return response.json().get('translatedText', '') - return 'Translation failed. Please try again later.' + elif response.status_code == 400: + return 'Bad request. Please check the input text and try again.' + elif response.status_code == 500: From 444696443eebeb0ab5267cf27f98dff2e4190607 Mon Sep 17 00:00:00 2001 From: "gitauto-ai[bot]" <161652217+gitauto-ai[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 00:08:17 +0000 Subject: [PATCH 4/4] Update tests/index.test.ts --- tests/index.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/index.test.ts b/tests/index.test.ts index 1c7b831..633727e 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -82,4 +82,9 @@ describe('fanyi CLI', () => { const { stderr } = await runScript(['ant love']); expect(stderr).not.toContain('访问 iciba 失败'); }); + + it('should translate Chinese text to English', async () => { + const { stdout } = await runScript(['translate', '无法翻译这段文字']); + expect(stdout).toContain('Translation failed. Please try again later.'); + }); });