Skip to content

Commit

Permalink
google translator
Browse files Browse the repository at this point in the history
  • Loading branch information
CroMarmot committed Nov 26, 2023
1 parent b32eeef commit 2dacf23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
57 changes: 19 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,37 @@
# DICT

## 怎么安装
## Install

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

```Bash
curl -o dict.py https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py
# edit ~/.bashrc
alias y='python3 <your path>/DICT.py'
# or
alias g='python3 <your path>/googletranslator.py'
```

- 编辑 ~/.bashrc
## Usage

```Bash
vim ~/.bashrc
```

- 在末尾加上

```Bash
alias y='python3 <yourpath>/DICT.py'
```

保存后执行`source ~/.bashrc`或重新开启终端可用

- 使用:

```Bash
```bash
y 你好
y word or sentence
g google 翻译可能需要proxy
```

- 也可以直接执行脚本进行交互式翻译(解决 要翻译的内容带有引号或命令行特殊符号的问题):

`y`

- 也可以直接使用:

```Bash
curl -s https://raw.githubusercontent.com/YeXiaoRain/DICT/python3/DICT.py | python3 - word or sentence
```

## 启用OpenapiYoudao
## Enable OpenapiYoudao

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

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

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

## 其它翻译项目(如nodejs分支)
## Deps

|repo|source|comment|modified|
|---|---|---|---|
|[command-line-tool/dictionary](https://github.com/command-line-tool/dictionary)|youdaoapi|不支持句子|youdaodict|
|[syaning/dict-en-zh](https://github.com/syaning/dict-en-zh)|youdao网页/shanbayapi|shanbay源比较垃圾|-|
|[Toybreak/cliDict](https://github.com/Toybreak/cliDict)|bing|没有中->英 不支持句子|bingdict|
|[justinleoye/tuzki-dict](https://github.com/justinleoye/tuzki-dict)||chrome插件|-|
- ~~<http://fanyi.youdao.com>~~
- yodao.com
- <https://openapi.youdao.com/api>
- googletrans
38 changes: 38 additions & 0 deletions googletranslator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# write by chatgpt3.5
import argparse
from googletrans import Translator

def google_translator(original_text):
def detect_language(text):
translator = Translator()
result = translator.detect(text)
return result.lang

def translate_text(text, target_language='en'):
translator = Translator()
translation = translator.translate(text, dest=target_language)
return translation.text
# Detect source language
source_language = detect_language(original_text)
print(f"Source Language: {source_language}")

# Translate text
if source_language == 'zh-CN':
target_language = 'en'
else:
target_language = 'zh-CN'

translated_text = translate_text(original_text, target_language)
print(f"Google Translation: {translated_text}")

def main():
parser = argparse.ArgumentParser(description='Translate text between Chinese and English.')
parser.add_argument('text', nargs='+', help='The text to translate, multiple words are accepted.')

args = parser.parse_args()
original_text = ' '.join(args.text)
google_translator(original_text)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
googletrans==4.0.0-rc1

0 comments on commit 2dacf23

Please sign in to comment.