一、前言
在python中,有个多语种翻译包,可以将各种语言进行翻译转换,例如英语转中文,韩语转中文,日语转韩语等,这个包便是translate,而且使用方便,翻译准确率还可以!
二、安装使用
-
首先需要安装translate包
pip install translate
-
然后使用在程序中使用
# 安装
# pip install translatefrom translate import Translatorprint(Translator(from_lang="Chinese",to_lang="English").translate('你好'))
print(Translator(from_lang="ZH",to_lang="EN-US").translate('你好'))print(Translator(from_lang="Chinese",to_lang="Japanese").translate('你好'))
print(Translator(from_lang="Japanese",to_lang="Chinese").translate('こんにちは'))
print(Translator(from_lang="ZH",to_lang="JA").translate('你好'))#Hello
#Hello
#こんにちは
#こんにちは
例如:
from_lang | to_lang |
---|---|
english | chinese |
i need you | 我需要你 |
Japanese | chinese |
今日の天気はどうですか? | 今天天气怎么样? |
Japanese | Korean |
今日の天気はどうですか? | 오늘 날씨는 어떻습니까? |
注意:如果不指定from_lang属性,则默认为english,to_lang属性必须指定一种输出语言,否则会报错。
三、扩展-支持语言类型
该包使用 https://www.deepl.com/ 提供的API,相关信息:
- 介绍:https://www.deepl.com/zh/docs-api/introduction/
- GitHub仓库:https://github.com/DeepLcom/deepl-python
- 有免费和付费版:免费版应该是有具体的用量和字符数限制(具体未知),付费版是按量付费的。
支持语言类型:详见:https://www.deepl.com/zh/docs-api/other-functions/listing-supported-languages/
[{"language": "BG","name": "Bulgarian","supports_formality":false},{"language": "CS","name": "Czech","supports_formality":false},{"language": "DA","name": "Danish","supports_formality":false},{"language": "DE","name": "German","supports_formality":true},{"language": "EL","name": "Greek","supports_formality":false},{"language": "EN-GB","name": "English (British)","supports_formality":false},{"language": "EN-US","name": "English (American)","supports_formality":false},{"language": "ES","name": "Spanish","supports_formality":true},{"language": "ET","name": "Estonian","supports_formality":false},{"language": "FI","name": "Finnish","supports_formality":false},{"language": "FR","name": "French","supports_formality":true},{"language": "HU","name": "Hungarian","supports_formality":false},{"language": "IT","name": "Italian","supports_formality":true},{"language": "JA","name": "Japanese","supports_formality":false},{"language": "LT","name": "Lithuanian","supports_formality":false},{"language": "LV","name": "Latvian","supports_formality":false},{"language": "NL","name": "Dutch","supports_formality":true},{"language": "PL","name": "Polish","supports_formality":true},{"language": "PT-BR","name": "Portuguese (Brazilian)","supports_formality":true},{"language": "PT-PT","name": "Portuguese (European)","supports_formality":true},{"language": "RO","name": "Romanian","supports_formality":false},{"language": "RU","name": "Russian","supports_formality":true},{"language": "SK","name": "Slovak","supports_formality":false},{"language": "SL","name": "Slovenian","supports_formality":false},{"language": "SV","name": "Swedish","supports_formality":false},{"language": "ZH","name": "Chinese","supports_formality":false}
]
四、总结
python好用的库有很多,需要我们在实战中慢慢积累,我们也可以学习一下别人编写好的库实现的逻辑是怎样的,阅读源代码有助于我们原生coding!
参考:https://blog.csdn.net/qq_38796548/article/details/108095533