安美奇直达天下广告平台将采用“云搜索”和“微广告”技术为大家提供精确的搜索直达服务和专业的广告宣传策划方案。
mw-aiohttp-babel的Python项目详细描述
rst file editor mw_aiohttp_babel =================== 修改aiohttp_babel源码,使它能适应 maxwin团队的开发框架
aiohttp_babel adds i18n and l10n support to aiohttp.
变更:
1,增加类似flask_babel的Babel类 2,_() 直接调用 babel.support.Translations.translation,原来的_()改为了lazy_gettext()
Babel使用样例:
importaiohttp_jinja2frommw_aiohttp.webimportApplicationfrommw_aiohttp_babelimportBabel,_babel=Babel(directory='/path/to/locales',domain='messages',default_locale='en_US')# you can use your own locale detection method, if necessary.# aiohttp_babel checks cookie parameter `locale` or `Accept-Language`# header by default.# def detector(request):# if request.url.host == 'es.example.com':# return 'es'# elif request.url.host == 'zh.example.com':# return 'zh'# else:# return 'en'# set_locale_detector(detector)jinja_loader=jinja2.FileSystemLoader('./templates')app=Application()babel.init_app(app)aiohttp_jinja2.setup(app,loader=jinja_loader)jinja_env=aiohttp_jinja2.get_env(app)jinja_env.globals['_']=_
保留aiohttp_babel的样例:
importaiohttp_jinja2frommw_aiohttp.webimportApplicationfrommw_aiohttp_babel.localeimport(load_gettext_translations,set_default_locale,set_locale_detector)frommw_aiohttp_babel.middlewaresimportbabel_middleware,lazy_gettextas_set_default_locale('en_GB')# set default locale, if necessary# load compiled localesload_gettext_translations('/path/to/locales','domain')# you can use your own locale detection method, if necessary.# aiohttp_babel checks cookie parameter `locale` or `Accept-Language`# header by default.defdetector(request):ifrequest.url.host=='es.example.com':return'es'elifrequest.url.host=='zh.example.com':return'zh'else:return'en'set_locale_detector(detector)jinja_loader=jinja2.FileSystemLoader('./templates')app=Application(middlewares=[babel_middleware])aiohttp_jinja2.setup(app,loader=jinja_loader)jinja_env=aiohttp_jinja2.get_env(app)jinja_env.globals['_']=_
创建多语言文件 :
创建pot模板
$ pybabel extract -F babel.cfg -o messages.pot app
创建多语言的po文件
> 简体中文
$ pybabel init -i messages.pot -d app/translations -l zh_CN
> 繁体中文
$ pybabel init -i messages.pot -d app/translations -l zh_TW
> 英文
$ pybabel init -i messages.pot -d app/translations -l en
更新多语言的po文件
$ pybabel update -i messages.pot -d app/translations
产生mo文件
$ pybabel compile -d app/translations
How to extract & compile locales:
Code from:
tornado-babel: https://github.com/openlabs/tornado-babel
django-babel: https://github.com/python-babel/django-babel
Changes
0.1.7 (2018-10-25)
- fix gbk error
0.1.4 (2018-10-25)
- 修改readme.rst的错误