Skip to main content

一个集成国内外翻译服务的第三方库,方便开发者快速集成翻译功能

Project description

translation-hub

没有任何依赖的翻译库,API 统一简单易用,适合快速集成翻译功能而不想引入庞大的依赖库。

  • 简单易用
  • 支持多种翻译服务
  • 仅使用标准库,连 requests 都没有,非常适合快速集成
  • 屏蔽各大翻译api语言代码,内置了常用语言的映射
  • 支持多种秘钥传入方式,无需每次都输入(直接传入,本地存储,环境变量)
  • 更加直观的自定义报错类型提示(翻译服务报错,秘钥错误等)
  • 详细的代码文档(包括免费额度, 如何开通和文档地址等)
  • 完整的单元测试

目前支持的国内翻译服务(均为有免费额度):

  • 百度翻译
  • 有道翻译
  • 腾讯翻译
  • 阿里翻译

目前支持的国外翻译服务:

  • deepl翻译
  • 支持谷歌翻译(无需秘钥)

Installation 安装说明

Install the latest translation-hub version with:

pip install translation-hub

QuickStart 快速开始

只需要定义一个翻译器对象,传入秘钥,然后调用 translate 方法即可。

同时支持直接传入,本地存储秘钥以及从环境变量获取秘钥

from translation_hub import BaiduAPI

translator = BaiduAPI("your appid", "secret_key")
result = translator.translate("hello")

print(result)  # 你好

自定义翻译语言,使用 Languages 枚举类来实现屏蔽各大翻译api语言代码

from translation_hub import GoogleApi, Languages

# 谷歌无需传入秘钥
translator = GoogleApi()

# 自动检测后翻译成中文
print(translator.translate("hello", Languages.Auto, Languages.Chinese))  # 你好

# 手动说明语言,然后翻译成日文
print(translator.translate("hello", Languages.English, Languages.Japanese))  # こんにちは

Supported Languages 支持的语言

每一个翻译 API 对应语言的缩写形式不同,比如简体中文在百度翻译中是 zh,在谷歌翻译中是 zh-CN,在有道翻译中是 zh-CHS 。为了方便使用,我定义了一个枚举类 Languages 来屏蔽这些细节,直接使用枚举类即可。

目前支持的通用语言如下,我为这些语言定义了一个枚举类 Languages 来屏蔽各大翻译api的语言代码:

  • 中文 Language.Chinese
  • 英语 Language.English
  • 日语 Language.Japanese
  • 韩语 Language.Korea
  • 俄语 Language.Russia
  • 自动选择 Language.Auto

如果您需要其他语言,可以自行前往查看每一个翻译服务的支持语言,然后手动传入语言代码即可。比如

from translation_hub import DeeplApi

deepl = DeeplApi("your api key")

# 翻译成德语
print(deepl.translate("hello", "EN", "DE"))  # hallo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

translation_hub-0.2.15.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

translation_hub-0.2.15-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

Details for the file translation_hub-0.2.15.tar.gz.

File metadata

  • Download URL: translation_hub-0.2.15.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for translation_hub-0.2.15.tar.gz
Algorithm Hash digest
SHA256 2ec7944da7d80407a9a337f5c98799c39cdc490f60703424c21f710dc8cc5ab0
MD5 c84e1bfc97c3fd3456fa7e5f4501cb0f
BLAKE2b-256 3bcb0c391f601605e38c3119d1f24bd79938b2730f4ee2504dae2f4e33fc44ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for translation_hub-0.2.15.tar.gz:

Publisher: python-publish.yml on 271374667/translation_hub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file translation_hub-0.2.15-py3-none-any.whl.

File metadata

File hashes

Hashes for translation_hub-0.2.15-py3-none-any.whl
Algorithm Hash digest
SHA256 3d3b6ffe3d4f764f6917f53916704008d544e34c5bd88881dd3c3353aa60d956
MD5 00c65226a6a4eb4c5254d158e7a73535
BLAKE2b-256 eccdbfd17ba25f5b07b9d7bd296484b81e58ae1952da1de3dee3476b341aad07

See more details on using hashes here.

Provenance

The following attestation bundles were made for translation_hub-0.2.15-py3-none-any.whl:

Publisher: python-publish.yml on 271374667/translation_hub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page