V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 5 小时 24 分钟前 · 211 次点击

    目前已有 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!"))
    
    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1661 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 16:20 · PVG 00:20 · LAX 09:20 · JFK 12:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.