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 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 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.1.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.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: translatorai-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 39b9a3b156aeeaed1e075fac31ba5a2f543fadc3cacc020823f9408f7675749e
MD5 ed94a3683dc544c4cc9613a72d5783f3
BLAKE2b-256 4c24ffe1a839bc0ca4803b01294f9cd29c7a86f2b7d27751b04a3ed45c835dd8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: translatorai-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 186df29950dbcb58775ed73cffc7f7f0cb82f0aa46a57c433c16b03731a4ef47
MD5 95eb37d9270177ffd1bf14a4afe8212f
BLAKE2b-256 180815e9e57114b609603efdf25fa671aaad611d6d4d6c3ef567f2cda3d2ec45

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