[编程基础] Python谷歌翻译库googletrans总结
1 使用说明
本文介绍python谷歌翻译库接口googletrans的使用。具体见官方文档:
https://py-googletrans.readthedocs.io/en/latest/#googletrans.Translator.translate
文章目录
1.1 安装
安装谷歌python翻译库,建议用清华源镜像
# pip install googletrans
在下面示例中,我们列出了 googletrans支持的语言。
import googletrans
# 谷歌翻译支持的语言
print(googletrans.LANGUAGES)
{‘af’: ‘afrikaans’, ‘sq’: ‘albanian’, ‘am’: ‘amharic’, ‘ar’: ‘arabic’, ‘hy’: ‘armenian’, ‘az’: ‘azerbaijani’, ‘eu’: ‘basque’, ‘be’: ‘belarusian’, ‘bn’: ‘bengali’, ‘bs’: ‘bosnian’, ‘bg’: ‘bulgarian’, ‘ca’: ‘catalan’, ‘ceb’: ‘cebuano’, ‘ny’: ‘chichewa’, ‘zh-cn’: ‘chinese (simplified)’, ‘zh-tw’: ‘chinese (traditional)’, ‘co’: ‘corsican’, ‘hr’: ‘croatian’, ‘cs’: ‘czech’, ‘da’: ‘danish’, ‘nl’: ‘dutch’, ‘en’: ‘english’, ‘eo’: ‘esperanto’, ‘et’: ‘estonian’, ‘tl’: ‘filipino’, ‘fi’: ‘finnish’, ‘fr’: ‘french’, ‘fy’: ‘frisian’, ‘gl’: ‘galician’, ‘ka’: ‘georgian’, ‘de’: ‘german’, ‘el’: ‘greek’, ‘gu’: ‘gujarati’, ‘ht’: ‘haitian creole’, ‘ha’: ‘hausa’, ‘haw’: ‘hawaiian’, ‘iw’: ‘hebrew’, ‘he’: ‘hebrew’, ‘hi’: ‘hindi’, ‘hmn’: ‘hmong’, ‘hu’: ‘hungarian’, ‘is’: ‘icelandic’, ‘ig’: ‘igbo’, ‘id’: ‘indonesian’, ‘ga’: ‘irish’, ‘it’: ‘italian’, ‘ja’: ‘japanese’, ‘jw’: ‘javanese’, ‘kn’: ‘kannada’, ‘kk’: ‘kazakh’, ‘km’: ‘khmer’, ‘ko’: ‘korean’, ‘ku’: ‘kurdish (kurmanji)’, ‘ky’: ‘kyrgyz’, ‘lo’: ‘lao’, ‘la’: ‘latin’, ‘lv’: ‘latvian’, ‘lt’: ‘lithuanian’, ‘lb’: ‘luxembourgish’, ‘mk’: ‘macedonian’, ‘mg’: ‘malagasy’, ‘ms’: ‘malay’, ‘ml’: ‘malayalam’, ‘mt’: ‘maltese’, ‘mi’: ‘maori’, ‘mr’: ‘marathi’, ‘mn’: ‘mongolian’, ‘my’: ‘myanmar (burmese)’, ‘ne’: ‘nepali’, ‘no’: ‘norwegian’, ‘or’: ‘odia’, ‘ps’: ‘pashto’, ‘fa’: ‘persian’, ‘pl’: ‘polish’, ‘pt’: ‘portuguese’, ‘pa’: ‘punjabi’, ‘ro’: ‘romanian’, ‘ru’: ‘russian’, ‘sm’: ‘samoan’, ‘gd’: ‘scots gaelic’, ‘sr’: ‘serbian’, ‘st’: ‘sesotho’, ‘sn’: ‘shona’, ‘sd’: ‘sindhi’, ‘si’: ‘sinhala’, ‘sk’: ‘slovak’, ‘sl’: ‘slovenian’, ‘so’: ‘somali’, ‘es’: ‘spanish’, ‘su’: ‘sundanese’, ‘sw’: ‘swahili’, ‘sv’: ‘swedish’, ‘tg’: ‘tajik’, ‘ta’: ‘tamil’, ‘te’: ‘telugu’, ‘th’: ‘thai’, ‘tr’: ‘turkish’, ‘uk’: ‘ukrainian’, ‘ur’: ‘urdu’, ‘ug’: ‘uyghur’, ‘uz’: ‘uzbek’, ‘vi’: ‘vietnamese’, ‘cy’: ‘welsh’, ‘xh’: ‘xhosa’, ‘yi’: ‘yiddish’, ‘yo’: ‘yoruba’, ‘zu’: ‘zulu’}
1.2 Python googletrans检测语言
from googletrans import Translator
text1 = '''
你好,世界!
'''
text2 = '''
hello, world!
'''
# 导入Translator,用于进行翻译
# 但是需要设定服务连接service_urls为中文谷歌翻译网站
translator = Translator(service_urls=['translate.google.cn'])
# 我们确定文本的语言,detec()并将结果打印到控制台。该方法将打印语言和置信度值,这是对语言进行正确猜测的概率。
dt1 = translator.detect(text1)
print(dt1)
dt2 = translator.detect(text2)
print(dt2)
Detected(lang=zh-CN, confidence=1.0)
Detected(lang=en, confidence=0.8225234)
1.3 python googletrans简单翻译
用该Translator’s translate()方法完成翻译。默认翻译为英文。
from googletrans import Translator
translator = Translator(service_urls=['translate.google.cn'])
translated = translator.translate('你好,世界!')
print(translated.text)
Hello world!
1.4 源语言和目标语言指定
在下面的示例中,我们指定源语言和目标语言。
from googletrans import Translator
translator = Translator(service_urls=['translate.google.cn'])
# src为源语言,dest为目标语言,通过googletrans.LANGUAGES查看语言简写
translated = translator.translate('hello, world!', src='en', dest='zh-tw')
print(translated.text)
你好,世界!
1.5 Python googletrans翻译列表
#!/usr/bin/env python
from googletrans import Translator
translator = Translator(service_urls=['translate.google.cn'])
data = ['hello world!', '你好 世界!', '今天你吃了吗?']
# 简体中文翻译为英文
translated = translator.translate(data, src='zh-tw', dest='en')
for trans in translated:
print(f'{trans.origin} -> {trans.text}')
hello world! -> hello world!
你好 世界! -> Hello world!
今天你吃了吗? -> Did you eat it today?
2 参考
https://py-googletrans.readthedocs.io/en/latest/#googletrans.Translator.translate
本文来自博客园,作者:落痕的寒假,转载请注明原文链接:https://www.cnblogs.com/luohenyueji/p/16970260.html
![](https://gitcode.net/LuohenYJ/article_picture_warehouse/-/raw/main/wechat/content/%E5%8A%A0%E6%B2%B9%E9%B8%AD.gif)