Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: 无法从中文翻译成英文 #157

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
12 changes: 12 additions & 0 deletions src/translation_module.py
Original file line number Diff line number Diff line change
@@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use a more secure method to encode the text, such as quote_plus from urllib.parse, to ensure special characters are correctly handled in the request. [security]

Suggested change
encoded_text = text.encode('utf-8')
from urllib.parse import quote_plus
encoded_text = quote_plus(text)

response = requests.post('https://api.translation.service/translate',
data={'text': encoded_text, 'source': source_lang, 'target': target_lang})
Comment on lines +6 to +7

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API endpoint URL is hardcoded directly in the function. This could be a security risk and reduces flexibility if the endpoint needs to change or be different in various environments.

Recommended Solution:
Extract the API URL into a configuration file or environment variable. This approach enhances security by not exposing the endpoint directly in the code and increases flexibility by allowing the endpoint to be easily changed without modifying the codebase.

import os
API_URL = os.getenv('TRANSLATION_API_URL')
response = requests.post(API_URL, 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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is missing a return statement for the case when response.status_code is 500. Consider adding a return statement to handle this scenario appropriately.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function lacks error handling for HTTP 500 responses, which indicates a server error. It's important to handle all potential API response statuses to ensure the application can gracefully handle errors and provide meaningful feedback to the user.

Recommended Solution:
Add an else block to handle other unexpected status codes, including 500. For example:

else:
    return 'An error occurred. Please try again later.'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Handle other HTTP status codes and potential exceptions to ensure the function is robust against unexpected responses or network issues. [enhancement]

Suggested change
elif response.status_code == 500:
else:
return f'Unexpected error: {response.status_code}. Please try again later.'

5 changes: 5 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@
const { stderr } = await runScript(['ant love']);
expect(stderr).not.toContain('访问 iciba 失败');
Comment on lines 82 to 83

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test checks for the absence of a specific error message but does not verify if the operation was successful or if other errors occurred. This could lead to false positives in test results.

Recommendation:
Enhance the test by checking for the successful completion of the operation or by verifying that no errors occurred at all, not just the absence of a specific message.

});

it('should translate Chinese text to English', async () => {
const { stdout } = await runScript(['translate', '无法翻译这段文字']);
expect(stdout).toContain('Translation failed. Please try again later.');

Check failure on line 88 in tests/index.test.ts

View workflow job for this annotation

GitHub Actions / test (18)

tests/index.test.ts > fanyi CLI > should translate Chinese text to English

AssertionError: expected '\n translate 无法翻译这段文字 \u001b[90m ~ i…' to contain 'Translation failed. Please try again …' - Expected + Received - Translation failed. Please try again later. + + translate 无法翻译这段文字 ~ iciba.com + + ----- + + translate [trænzˈleɪt] ~ 翻译 [fān yì] ~ 💡 + + - 动词 [动] 把一种语言转换成另一种语言 + - 名词 [名] 翻译的结果;译文 + + 例句: + 1. 请帮助我翻译这篇文章。 + Please help me translate this article. + 2. 他是一位优秀的翻译家。 + He is an excellent translator. + + 💡 Believe in the power of language, and let translation be the bridge to a wider world. + 相信语言的力量,让翻译成为通往更广阔世界的桥梁。 + ❯ tests/index.test.ts:88:20
Comment on lines +86 to +88

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test expects a failure message ('Translation failed. Please try again later.') without clarifying if this is the intended behavior or a simulated condition. This could confuse the purpose of the test and what it is actually verifying.

Recommendation:
Clarify in the test description whether this is the expected behavior under normal conditions or if the test setup is specifically designed to simulate a failure scenario. This will improve the readability and the intent of the test.

});
});
Loading