• Stars
    star
    197
  • Rank 196,519 (Top 4 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created about 3 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

谷歌翻译, 支持 APIKEY 一口气翻译十万条

pygtrans

谷歌翻译, 支持 APIKEY

Downloads

安装

pip install -U pygtrans
pip install git+ssh://[email protected]/foyoux/pygtrans.git
pip install git+https://github.com/foyoux/pygtrans.git

基本功能

  • 获取语言支持列表
  • 自动检测语言, 支持批量
  • 文本/HTML 翻译, 支持批量
  • 支持 TTS

快速入门

from pygtrans import Translate

client = Translate(proxies={'https': 'http://localhost:10809'})

# 检测语言
text = client.detect('Answer the question.')
assert text.language == 'en'

# 翻译句子
text = client.translate('Look at these pictures and answer the questions.')
assert text.translatedText == '看这些图片,回答问题。'

# 批量翻译
texts = client.translate([
    'Good morning. What can I do for you?',
    'Read aloud and underline the sentences about booking a flight.',
    'May I have your name and telephone number?'
])
assert [text.translatedText for text in texts] == [
    '早上好。我能为你做什么?',
    '大声朗读并在有关预订航班的句子下划线。',
    '可以给我你的名字和电话号码吗?'
]

# 翻译到日语
text = client.translate('请多多指教', target='ja')
assert text.translatedText == 'お知らせ下さい'

# 翻译到韩语
text = client.translate('请多多指教', target='ko')
assert text.translatedText == '조언 부탁드립니다'

# 文本到语音
tts = client.tts('やめて', target='ja')
open('やめて.mp3', 'wb').write(tts)

最佳实践

  1. pygtrans中包含两个翻译模块
    1. Translate:
      • 完全免费,支持批量
      • 从2021年9月15日开始, 需要翻墙才能使用, 具体参考 #8
    2. ApiKeyTranslate: 需要有效的谷歌翻译 API KEY谷歌提供免费试用
  2. Translate的最佳实践:
    1. http 代理:Translate(proxies={"https": "http://localhost:10809"})
    2. socks5 代理: Translate(proxies={"https": "socks5://localhost:10808"})
    3. 重要:尽量一次性多翻译,减少请求次数,参考 #13,比如一次性翻译 2000 / 5000 / 10000