API
文档这一部分列出了 Flask-Babel 中每一个公开的类或者函数。
配置
class flask.ext.babel.Babel(app=None, default_locale='en', default_timezone='UTC', date_formats=None, configure_jinja=True)
Central controller class that can be used to configure how Flask-Babel behaves. Each application that wants to use Flask-Babel has to create, or run init_app()
on, an instance of this class after the configuration was initialized.
default_locale
The default locale from the configuration as instance of a babel.Locale
object.
default_timezone
The default timezone from the configuration as instance of a pytz.timezone
object.
init_app(app)
Set up this instance for use with app, if no app was passed to the constructor.
list_translations()
Returns a list of all the locales translations exist for. The list returned will be filled with actual locale objects and not just strings.
New in version 0.6.
localeselector(f)
Registers a callback function for locale selection. The default behaves as if a function was registered that returns None
all the time. If None
is returned, the locale falls back to the one from the configuration.
This has to return the locale as string (eg: 'de_AT'
, ‘’en_US
‘’)
timezoneselector(f)
Registers a callback function for timezone selection. The default behaves as if a function was registered that returns None
all the time. If None
is returned, the timezone falls back to the one from the configuration.
This has to return the timezone as string (eg: 'Europe/Vienna'
)
Context 函数
flask.ext.babel.get_translations()
Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found.
flask.ext.babel.get_locale()
Returns the locale that should be used for this request as babel.Locale
object. This returns None
if used outside of a request.
flask.ext.babel.get_timezone()
Returns the timezone that should be used for this request as pytz.timezone
object. This returns None
if used outside of a request.
Datetime 函数
flask.ext.babel.to_user_timezone(datetime)
Convert a datetime object to the user’s timezone. This automatically happens on all date formatting unless rebasing is disabled. If you need to convert a datetime.datetime
object at any time to the user’s timezone (as returned by get_timezone()
this function can be used).
flask.ext.babel.to_utc(datetime)
Convert a datetime object to UTC and drop tzinfo. This is the opposite operation to to_user_timezone()
.
flask.ext.babel.format_datetime(datetime=None, format=None, rebase=True)
Return a date formatted according to the given pattern. If no datetime
object is passed, the current time is assumed. By default rebasing happens which causes the object to be converted to the users’s timezone (as returned by to_user_timezone()
). This function formats both date and time.
The format parameter can either be 'short'
, 'medium'
, 'long'
or 'full'
(in which cause the language’s default for that setting is used, or the default from the Babel.date_formats
mapping is used) or a format string as documented by Babel.
This function is also available in the template context as filter named datetimeformat
.
flask.ext.babel.format_date(date=None, format=None, rebase=True)
Return a date formatted according to the given pattern. If no datetime
or date
object is passed, the current time is assumed. By default rebasing happens which causes the object to be converted to the users’s timezone (as returned by to_user_timezone()
). This function only formats the date part of a datetime
object.
The format parameter can either be 'short'
, 'medium'
, 'long'
or 'full'
(in which cause the language’s default for that setting is used, or the default from the Babel.date_formats
mapping is used) or a format string as documented by Babel.
This function is also available in the template context as filter named dateformat
.
flask.ext.babel.format_time(time=None, format=None, rebase=True)
Return a time formatted according to the given pattern. If no datetime
object is passed, the current time is assumed. By default rebasing happens which causes the object to be converted to the users’s timezone (as returned by to_user_timezone()
). This function formats both date and time.
The format parameter can either be 'short'
, 'medium'
, 'long'
or 'full'
(in which cause the language’s default for that setting is used, or the default from the Babel.date_formats
mapping is used) or a format string as documented by Babel.
This function is also available in the template context as filter named timeformat
.
flask.ext.babel.format_timedelta(datetime_or_timedelta, granularity='second')
Format the elapsed time from the given date to now or the given timedelta. This currently requires an unreleased development version of Babel.
This function is also available in the template context as filter named timedeltaformat
.
Gettext 函数
flask.ext.babel.gettext(string, **variables)
Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string.
gettext(u'Hello World!')
gettext(u'Hello %(name)s!', name='World')
flask.ext.babel.ngettext(singular, plural, num, **variables)
Translates a string with the current locale and passes in the given keyword arguments as mapping to a string formatting string. The num
parameter is used to dispatch between singular and various plural forms of the message. It is available in the format string as %(num)d
or %(num)s
. The source language should be English or a similar language which only has one plural form.
ngettext(u'%(num)d Apple', u'%(num)d Apples', num=len(apples))
flask.ext.babel.pgettext(context, string, **variables)
Like gettext()
but with a context.
New in version 0.7.
flask.ext.babel.npgettext(context, singular, plural, num, **variables)
Like ngettext()
but with a context.
New in version 0.7.
flask.ext.babel.lazy_gettext(string, **variables)
Like gettext()
but the string returned is lazy which means it will be translated when it is used as an actual string.
Example:
hello = lazy_gettext(u'Hello World')
@app.route('/')
def index():
return unicode(hello)
flask.ext.babel.lazy_pgettext(context, string, **variables)
Like pgettext()
but the string returned is lazy which means it will be translated when it is used as an actual string.
New in version 0.7.
低层的 API
flask.ext.babel.refresh()
Refreshes the cached timezones and locale information. This can be used to switch a translation between a request and if you want the changes to take place immediately, not just with the next request:
user.timezone = request.form['timezone']
user.locale = request.form['locale']
refresh()
flash(gettext('Language was changed'))
Without that refresh, the flash()
function would probably return English text and a now German page.