Skip to main content

Simple and Elegant Python3 Internationalization (i18n) Tool

Project description

icon

Simple and Elegant Python3 Internationalization (i18n) Tool

PyPI version

English | 中文 | 日本語

🌍 Easy AI18n

Easy AI18n is a modern internationalization tool library for Python3. It supports AI translation, multi-user scenarios, and full string formatting syntax, making globalization of your project more elegant and natural.

✨ Key Features:

  • 🚀 Easy to Use: Implement i18n with just a few lines of code
  • ✨ Elegant Syntax: Use _() to wrap translatable texts, seamlessly integrating into your code
  • 🤖 AI Translation: Supports translation using large language models (LLMs) for high-quality results
  • 📝 Full Formatting Support: Fully supports all Python string formatting syntaxes
  • 🌐 Multi-language Support: Choose languages using [] selector for multilingual support

🔍 Comparison with Other i18n Tools

Other i18n Tools EasyAI18n

Requires manual maintenance of keys and i18n files, high development cost

Automatically extracts translation content, no manual file maintenance needed

Supports only partial formatting syntax

Fully supports all formatting syntax

No real-time multi-language switching, unsuitable for multi-user scenarios

Supports default language and multi-language switching, adaptable to multi-user environments

⚡ Quick Start

📦 Installation

pip install easy-ai18n

🧪 Simple Example

from easy_ai18n import EasyAI18n

i18n = EasyAI18n(target_lang=["ru", "ja", 'zh-CN'])
i18n.build()

_ = i18n.t()

print(_("Hello, world!")['zh-CN'])

🗂️ Project Structure

easy_ai18n
├── core                 # Core functionality module
│   ├── builder.py       # Builder: extract, translate, generate YAML files
│   ├── i18n.py          # Main translation logic
│   ├── loader.py        # Loader: load translation files
│   └── parser.py        # AST parser
├── prompts              # Translation prompts
├── translator           # Translator module
└── main.py              # Project entry point

📘 Usage Tutorial

⚙️ Initialize EasyAI18n Instance

from easy_ai18n import EasyAI18n, PreLanguageSelector, PostLanguageSelector
from easy_ai18n.translator import GoogleTranslator

# Initialize EasyAI18n instance
i18n = EasyAI18n(
    global_lang="zh",  # Global default language
    target_lang=["zh", "ja"],  # Target translation languages
    languages=["zh", "ja"],  # Enabled languages (default is target_lang)
    project_dir="/path/to/your/project",  # Root directory (default is current dir)
    include=[],  # Included files/directories
    exclude=[".idea"],  # Excluded files/directories
    i18n_file_dir="i18n",  # Directory to store translation files
    func_name=["_"],  # Translation function names (supports multiple)
    sep=" ",  # Separator (default is space)
    translator=GoogleTranslator(),  # Translator (default is Google)
    pre_lang_selector=PreLanguageSelector,  # Pre language selector
    post_lang_selector=PostLanguageSelector  # Post language selector
)

# Build translation files
i18n.build()

# Set translation function, here we use _, can be customized
_ = i18n.t()

# Put strings to be translated inside the function
print(_("Hello, world!"))

🛠️ Custom Translation Function Names

from easy_ai18n import EasyAI18n

i18n = EasyAI18n(
    func_name=["_t", '_']  # Custom translation function names
)

_t = i18n.t()
_ = _t

print(_t("Hello, world!"))
print(_("Hello, world!"))

🤖 Use AI for Translation

from easy_ai18n import EasyAI18n
from easy_ai18n.translator import OpenAIYAMLTranslator

translator = OpenAIYAMLTranslator(api_key=..., base_url=..., model='gpt-4o-mini')

i18n = EasyAI18n(target_lang=["ru", "ja", 'zh-CN'], translator=translator)
i18n.build()

_ = i18n.t()

print(_("Hello, world!")['zh-CN'])

👥 Multi-user Language Scenarios (e.g. Telegram Bot)

Use custom language selector to dynamically select languages in multi-user environments:

from pyrogram import Client
from pyrogram.types import Message

from easy_ai18n import EasyAI18n, PostLanguageSelector


class MyPostLanguageSelector(PostLanguageSelector):
    def __getitem__(self, msg: Message):
        # Get user's language
        lang = msg.from_user.language_code
        return super().__getitem__(lang)


i18n = EasyAI18n(
    target_lang=['zh', 'ru'],
    post_lang_selector=MyPostLanguageSelector,
)
_ = i18n.t()

bot = Client("my_bot")


@bot.on_message()
async def start(__, msg: Message):
    await msg.reply(_[msg]("Hello, world!"))


if __name__ == "__main__":
    bot.loop.run_until_complete(i18n.build_async())
    bot.run()

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

easy_ai18n-0.0.11.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

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

easy_ai18n-0.0.11-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file easy_ai18n-0.0.11.tar.gz.

File metadata

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

File hashes

Hashes for easy_ai18n-0.0.11.tar.gz
Algorithm Hash digest
SHA256 4ca5822cdc0d6a7e332a2c1155a033ce1e8d90e8de030366067ccd547af468de
MD5 3bec8437f5dd01d8ccf46d08eb428a62
BLAKE2b-256 4c25920dd9f79f2418334cc9cbe11d3ee2106eeafb17a26dd93e0e06b52b8f5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_ai18n-0.0.11.tar.gz:

Publisher: python-publish.yml on z-mio/easy-ai18n

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

File details

Details for the file easy_ai18n-0.0.11-py3-none-any.whl.

File metadata

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

File hashes

Hashes for easy_ai18n-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 6d29f85fe2dd498bf540d42c6ef2167883f3052b29800c9574ab0996d09b29fb
MD5 f4295d80a9b8c4cc56575558777adc25
BLAKE2b-256 71b70c0b25abc869537ebf71f6f1f4553b84e8f6a1614b09678fc8650185c167

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_ai18n-0.0.11-py3-none-any.whl:

Publisher: python-publish.yml on z-mio/easy-ai18n

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