Skip to main content

Simple internationalization (i18n) utilities for Python applications

Project description

kiarina-i18n

Simple internationalization (i18n) utilities for Python applications.

Purpose

kiarina-i18n provides a lightweight and straightforward approach to internationalization in Python applications. It focuses on simplicity and predictability, avoiding complex grammar rules or plural forms. For applications requiring advanced features like plural forms or complex localization, consider using established tools like gettext.

Installation

pip install kiarina-i18n

Quick Start

Basic Usage

from kiarina.i18n import get_translator, settings_manager

# Configure the catalog
settings_manager.user_config = {
    "catalog": {
        "en": {
            "app.greeting": {
                "hello": "Hello, $name!",
                "goodbye": "Goodbye!"
            }
        },
        "ja": {
            "app.greeting": {
                "hello": "こんにちは、$name!",
                "goodbye": "さようなら!"
            }
        }
    }
}

# Get a translator
t = get_translator("ja", "app.greeting")

# Translate with template variables
print(t("hello", name="World"))  # Output: こんにちは、World!
print(t("goodbye"))  # Output: さようなら!

Using Catalog File

from kiarina.i18n import get_translator, settings_manager

# Load catalog from YAML file
settings_manager.user_config = {
    "catalog_file": "i18n_catalog.yaml"
}

t = get_translator("en", "app.greeting")
print(t("hello", name="Alice"))

Example i18n_catalog.yaml:

en:
  app.greeting:
    hello: "Hello, $name!"
    goodbye: "Goodbye!"
ja:
  app.greeting:
    hello: "こんにちは、$name!"
    goodbye: "さようなら!"

API Reference

get_translator(language: str, scope: str) -> Translator

Get a translator for the specified language and scope.

Parameters:

  • language: Target language code (e.g., "en", "ja", "fr")
  • scope: Translation scope (e.g., "app.greeting", "app.error")

Returns:

  • Translator: Translator instance configured for the specified language and scope

Example:

t = get_translator("ja", "app.greeting")

Translator(catalog, language, scope, fallback_language="en")

Translator class for internationalization support.

Parameters:

  • catalog: Translation catalog mapping languages to scopes to keys to translations
  • language: Target language for translation
  • scope: Scope for translation keys
  • fallback_language: Fallback language when translation is not found (default: "en")

Methods:

  • __call__(key, default=None, **kwargs): Translate a key with optional template variables

Example:

from kiarina.i18n import Translator

catalog = {
    "en": {"app.greeting": {"hello": "Hello, $name!"}},
    "ja": {"app.greeting": {"hello": "こんにちは、$name!"}}
}

t = Translator(catalog=catalog, language="ja", scope="app.greeting")
print(t("hello", name="World"))  # Output: こんにちは、World!

Translation Behavior

  1. Primary lookup: Searches for the key in the target language
  2. Fallback lookup: If not found, searches in the fallback language
  3. Default value: If still not found, uses the provided default value
  4. Error handling: If no default is provided, returns "{scope}#{key}" and logs an error

Configuration

Using pydantic-settings-manager

# config.yaml
kiarina.i18n:
  default_language: "en"
  catalog:
    en:
      app.greeting:
        hello: "Hello, $name!"
    ja:
      app.greeting:
        hello: "こんにちは、$name!"
from pydantic_settings_manager import load_user_configs
import yaml

with open("config.yaml") as f:
    config = yaml.safe_load(f)

load_user_configs(config)

Settings Fields

  • default_language (str): Default language to use when translation is not found (default: "en")
  • catalog_file (str | None): Path to YAML file containing translation catalog
  • catalog (dict): Translation catalog mapping languages to scopes to keys to translations

Testing

# Run tests
pytest

# Run tests with coverage
pytest --cov=kiarina.i18n --cov-report=html

Dependencies

  • pydantic>=2.0.0
  • pydantic-settings>=2.0.0
  • pydantic-settings-manager>=2.3.0
  • pyyaml>=6.0.0

License

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

Related Projects

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

kiarina_i18n-1.16.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

kiarina_i18n-1.16.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_i18n-1.16.0.tar.gz.

File metadata

  • Download URL: kiarina_i18n-1.16.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_i18n-1.16.0.tar.gz
Algorithm Hash digest
SHA256 0227f4fd07c949b6a381d71e979542499cee05bd1b1c1a3274e94f0bb7692c4e
MD5 781597b4a093cab30981c7fb3b3c3533
BLAKE2b-256 4e0d91d702c8137afd9b614a793e4bce05c04f197d02caa739d55bb04bed90ab

See more details on using hashes here.

File details

Details for the file kiarina_i18n-1.16.0-py3-none-any.whl.

File metadata

  • Download URL: kiarina_i18n-1.16.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_i18n-1.16.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb8fa5658c459c57c371ad4d94288688ee9f22d13be531bc761b37572a0080af
MD5 aee172d32660084a65187ecbeebe0323
BLAKE2b-256 fed9fb5eacf139146299d7e62227e99ef35f90bc4a914c8cf8b0ab5150b53cba

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