V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zilingmio
V2EX  ›  Python

Easy AI18n | 更好用的 Python3 i18n 库

  •  
  •   zilingmio ·
    z-mio · Sep 13, 2025 · 7379 views
    This topic created in 226 days ago, the information mentioned may be changed or developed.

    目前已有 i18n 库的痛点

    1. Python 自带的 gettext 不支持动态选择语言 (比如 Bot 需要根据用户选择语言)
    2. 其他库需要手动维护翻译字典, 开发效率低
    3. 都不支持 f-string 语法, 写起来不够优雅, 不够方便

    所以, Easy AI18n 诞生了, 解决了以上所有痛点


    Easy AI18n 是一个现代化的 Python3 国际化工具库,支持 AI 翻译、多用户场景以及完整的字符串格式化语法,让项目全球化变得更加优雅自然。

    https://github.com/z-mio/easy-ai18n


    特点

    • 自动提取内容生成翻译字典, 提高开发效率
    • 支持动态多语言, 适配多用户场景
    • 支持所有格式化语法, 代码更优雅
    • 支持 AI 翻译, 翻译质量更好

    对比

    其他 i18n 工具 EasyAI18n
    需手动维护翻译字典, 开发效率低 自动提取翻译内容,无需手动维护文件
    仅支持部分格式化语法 完全支持所有格式化语法
    不支持实时多语言切换,不适用于多用户场景 支持默认语言与多语言切换,适配多用户环境

    👥 多用户多语言场景(如 Telegram Bot )

    通过自定义语言选择器, 在多用户环境中实现动态语言选择:

    /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:

    @bot.on_message()
    async def start(__, msg: Message):
        await msg.reply(_[msg]("Hello, world!"))
    
    2 replies    2025-09-15 20:54:38 +08:00
    ruanimal
        1
    ruanimal  
       Sep 15, 2025
    下划线是 python 常用变量吧
    zilingmio
        2
    zilingmio  
    OP
       Sep 15, 2025
    @ruanimal 是, 可以自定义, 不强制下划线

    ```
    i18n = EasyAI18n(i18n_function_names=['_', '_t', 't_'])

    _t = i18n.i18n(post_lang_selector=MyPostLanguageSelector)
    _t('test')

    _ = _t
    _('test')

    t_ = _t
    t_('test')

    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4433 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:11 · PVG 12:11 · LAX 21:11 · JFK 00:11
    ♥ Do have faith in what you're doing.