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" }, diff --git a/src/translation_module.py b/src/translation_module.py new file mode 100644 index 0000000..a2dde58 --- /dev/null +++ b/src/translation_module.py @@ -0,0 +1,12 @@ +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', '') + elif response.status_code == 400: + return 'Bad request. Please check the input text and try again.' + elif response.status_code == 500: 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.'); + }); });