本文整理汇总了Python中flask.g.locale方法的典型用法代码示例。如果您正苦于以下问题:Python g.locale方法的具体用法?Python g.locale怎么用?Python g.locale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.g
的用法示例。
在下文中一共展示了g.locale方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init_babel
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def __init_babel(self):
self.babel = Babel(self.app)
# translations = Translations.load('translations')
# translations.merge(Translations.load())
# todo: support for multiple plugins
supported_languages = []
for supported_lang in self.cfg_engine.get('appkernel.i18n.languages', ['en-US']):
supported_languages.append(supported_lang)
if '-' in supported_lang:
supported_languages.append(supported_lang.split('-')[0])
def get_current_locale():
with self.app.app_context():
best_match = request.accept_languages.best_match(supported_languages, default='en')
return best_match.replace('-', '_')
self.babel.localeselector(get_current_locale)
# catalogs = gettext.find('locale', 'locale', all=True)
# self.logger.info('Using message catalogs: {}'.format(catalogs))
示例2: before_request
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def before_request():
g.locale = 'en'
g.session = database.get_db_session(flask_sess_if_possible=False)
print("Checked out session")
示例3: before_request
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def before_request() -> None:
"""Set user last seen time user."""
if current_user.is_authenticated:
current_user.last_seen = udatetime.utcnow().replace(tzinfo=None)
db.session.commit()
g.search_form = SearchForm()
g.locale = str(get_locale())
示例4: get_locale
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def get_locale():
locale = 'en' # request.accept_languages.best_match(LANGUAGES.keys())
if request.cookies.get('lang') in LANGUAGES.keys():
locale = request.cookies.get('lang')
if request.args.get('lang') in LANGUAGES.keys():
locale = request.args.get('lang')
return locale
示例5: before_request
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def before_request():
g.locale = get_locale()
示例6: title
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def title(self):
return self.any_titles.get(g.locale, self.name)
示例7: title
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def title(self):
return self.titles.get(g.locale, self.name)
示例8: __init_web_layer
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def __init_web_layer(self):
self.app.json_encoder = AppKernelJSONEncoder
self.app.register_error_handler(Exception, self.generic_error_handler)
for code in default_exceptions.keys():
# add a default error handler for everything is unhandled
self.app.register_error_handler(code, self.generic_error_handler)
def set_locale_on_request():
g.locale = str(get_locale())
self.app.before_request(set_locale_on_request)
示例9: get
# 需要导入模块: from flask import g [as 别名]
# 或者: from flask.g import locale [as 别名]
def get(self, server=None, client=None, conf=None):
"""Reads a given client configuration"""
try:
conf = unquote(conf)
except:
pass
args = self.parser_get.parse_args()
template = args.get('template', False)
statictemplate = args.get('statictemplate', False)
parser = bui.client.get_parser(agent=server)
res = parser.read_client_conf(client, conf, template, statictemplate)
refresh()
# Translate the doc and placeholder API side
cache_keys = {
'doc': '_doc_parser_{}-{}'.format(server, g.locale),
'placeholders': '_placeholders_parser_{}-{}'.format(server, g.locale),
'boolean_cli': '_boolean_cli_parser_{}'.format(server),
'string_cli': '_string_cli_parser_{}'.format(server),
'integer_cli': '_integer_cli_parser_{}'.format(server),
'multi_cli': '_multi_cli_parser_{}'.format(server),
'values': '_suggest_parser_{}'.format(server),
'defaults': '_defaults_parser_{}'.format(server),
}
cache_results = {}
for name, key in cache_keys.items():
if not cache.cache.has(key):
if name in ['doc', 'placeholders']:
_tmp = bui.client.get_parser_attr(name, server).copy()
_tmp2 = {}
for k, v in _tmp.items():
_tmp2[k] = _(v)
cache_results[name] = _tmp2
else:
cache_results[name] = bui.client.get_parser_attr(name, server)
cache.cache.set(key, cache_results[name], 3600)
else:
cache_results[name] = cache.cache.get(key)
return jsonify(
results=res,
boolean=cache_results['boolean_cli'],
string=cache_results['string_cli'],
integer=cache_results['integer_cli'],
multi=cache_results['multi_cli'],
server_doc=cache_results['doc'],
suggest=cache_results['values'],
placeholders=cache_results['placeholders'],
defaults=cache_results['defaults']
)