Skip to content

Commit 2dacf23

Browse files
committed
google translator
1 parent b32eeef commit 2dacf23

File tree

3 files changed

+58
-38
lines changed

3 files changed

+58
-38
lines changed

README.md

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,37 @@
11
# DICT
22

3-
## 怎么安装
3+
## Install
44

5-
- 下载 dict.py 到电脑里
5+
```bash
6+
apt install python3 python3-pip git
7+
git clone https://github.com/YeXiaoRain/DICT.git -b python3
8+
pip install --user requirememnts.txt
69

7-
```Bash
8-
curl -o dict.py https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py
10+
# edit ~/.bashrc
11+
alias y='python3 <your path>/DICT.py'
12+
# or
13+
alias g='python3 <your path>/googletranslator.py'
914
```
1015

11-
- 编辑 ~/.bashrc
16+
## Usage
1217

13-
```Bash
14-
vim ~/.bashrc
15-
```
16-
17-
- 在末尾加上
18-
19-
```Bash
20-
alias y='python3 <yourpath>/DICT.py'
21-
```
22-
23-
保存后执行`source ~/.bashrc`或重新开启终端可用
24-
25-
- 使用:
26-
27-
```Bash
18+
```bash
19+
y 你好
2820
y word or sentence
21+
g google 翻译可能需要proxy
2922
```
3023

31-
- 也可以直接执行脚本进行交互式翻译(解决 要翻译的内容带有引号或命令行特殊符号的问题):
32-
33-
`y`
34-
35-
- 也可以直接使用:
36-
37-
```Bash
38-
curl -s https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py | python3 - word or sentence
39-
```
40-
41-
## 启用OpenapiYoudao
24+
## Enable OpenapiYoudao
4225

4326
`https://ai.youdao.com/`创建应用
4427

4528
复制`_config.ini``config.ini`并配置你的`APP_ID``APP_SECRET`
4629

4730
如果没有配置则不会调用`OpenapiYoudao`
4831

49-
## 其它翻译项目(如nodejs分支)
32+
## Deps
5033

51-
|repo|source|comment|modified|
52-
|---|---|---|---|
53-
|[command-line-tool/dictionary](https://github.com/command-line-tool/dictionary)|youdaoapi|不支持句子|youdaodict|
54-
|[syaning/dict-en-zh](https://github.com/syaning/dict-en-zh)|youdao网页/shanbayapi|shanbay源比较垃圾|-|
55-
|[Toybreak/cliDict](https://github.com/Toybreak/cliDict)|bing|没有中->英 不支持句子|bingdict|
56-
|[justinleoye/tuzki-dict](https://github.com/justinleoye/tuzki-dict)||chrome插件|-|
34+
- ~~<http://fanyi.youdao.com>~~
35+
- yodao.com
36+
- <https://openapi.youdao.com/api>
37+
- googletrans

googletranslator.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# write by chatgpt3.5
2+
import argparse
3+
from googletrans import Translator
4+
5+
def google_translator(original_text):
6+
def detect_language(text):
7+
translator = Translator()
8+
result = translator.detect(text)
9+
return result.lang
10+
11+
def translate_text(text, target_language='en'):
12+
translator = Translator()
13+
translation = translator.translate(text, dest=target_language)
14+
return translation.text
15+
# Detect source language
16+
source_language = detect_language(original_text)
17+
print(f"Source Language: {source_language}")
18+
19+
# Translate text
20+
if source_language == 'zh-CN':
21+
target_language = 'en'
22+
else:
23+
target_language = 'zh-CN'
24+
25+
translated_text = translate_text(original_text, target_language)
26+
print(f"Google Translation: {translated_text}")
27+
28+
def main():
29+
parser = argparse.ArgumentParser(description='Translate text between Chinese and English.')
30+
parser.add_argument('text', nargs='+', help='The text to translate, multiple words are accepted.')
31+
32+
args = parser.parse_args()
33+
original_text = ' '.join(args.text)
34+
google_translator(original_text)
35+
36+
37+
if __name__ == "__main__":
38+
main()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
googletrans==4.0.0-rc1

0 commit comments

Comments
 (0)