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使用样例:
import aiohttp_jinja2
from mw_aiohttp.web import Application
from mw_aiohttp_babel import Babel,_
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的样例:
import aiohttp_jinja2
from mw_aiohttp.web import Application
from mw_aiohttp_babel.locale import (load_gettext_translations,
set_default_locale,
set_locale_detector)
from mw_aiohttp_babel.middlewares import babel_middleware, lazy_gettext as _
set_default_locale('en_GB') # set default locale, if necessary
# load compiled locales
load_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.
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(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的错误
Metadata
Release files for mw-aiohttp-babel 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mw_aiohttp_babel-0.1.8.tar.gz | 6.3 kB | Without provenance | Details for mw_aiohttp_babel-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mw_aiohttp_babel-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for mw_aiohttp_babel-0.1.8-py3-none-any.whl |
Total release size: 12.9 kB
Release files / mw_aiohttp_babel-0.1.8.tar.gz
| Download URL | mw_aiohttp_babel-0.1.8.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
87cdd6e53bd62d098c6799d7a4a45de91fc5973a6ac67452cef79eab4d31a468
|
|
BLAKE2b-256 checksum How to use checksums |
195e757f34cce97d0826efe06ca43a6dd7a1ca8ccc25794eaf02d1bc86d52889
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|
Release files / mw_aiohttp_babel-0.1.8-py3-none-any.whl
| Download URL | mw_aiohttp_babel-0.1.8-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2deb3397d42b2edd92e5adf31731bc57ec922b3f9a67647adf180e5f4bc6ddd3
|
|
BLAKE2b-256 checksum How to use checksums |
0b44631938cf42c58a5921b0072d70336471a44075aa7c056577797a665cbe23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.9
|
Security for mw-aiohttp-babel 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.