Skip to main content

Lingo.dev Python SDK

Project description

Lingo.dev Python SDK

💬 Join our Discord community for support, discussions, and updates!

PyPI version Python support License Tests Coverage

A powerful Python SDK for the Lingo.dev localization platform. This SDK provides easy-to-use methods for localizing various content types including plain text, objects, and chat sequences.

Features

  • 🌍 Multiple Content Types: Localize text, objects, and chat sequences
  • 🚀 Batch Processing: Efficient handling of large content with automatic chunking
  • 🔄 Progress Tracking: Optional progress callbacks for long-running operations
  • 🎯 Language Detection: Automatic language recognition
  • 📊 Fast Mode: Optional fast processing for larger batches
  • 🛡️ Type Safety: Full type hints and Pydantic validation
  • 🧪 Well Tested: Comprehensive test suite with high coverage
  • 🔧 Easy Configuration: Simple setup with minimal configuration required

Installation

pip install lingodotdev

Quick Start

from lingodotdev import LingoDotDevEngine

# Initialize the engine
engine = LingoDotDevEngine({
    'api_key': 'your-api-key-here'
})

# Localize a simple text
result = engine.localize_text(
    "Hello, world!",
    {
        'source_locale': 'en',
        'target_locale': 'es'
    }
)
print(result)  # "¡Hola, mundo!"

# Localize an object
data = {
    'greeting': 'Hello',
    'farewell': 'Goodbye',
    'question': 'How are you?'
}

result = engine.localize_object(
    data,
    {
        'source_locale': 'en',
        'target_locale': 'fr'
    }
)
print(result)
# {
#     'greeting': 'Bonjour',
#     'farewell': 'Au revoir',
#     'question': 'Comment allez-vous?'
# }

API Reference

LingoDotDevEngine

Constructor

engine = LingoDotDevEngine(config)

Parameters:

  • config (dict): Configuration dictionary with the following options:
    • api_key (str, required): Your Lingo.dev API key

Methods

localize_text(text, params, progress_callback=None)

Localize a single text string.

Parameters:

  • text (str): The text to localize
  • params (dict): Localization parameters
    • source_locale (str): Source language code (e.g., 'en')
    • target_locale (str): Target language code (e.g., 'es')
  • progress_callback (callable): Progress callback function

Returns: str - The localized text

Example:

result = engine.localize_text(
    "Welcome to our application",
    {
        'source_locale': 'en',
        'target_locale': 'es'
    }
)

localize_object(obj, params, progress_callback=None)

Localize a Python dictionary with string values.

Parameters:

  • obj (dict): The object to localize
  • params (dict): Localization parameters (same as localize_text)
  • progress_callback (callable): Progress callback function

Returns: dict - The localized object with the same structure

Example:

def progress_callback(progress, source_chunk, processed_chunk):
    print(f"Progress: {progress}%")

result = engine.localize_object(
    {
        'title': 'My App',
        'description': 'A great application',
        'button_text': 'Click me'
    },
    {
        'source_locale': 'en',
        'target_locale': 'de'
    },
    progress_callback=progress_callback
)

batch_localize_text(text, params)

Localize a text string to multiple target languages.

Parameters:

  • text (str): The text to localize
  • params (dict): Batch localization parameters
    • source_locale (str): Source language code
    • target_locales (list): List of target language codes

Returns: list - List of localized strings in the same order as target_locales

Example:

results = engine.batch_localize_text(
    "Welcome to our platform",
    {
        'source_locale': 'en',
        'target_locales': ['es', 'fr', 'de', 'it']
    }
)

localize_chat(chat, params, progress_callback=None)

Localize a chat conversation while preserving speaker names.

Parameters:

  • chat (list): List of chat messages with name and text keys
  • params (dict): Localization parameters (same as localize_text)
  • progress_callback (callable, optional): Progress callback function

Returns: list - Localized chat messages with preserved structure

Example:

chat = [
    {'name': 'Alice', 'text': 'Hello everyone!'},
    {'name': 'Bob', 'text': 'How are you doing?'},
    {'name': 'Charlie', 'text': 'Great, thanks for asking!'}
]

result = engine.localize_chat(
    chat,
    {
        'source_locale': 'en',
        'target_locale': 'es'
    }
)

recognize_locale(text)

Detect the language of a given text.

Parameters:

  • text (str): The text to analyze

Returns: str - The detected language code (e.g., 'en', 'es', 'fr')

Example:

locale = engine.recognize_locale("Bonjour, comment allez-vous?")
print(locale)  # 'fr'

whoami()

Get information about the current API key.

