Skip to main content

Simple and Elegant Python3 Internationalization (i18n) Tool

Project description

icon

Simple and Elegant Python3 Internationalization (i18n) Tool

Python GitHub stars GitHub forks PyPI version GitHub License

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

/i18n.py

from easy_ai18n import EasyAI18n

i18n = EasyAI18n()

_ = i18n.i18n()

if __name__ == "__main__":
    i18n.build(["ja"])

/main.py

from i18n import _


def main():
    print(_("Hello, world!")['ja'])


if __name__ == "__main__":
    main()

🗂️ 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

🛠️ Custom Translation Function Names

from easy_ai18n import EasyAI18n

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

_t = i18n.i18n()
_ = _t

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

🤖 Use AI for Translation

from easy_ai18n import EasyAI18n
from easy_ai18n.translator import OpenAIBulkTranslator

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

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

_ = i18n.i18n()

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

🔎 Language Selector

from easy_ai18n import EasyAI18n

i18n = EasyAI18n()
_ = i18n.i18n()
_t = _['ja']

d = {
    1: _('apple'),
    2: _('banana'),
    3: _t('orange'),
}
print(d[1]['zh-hans'])  # output: 苹果
print(d[2])  # output: banana
print(d[3])  # output: みかん

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

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

/i18n.py:

from pyrogram.types import Message
from easy_ai18n import EasyAI18n, PostLanguageSelector


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


i18n = EasyAI18n()

_ = i18n.i18n(post_lang_selector=MyPostLanguageSelector)

if __name__ == "__main__":
    i18n.build(target_lang=['en', 'ru'])

/bot.py:

from pyrogram import Client
from pyrogram.types import Message
from i18n import _

bot = Client("my_bot")


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


if __name__ == "__main__":
    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-1.0.1.tar.gz (19.2 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-1.0.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for easy_ai18n-1.0.1.tar.gz
Algorithm Hash digest
SHA256 907fa63b156790d9a7122698369f957ed1ac7a46c6d4153116326f8de99e8e7a
MD5 6e5b05f137308238ffa2a87c97c5f89c
BLAKE2b-256 4346de0b4b6935bccfe107b09f80f5ce54b2d6fc6bb3fb946a170c6f4ec841d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_ai18n-1.0.1.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-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: easy_ai18n-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 19.3 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-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a9f74fe7bf1a3799b25c6622868b18b02f0833d6ba9a2a678b18a658de6a290
MD5 c09f3fde61a754a94e94ffa2e703c205
BLAKE2b-256 a36c95902df477f4e5e40339a0e41dea1b97ece3d904d4cd477d007811862298

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_ai18n-1.0.1-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