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 (Functional API)
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: さようなら!
Type-Safe Class-Based API (Recommended)
For better type safety and IDE support, use the class-based API:
from kiarina.i18n import I18n, get_i18n, settings_manager
# Define your i18n class
class AppI18n(I18n):
scope: str = "app.greeting"
hello: str = "Hello, $name!"
goodbye: str = "Goodbye!"
welcome: str = "Welcome to our app!"
# Configure the catalog
settings_manager.user_config = {
"catalog": {
"ja": {
"app.greeting": {
"hello": "こんにちは、$name!",
"goodbye": "さようなら!",
"welcome": "アプリへようこそ!"
}
}
}
}
# Get translated instance
t = get_i18n(AppI18n, "ja")
# Access translations with full type safety and IDE completion
print(t.hello) # Output: こんにちは、$name!
print(t.goodbye) # Output: さようなら!
print(t.welcome) # Output: アプリへようこそ!
# Template variables are handled by the functional API
from kiarina.i18n import get_translator
translator = get_translator("ja", "app.greeting")
print(translator("hello", name="World")) # Output: こんにちは、World!
Benefits of Class-Based API:
- Type Safety: IDE detects typos in field names
- Auto-completion: IDE suggests available translation keys
- Self-documenting: Class definition serves as documentation
- Default Values: Explicit fallback values when translation is missing
- Immutable: Translation instances are frozen and cannot be modified
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
Class-Based API
I18n
Base class for defining i18n translations with type safety.
Usage:
from kiarina.i18n import I18n
class MyI18n(I18n):
scope: str = "my.module"
title: str = "Default Title"
description: str = "Default Description"
Features:
- Immutable: Instances are frozen and cannot be modified
- Type-safe: Full type hints and validation
- Self-documenting: Field names are translation keys, field values are defaults
get_i18n(i18n_class: type[T], language: str) -> T
Get a translated i18n instance.
Parameters:
i18n_class: I18n class to instantiate (not instance!)language: Target language code (e.g., "en", "ja")
Returns:
- Translated i18n instance with all fields translated
Example:
from kiarina.i18n import I18n, get_i18n
class AppI18n(I18n):
scope: str = "app"
title: str = "My App"
t = get_i18n(AppI18n, "ja")
print(t.title) # Translated title
Functional API
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 translationslanguage: Target language for translationscope: Scope for translation keysfallback_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
- Primary lookup: Searches for the key in the target language
- Fallback lookup: If not found, searches in the fallback language
- Default value: If still not found, uses the provided default value
- 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 catalogcatalog(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.0pydantic-settings>=2.0.0pydantic-settings-manager>=2.3.0pyyaml>=6.0.0
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- kiarina-python - Parent monorepo containing all kiarina packages
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kiarina_i18n-1.17.0.tar.gz.
File metadata
- Download URL: kiarina_i18n-1.17.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca3684e23d57d521764b109a02f0bba646b50327d0415ea0d8ffe104b92a8a28
|
|
| MD5 |
fe943a76e88d85f92e5b2519c8547903
|
|
| BLAKE2b-256 |
4cc17ab6b6f0b073132d6d3abc497e170f7ead848d0ccdc209c54b5356f4ee81
|
File details
Details for the file kiarina_i18n-1.17.0-py3-none-any.whl.
File metadata
- Download URL: kiarina_i18n-1.17.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f0caa5c00ca354dd062cd0c9dcd0a6325ad8e4249ce2342b34955056e6af00b
|
|
| MD5 |
83ac38874ff6e321552debb01acd29f5
|
|
| BLAKE2b-256 |
dff85eb67ce4adccd126361ba8266900d8cb1bb19f124f0e352f3fef268ca9f5
|