Returns: dict or None - User information with 'email' and 'id' keys, or None if not authenticated

Example:

user_info = engine.whoami()
if user_info:
    print(f"Authenticated as: {user_info['email']}")
else:
    print("Not authenticated")

Error Handling

The SDK raises the following exceptions:

  • ValueError: For invalid input parameters
  • RuntimeError: For API errors and network issues
  • pydantic.ValidationError: For configuration validation errors

Example:

try:
    result = engine.localize_text(
        "Hello world",
        {'target_locale': 'es'}  # Missing source_locale
    )
except ValueError as e:
    print(f"Invalid parameters: {e}")
except RuntimeError as e:
    print(f"API error: {e}")

Advanced Usage

Using Reference Translations

You can provide reference translations to improve consistency:

reference = {
    'es': {
        'greeting': 'Hola',
        'app_name': 'Mi Aplicación'
    },
    'fr': {
        'greeting': 'Bonjour',
        'app_name': 'Mon Application'
    }
}

result = engine.localize_object(
    {
        'greeting': 'Hello',
        'app_name': 'My App',
        'welcome_message': 'Welcome to My App'
    },
    {
        'source_locale': 'en',
        'target_locale': 'es',
        'reference': reference
    }
)

Progress Tracking

For long-running operations, you can track progress:

def progress_callback(progress, source_chunk, processed_chunk):
    print(f"Progress: {progress}%")
    print(f"Processing: {len(source_chunk)} items")
    print(f"Completed: {len(processed_chunk)} items")

# Large dataset that will be processed in chunks
large_data = {f"key_{i}": f"Text content {i}" for i in range(1000)}

result = engine.localize_object(
    large_data,
    {
        'source_locale': 'en',
        'target_locale': 'es'
    },
    progress_callback=progress_callback
)

Development

Setup

git clone https://github.com/lingodotdev/sdk-python.git
cd sdk-python
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src/lingo_dev_sdk --cov-report=html

# Run only unit tests
pytest tests/test_engine.py

# Run integration tests (requires API key)
export LINGO_DEV_API_KEY=your-api-key
pytest tests/test_integration.py

Code Quality

# Format code
black .

# Lint code
flake8 .

# Type checking
mypy src/lingo_dev_sdk

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes and add tests
  4. Commit your changes using Conventional Commits:
    • feat: add new feature
    • fix: resolve bug
    • docs: update documentation
    • style: format code
    • refactor: refactor code
    • test: add tests
    • chore: update dependencies
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

Release Process

This project uses automated semantic releases:

  • Pull Requests: Automatically run tests and build checks
  • Main Branch: Automatically analyzes commit messages, bumps version, updates changelog, and publishes to PyPI
  • Commit Messages: Must follow Conventional Commits format
    • feat: triggers a minor version bump (0.1.0 → 0.2.0)
    • fix: triggers a patch version bump (0.1.0 → 0.1.1)
    • BREAKING CHANGE: triggers a major version bump (0.1.0 → 1.0.0)

Development Workflow

  1. Create a feature branch
  2. Make changes with proper commit messages
  3. Open a PR (triggers CI/CD)
  4. Merge to main (triggers release if applicable)
  5. Automated release to PyPI

Support

Changelog

See CHANGELOG.md for a detailed history of changes.


💬 Join our Discord community for support, discussions, and updates!

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

lingodotdev-1.0.4.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

lingodotdev-1.0.4-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file lingodotdev-1.0.4.tar.gz.

File metadata

  • Download URL: lingodotdev-1.0.4.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lingodotdev-1.0.4.tar.gz
Algorithm Hash digest
SHA256 6fc02efa48034ec65f317e1b7f48c37f079cebc04e75a9c3c84a4b38f8a487e9
MD5 bc531343d9865163459dd9e7dd5d7c8a
BLAKE2b-256 78c08bf05f1cb409d314e9dd00e1482f113463cefeb9190461f08381dea0c857

See more details on using hashes here.

Provenance

The following attestation bundles were made for lingodotdev-1.0.4.tar.gz:

Publisher: release.yml on lingodotdev/sdk-python

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

File details

Details for the file lingodotdev-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: lingodotdev-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lingodotdev-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d6d127b79d5077b3c30bdfcb469fce4cd5a01bd3770c81526ec13ff18cd9caac
MD5 76985909efd1fc99c35b72825abbb10e
BLAKE2b-256 be4ac066600bc2bf46ab54b3cbec0e767f76b2e3d4d1fe835e4205e3ff01d32b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lingodotdev-1.0.4-py3-none-any.whl:

Publisher: release.yml on lingodotdev/sdk-python

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