Skip to main content

easy using AI, more texting models!

Project description

AI Translator

A powerful Python library for accessing multiple translation services through a unified API.

Table of Contents

Installation

pip install aitranslator

Quick Start

from aitranslator 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 translate
  • target (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}")

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

translatorai-1.0.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

translatorai-1.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file translatorai-1.0.0.tar.gz.

File metadata

  • Download URL: translatorai-1.0.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for translatorai-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5cc04d79b1a0b04aab4562438ad264c4eb5854637acabf02a737346e3bf53e24
MD5 631ad8768cfec6eb7f1aae8a6a017d31
BLAKE2b-256 6043f485d3c1706aab370d45b2337ea42d375e4e1854cdb72dd0ad075a8f166b

See more details on using hashes here.

File details

Details for the file translatorai-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: translatorai-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for translatorai-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 747b410e1db42435cc47332bdac401f9e6b7539082d4be3da5255237b6791088
MD5 c3c4a7b640550e6ea41e9119dfe4ba0e
BLAKE2b-256 54d68a9aca04f3b13daba25cd54b8d983422886604762c1e55d8c849f4c66fd0

See more details on using hashes here.

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