Skip to main content

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

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)

Source distribution for mw-aiohttp-babel 0.1.8
File Size Uploaded
mw_aiohttp_babel-0.1.8.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mw-aiohttp-babel 0.1.8
File Interpreter ABI Platform
mw_aiohttp_babel-0.1.8-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page