# pygtrans
谷歌翻译, 支持 **APIKEY**
- [Github](https://github.com/foyoux/pygtrans)
- [语言支持列表](https://pygtrans.readthedocs.io/zh_CN/latest/langs.html)
[![](https://img.shields.io/github/v/release/foyoux/pygtrans)](https://github.com/foyoux/pygtrans/releases) ![](https://img.shields.io/github/last-commit/foyoux/pygtrans) [![Downloads](https://static.pepy.tech/personalized-badge/pygtrans?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pygtrans)
## 安装
[![](https://img.shields.io/pypi/pyversions/pygtrans)](https://pypi.org/project/pygtrans/)
```bash
pip install -U pygtrans
pip install git+ssh://git@github.com/foyoux/pygtrans.git
pip install git+https://github.com/foyoux/pygtrans.git
```
## 基本功能
- [x] 获取语言支持列表
- [x] 自动检测语言, 支持批量
- [x] 文本/HTML 翻译, 支持批量
- [x] 支持 TTS
## 快速入门
```python
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](https://github.com/foyoux/pygtrans/issues/8)
2. `ApiKeyTranslate`: 需要有效的谷歌翻译 **API KEY**,[谷歌提供免费试用](https://cloud.google.com/translate/docs/quickstarts)
2. `Translate`的最佳实践:
1. `http` 代理:`Translate(proxies={"https": "http://localhost:10809"})`
2. `socks5` 代理: `Translate(proxies={"https": "socks5://localhost:10808"})`
3. **重要**:尽量一次性多翻译,减少请求次数,参考 [#13](https://github.com/foyoux/pygtrans/issues/13),比如一次性翻译 2000 / 5000 / 10000
Raw data
{
"_id": null,
"home_page": "",
"name": "pygtrans",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "pygtrans,google,translate,apikey,text,html,google.cn,google.com",
"author": "foyoux",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/69/a5/4b74dc7784ff616cd1efb0e169f375a9d6eeb1eef48bbb02e6edf3c5724d/pygtrans-1.5.3.tar.gz",
"platform": null,
"description": "# pygtrans\n\n\u8c37\u6b4c\u7ffb\u8bd1, \u652f\u6301 **APIKEY**\n\n- [Github](https://github.com/foyoux/pygtrans)\n- [\u8bed\u8a00\u652f\u6301\u5217\u8868](https://pygtrans.readthedocs.io/zh_CN/latest/langs.html)\n\n[![](https://img.shields.io/github/v/release/foyoux/pygtrans)](https://github.com/foyoux/pygtrans/releases) ![](https://img.shields.io/github/last-commit/foyoux/pygtrans) [![Downloads](https://static.pepy.tech/personalized-badge/pygtrans?period=total&units=international_system&left_color=black&right_color=orange&left_text=Downloads)](https://pepy.tech/project/pygtrans)\n\n\n\n## \u5b89\u88c5\n\n[![](https://img.shields.io/pypi/pyversions/pygtrans)](https://pypi.org/project/pygtrans/) \n\n```bash\npip install -U pygtrans\npip install git+ssh://git@github.com/foyoux/pygtrans.git\npip install git+https://github.com/foyoux/pygtrans.git\n```\n\n\n## \u57fa\u672c\u529f\u80fd\n\n- [x] \u83b7\u53d6\u8bed\u8a00\u652f\u6301\u5217\u8868\n- [x] \u81ea\u52a8\u68c0\u6d4b\u8bed\u8a00, \u652f\u6301\u6279\u91cf\n- [x] \u6587\u672c/HTML \u7ffb\u8bd1, \u652f\u6301\u6279\u91cf\n- [x] \u652f\u6301 TTS\n\n\n## \u5feb\u901f\u5165\u95e8\n\n```python\nfrom pygtrans import Translate\n\nclient = Translate(proxies={'https': 'http://localhost:10809'})\n\n# \u68c0\u6d4b\u8bed\u8a00\ntext = client.detect('Answer the question.')\nassert text.language == 'en'\n\n# \u7ffb\u8bd1\u53e5\u5b50\ntext = client.translate('Look at these pictures and answer the questions.')\nassert text.translatedText == '\u770b\u8fd9\u4e9b\u56fe\u7247\uff0c\u56de\u7b54\u95ee\u9898\u3002'\n\n# \u6279\u91cf\u7ffb\u8bd1\ntexts = client.translate([\n 'Good morning. What can I do for you?',\n 'Read aloud and underline the sentences about booking a flight.',\n 'May I have your name and telephone number?'\n])\nassert [text.translatedText for text in texts] == [\n '\u65e9\u4e0a\u597d\u3002\u6211\u80fd\u4e3a\u4f60\u505a\u4ec0\u4e48\uff1f',\n '\u5927\u58f0\u6717\u8bfb\u5e76\u5728\u6709\u5173\u9884\u8ba2\u822a\u73ed\u7684\u53e5\u5b50\u4e0b\u5212\u7ebf\u3002',\n '\u53ef\u4ee5\u7ed9\u6211\u4f60\u7684\u540d\u5b57\u548c\u7535\u8bdd\u53f7\u7801\u5417\uff1f'\n]\n\n# \u7ffb\u8bd1\u5230\u65e5\u8bed\ntext = client.translate('\u8bf7\u591a\u591a\u6307\u6559', target='ja')\nassert text.translatedText == '\u304a\u77e5\u3089\u305b\u4e0b\u3055\u3044'\n\n# \u7ffb\u8bd1\u5230\u97e9\u8bed\ntext = client.translate('\u8bf7\u591a\u591a\u6307\u6559', target='ko')\nassert text.translatedText == '\uc870\uc5b8 \ubd80\ud0c1\ub4dc\ub9bd\ub2c8\ub2e4'\n\n# \u6587\u672c\u5230\u8bed\u97f3\ntts = client.tts('\u3084\u3081\u3066', target='ja')\nopen('\u3084\u3081\u3066.mp3', 'wb').write(tts)\n```\n\n\n\n## \u6700\u4f73\u5b9e\u8df5\n\n1. `pygtrans`\u4e2d\u5305\u542b\u4e24\u4e2a\u7ffb\u8bd1\u6a21\u5757\n 1. `Translate`: \n - \u5b8c\u5168\u514d\u8d39\uff0c\u652f\u6301\u6279\u91cf\n - \u4ece2021\u5e749\u670815\u65e5\u5f00\u59cb, \u9700\u8981\u7ffb\u5899\u624d\u80fd\u4f7f\u7528, \u5177\u4f53\u53c2\u8003 [#8](https://github.com/foyoux/pygtrans/issues/8)\n 2. `ApiKeyTranslate`: \u9700\u8981\u6709\u6548\u7684\u8c37\u6b4c\u7ffb\u8bd1 **API KEY**\uff0c[\u8c37\u6b4c\u63d0\u4f9b\u514d\u8d39\u8bd5\u7528](https://cloud.google.com/translate/docs/quickstarts)\n2. `Translate`\u7684\u6700\u4f73\u5b9e\u8df5:\n 1. `http` \u4ee3\u7406\uff1a`Translate(proxies={\"https\": \"http://localhost:10809\"})`\n 2. `socks5` \u4ee3\u7406: `Translate(proxies={\"https\": \"socks5://localhost:10808\"})`\n 3. **\u91cd\u8981**\uff1a\u5c3d\u91cf\u4e00\u6b21\u6027\u591a\u7ffb\u8bd1\uff0c\u51cf\u5c11\u8bf7\u6c42\u6b21\u6570\uff0c\u53c2\u8003 [#13](https://github.com/foyoux/pygtrans/issues/13)\uff0c\u6bd4\u5982\u4e00\u6b21\u6027\u7ffb\u8bd1 2000 / 5000 / 10000\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Google Translate, support APIKEY",
"version": "1.5.3",
"project_urls": {
"Bug Tracker": "https://github.com/foyoux/pygtrans/issues",
"Documentation": "https://pygtrans.readthedocs.io/zh_CN/latest/",
"Homepage": "https://github.com/foyoux/pygtrans",
"Source": "https://github.com/foyoux/pygtrans"
},
"split_keywords": [
"pygtrans",
"google",
"translate",
"apikey",
"text",
"html",
"google.cn",
"google.com"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bd73475eff1206d72eb887c1fdc101ddc6c1a974e129e07686a9837f0b2a1835",
"md5": "e2ae678de9d572fa3ed5462d76d8987f",
"sha256": "2eeec1cbd2a738081a857c2879678410ac150eff7be347ee4a1b770db9b14d08"
},
"downloads": -1,
"filename": "pygtrans-1.5.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e2ae678de9d572fa3ed5462d76d8987f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 23949,
"upload_time": "2023-07-27T08:30:52",
"upload_time_iso_8601": "2023-07-27T08:30:52.636348Z",
"url": "https://files.pythonhosted.org/packages/bd/73/475eff1206d72eb887c1fdc101ddc6c1a974e129e07686a9837f0b2a1835/pygtrans-1.5.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "69a54b74dc7784ff616cd1efb0e169f375a9d6eeb1eef48bbb02e6edf3c5724d",
"md5": "413690f9a5f49575b499dc96f8947353",
"sha256": "c28a6d77281f9c02f65b0f5d4138f7c2ee5473d2cf8e87e3390dd5cf0a834ab7"
},
"downloads": -1,
"filename": "pygtrans-1.5.3.tar.gz",
"has_sig": false,
"md5_digest": "413690f9a5f49575b499dc96f8947353",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 22863,
"upload_time": "2023-07-27T08:30:53",
"upload_time_iso_8601": "2023-07-27T08:30:53.935136Z",
"url": "https://files.pythonhosted.org/packages/69/a5/4b74dc7784ff616cd1efb0e169f375a9d6eeb1eef48bbb02e6edf3c5724d/pygtrans-1.5.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-27 08:30:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "foyoux",
"github_project": "pygtrans",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": []
}
],
"lcname": "pygtrans"
}