如何 cto fuzzy etime ins color 更改 timezone
pip install flask-babel
先初始化一个 Flask-Babel 的实例
- fromflaskimport Flask
- fromflask.ext.babelimport Babel
- app = Flask(__name__)
- babel = Babel(app)
Flask-Babel 提供了两个 Flask 应用配置项:
- app.config.update(
- DEBUG=True,
- BABEL_DEFAULT_LOCALE='zh'
- )
当程序里没指定时,就会采用这些默认设置。那么如何在程序里指定呢?Flask-Babel 提供了两个装饰器 "localeselector" 和 "timezoneselector",分别用来设置语言和时区:
- @babel.localeselector
- def get_locale():
- return 'zh'
- @babel.timezoneselector
- def get_timezone():
- return 'UTC'
这里的设置将会覆盖应用配置项中 "BABEL_DEFAULT_LOCALE" 和 "BABEL_DEFAULT_TIMEZONE"。上面的程序不是个好例子,常见的情况是从当前用户会话中,或者从服务器环境中获取语言 / 时区设置。
装饰器 "localeselector" 和 "timezoneselector" 修饰的函数,被调用一次后就会被缓存,也就是不会被多次调用。但是有时候,当切换用户时,我们想从新用户会话中重新获取语言 / 时区设置,此时可以在登录请求中调用 "refresh()" 方法清缓存:
- fromflask_babelimport refresh
- @app.route('/login')
- def login():
- ... # Get new user locale and timezone
- refresh()
- ... # Render response
Flask-Babel 封装了 Python 的 "gettext()" 方法,你可以在视图函数中使用它:
- fromflask_babelimport gettext, ngettext
- @app.route('/trans')
- @app.route('/trans/<int:num>')
- deftranslate(num=None):
- ifnumis None:
- returngettext(u'No users')
- returnngettext(u'%(num)d user', u'%(num)d users', num)
Flask-Babel 还提供了 "lazy_gettext()" 方法,它的作用同 "gettext()" 类似,区别是它在文字被使用时才会被翻译,所以可以用来在上下文环境外定义要翻译的文字,比如:
- fromflask_babelimport lazy_gettext
- hello = lazy_gettext(u'Hello World')
- @app.route('/lazy')
- def lazy():
- returnunicode(hello)
同样在模板中,我们也可以使用 "gettext()" 方法,更简单的我们可以用 "_()" 方法代替:
- {
- {
- _('Test Sample')
- }
- } {
- {
- _('Hello World ! ')
- }
- }
在 Flask 请求中,我们来渲染此模板:
- @app.route('/')
- def index():
- returnrender_template('hello.html')
启动应用,访问上面的视图,验证下程序是否正常运行。大家应该可以看到 "gettext()" 方法里的文字被显示出来了,目前还没有被翻译。
Babel 中的 "pybabel" 命令来创建翻译文件
这个文件告诉 "pybabel" 要从当前目录及其子目录下所有的 "*.py" 文件,和 templates 目录及其子目录下所有的 "*.html" 文件里面搜寻可翻译的文字,即所有调用 "gettext()","ngettext()" 和 "_()" 方法时传入的字符串。同时它告诉 "pybabel",当前 Jinja2 模板启用了 autoescape 和 with 扩展。
- [python: **.py]
- [jinja2: **/templates/**.html]
- extensions=jinja2.ext.autoescape,jinja2.ext.with_
- pybabel extract - F babel.cfg - o messages.pot.打开"messages.pot",你会发现,上例中"No users",
- "Test Sample"等文字都出现在"msgid"项中了,很强大吧。参数" - F"指定了Babel配置文件;" - o"指定了输出文件名。
- pybabel init -i messages.pot -d translations -l zh
- #: flask-ext3.py:31msgid"No users"
- msgstr "没有用户"
- #: flask-ext3.py:32msgid"%(num)d user"
- msgid_plural "%(num)d users"
- msgstr[0] "%(num)d个用户"
- #: templates/hello.html:2msgid"Test Sample"
- msgstr "测试范例"
- #: templates/hello.html:3msgid"Hello World!"
- msgstr "世界,你好!"
- pybabel compile -d translations
- pybabel update -i messages.pot -d translations
Flask-Babel 不仅可以翻译文字,还可以自动翻译日期格式,运行下面的例子:
- fromflask_babelimport format_datetime
- fromdatetimeimport datetime
- @app.route('/now')
- def current_time():
- returnformat_datetime(datetime.now())
假设当前系统时间是 "2016-3-20 11:38:32″,在 locale 是 en 的情况下,会显示"Mar 20, 2016, 11:39:59 AM";而在 locale 是 zh 的情况下,会显示"2016 年 3 月 20 日 上午 11:38:32″。
"format_datetime()" 方法还可以带第二个参数指定输出格式,如 "full", "short", "yyyy-MM-dd" 等
Flask-Babel 提供了 "format_number" 和 "format_decimal" 方法来格式化数字,使用方法同上例中的 "format_datetime" 非常类似,只需传入待格式化的数字即可:
- fromflask.ext.babelimport format_decimal
- @app.route('/num')
- def get_num():
- returnformat_decimal(1234567.89)
上面的数字,在 locale 是 en 的情况下,会显示 "1,234,567.90″;而在 locale 是 de 的情况下,会显示"1.234.567,89″。
既然可以格式化数字,自然也少不了货币格式化显示的功能了。我们可以使用 "format_currency" 方法,它同 "format_decimal" 的区别是它必须传入两个参数,第二个参数指定了货币类型:
- fromflask_babelimport format_currency
- @app.route('/currency')
- def currency():
- returnformat_currency(1234.5,'CNY')
上面的数字 "1234.5″,在类型(即第二个参数)是"CNY"的情况下,会显示"¥1,234.50″;而在类型是 "USD" 的情况下,会显示 "US$1,234.50″。
Flask-Babel 还提供了格式化百分数 "format_percent",和格式化科学计数 "format_scientific" 的方法等
Flask 扩展 国际化 本地化
来源: http://www.bubuko.com/infodetail-2128964.html