Skip to main content

Babel localisation support for aiohttp,适用maxwin团队的开发框架

Project description

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['_'] = _

创建多语言文件 :

  1. 创建pot模板

    $ pybabel extract -F babel.cfg -o messages.pot app

  2. 创建多语言的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

  3. 更新多语言的po文件

    $ pybabel update -i messages.pot -d app/translations

  4. 产生mo文件

    $ pybabel compile -d app/translations

How to extract & compile locales:

http://babel.pocoo.org/en/latest/messages.html

http://babel.pocoo.org/en/latest/cmdline.html

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的错误

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

mw_aiohttp_babel-0.1.8.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

mw_aiohttp_babel-0.1.8-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file mw_aiohttp_babel-0.1.8.tar.gz.

File metadata

  • Download URL: mw_aiohttp_babel-0.1.8.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for mw_aiohttp_babel-0.1.8.tar.gz
Algorithm Hash digest
SHA256 87cdd6e53bd62d098c6799d7a4a45de91fc5973a6ac67452cef79eab4d31a468
MD5 ab409a5baf6e43a83b04cf93af169300
BLAKE2b-256 195e757f34cce97d0826efe06ca43a6dd7a1ca8ccc25794eaf02d1bc86d52889

See more details on using hashes here.

File details

Details for the file mw_aiohttp_babel-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for mw_aiohttp_babel-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 2deb3397d42b2edd92e5adf31731bc57ec922b3f9a67647adf180e5f4bc6ddd3
MD5 1dc209bb03bb7e34293dae47716743bf
BLAKE2b-256 0b44631938cf42c58a5921b0072d70336471a44075aa7c056577797a665cbe23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page