AI Translator
A powerful Python library for accessing multiple translation services through a unified API.
Table of Contents
- Installation
- Quick Start
- Core Features
- Translation Services
- Language Detection
- API Reference
- Examples
- Error Handling
- Limitations
Installation
pip install translatorai
Quick Start
from translatorai import Translator
# Initialize the translator
translator = Translator()
# Translate text from auto-detected language to English
result = translator.google("Привет, мир!", "en")
print(result['response']['translated_text']) # Output: "Hello, world!"
Core Features
- Access to 5 different translation services via a unified API
- Automatic language detection
- Support for specific source and target languages
- Error handling with appropriate HTTP status codes
- JSON response format for easy integration
Translation Services
The library provides access to the following translation engines:
| Method | Service | Description |
|---|---|---|
google() |
Google Translate | Google's neural machine translation service |
deepl() |
DeepL | Highly accurate translation service known for natural-sounding results |
amazon() |
Amazon Translate | Amazon Web Services' neural machine translation service |
modern_mt() |
ModernMT | Adaptive neural machine translation |
libre() |
LibreTranslate | Open-source machine translation |
Language Detection
The library includes automatic language detection which can be used both as a standalone feature and as part of the translation process.
# Detect language
detection = translator.detect("гЃ“г‚“гЃ«гЃЎгЃЇ")
print(detection["data"]["language_probability"]["code"]) # Output: "ja"
API Reference
Translator Class
Translator()
Initializes a new translator instance.
Translation Methods
Each translation method follows the same signature:
def method_name(text, target, source='auto')
text(str): The text to translatetarget(str): Target language code (e.g., "en", "fr", "de")source(str, optional): Source language code. Defaults to 'auto' for automatic detection.
Returns a JSON object with translation results.
Available Methods
Google Translate
translator.google(text, target, source='auto')
DeepL
translator.deepl(text, target, source='auto')
Amazon Translate
translator.amazon(text, target, source='auto')
ModernMT
translator.modern_mt(text, target, source='auto')
LibreTranslate
translator.libre(text, target, source='auto')
Language Detection
translator.detect(text)
text(str): The text to analyze (only first 100 characters are used)
Returns a JSON object with detected language information.
Examples
Translating with Different Services
translator = Translator()
# Original text
text = "La vie est belle"
# Translate with different services
google_result = translator.google(text, "en", "fr")
deepl_result = translator.deepl(text, "en", "fr")
amazon_result = translator.amazon(text, "en", "fr")
print(f"Google: {google_result['response']['translated_text']}")
print(f"DeepL: {deepl_result['response']['translated_text']}")
print(f"Amazon: {amazon_result['response']['translated_text']}")
Using Auto-Detection
# The source language will be automatically detected
result = translator.google("Guten Tag", "en")
print(result['response']['translated_text']) # Output: "Good day"
Error Handling
The library uses the raise_for_status() method from the requests library to handle HTTP errors. If the translation service returns a non-200 response, an exception will be raised.
try:
result = translator.google("Hello world", "invalid_language_code")
except requests.exceptions.HTTPError as e:
print(f"An error occurred: {e}")
Metadata
Release files for translatorai 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| translatorai-1.0.1.tar.gz | 4.1 kB | Without provenance | Details for translatorai-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| translatorai-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for translatorai-1.0.1-py3-none-any.whl |
Total release size: 8.4 kB
Release files / translatorai-1.0.1.tar.gz
| Download URL | translatorai-1.0.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39b9a3b156aeeaed1e075fac31ba5a2f543fadc3cacc020823f9408f7675749e
|
|
BLAKE2b-256 checksum How to use checksums |
4c24ffe1a839bc0ca4803b01294f9cd29c7a86f2b7d27751b04a3ed45c835dd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.0
|
Release files / translatorai-1.0.1-py3-none-any.whl
| Download URL | translatorai-1.0.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
186df29950dbcb58775ed73cffc7f7f0cb82f0aa46a57c433c16b03731a4ef47
|
|
BLAKE2b-256 checksum How to use checksums |
180815e9e57114b609603efdf25fa671aaad611d6d4d6c3ef567f2cda3d2ec45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.0
|
Security for translatorai 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.