本文整理汇总了Python中config.LANGUAGES.keys方法的典型用法代码示例。如果您正苦于以下问题:Python LANGUAGES.keys方法的具体用法?Python LANGUAGES.keys怎么用?Python LANGUAGES.keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类config.LANGUAGES
的用法示例。
在下文中一共展示了LANGUAGES.keys方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
if (g.user is not None) and (not g.user.is_anonymous()):
if g.user.lang_id is not None:
app.logger.info(g.user.lang.short_name)
return g.user.lang.short_name
else:
accept_lang = request.accept_languages.best_match(LANGUAGES.keys())
if accept_lang is None or (accept_lang == ''):
accept_lang = 'en'
g.user.lang = Language.query.filter_by(short_name = accept_lang).first()
db.session.add(g.user)
db.session.commit()
return g.user.lang.short_name
else:
return request.accept_languages.best_match(LANGUAGES.keys())
示例2: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
print "hi"
# for testing, hard code the language to show
# return 'fr'
print request.accept_languages
# in production use the below to change language based on http 'accept-languages' header:
return request.accept_languages.best_match(LANGUAGES.keys())
示例3: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
if request.args.get('lang'):
g.lang = request.args.get('lang')
if g.get('lang'):
return g.lang
g.lang = request.accept_languages.best_match(LANGUAGES.keys())
return g.lang
示例4: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
# if language forced
if 'LANGUAGE'in app.config:
if app.config['LANGUAGE'] in LANGUAGES.keys():
return app.config['LANGUAGE']
else:
print 'Language not found in Config - LANGUAGES'
# else if a user is logged in, use the locale from the user settings
user = getattr(g, 'user', None)
if user is not None:
print user.locale
return user.locale
# otherwise try to guess the language from the user accept
# header the browser transmits. We support de/fr/en in this
# example. The best match wins.
return request.accept_languages.best_match(LANGUAGES.keys()) # 'it'
示例5: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
# if a user is logged in, use the locale from the user settings
# user = getattr(g, 'user', None)
# if user is not None:
# return user.locale
# otherwise try to guess the language from the user accept
# header the browser transmits. We support de/fr/en in this
# example. The best match wins.
return request.accept_languages.best_match(LANGUAGES.keys())
示例6: before
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def before():
g.languages = LANGUAGES
g.locale = request.cookies.get('locale')
if g.locale is None:
g.locale = request.accept_languages.best_match(LANGUAGES.keys())
@after_this_request
def remember_language(response):
response.set_cookie('locale', g.locale)
return response
g.search_form = SearchForm()
if current_user and current_user.is_authenticated:
current_user.see_you()
g.roles = [role.role_name for role in current_user.roles]
示例7: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
supported_languages = LANGUAGES.keys()
language_arg = request.args.get('l')
if language_arg is not None:
if language_arg in supported_languages:
@after_this_request
def remember_language(response):
response.set_cookie('language', language_arg)
return language_arg
else:
language_cookie = request.cookies.get('language')
if language_cookie in supported_languages:
return language_cookie
return request.accept_languages.best_match(supported_languages)
示例8: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
"""
the function targeted by this selector above will be called before each request, to give us a chance to choose the
language when producing its response.
For now we will do something simple, we will just read the Accept-Lanugages header sent by the browser in the HTTP
request and find the best-matching language from the list we support. This is simple, 'best_match' method does it
for us.
Accept-Languages header in most browsers is config'd by default with language selected at OS level, but all give
users chance to select others. Users can provide a list of languages, each with a weight. Looks like:
Accept-Language: da, en-gb;q=0.8, en;q=0.7
Danish default weight=1 is preferred, followed by GB-English, finally with generic english.
To translate text, use gettext(string to translate) within Python code, or {{ _('string')}} inside HTML
:return:
"""
#return 'es'
return request.accept_languages.best_match(LANGUAGES.keys()) # temp set to spanish for testing
示例9: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
return request.accept_languages.best_match(LANGUAGES.keys())
if query.duration >= DATABASE_QUERY_TIMEOUT:
app.logger.warning("SLOW QUERY: %s\nParameters: %s\nDuration: %fs\nContext: %s\n" % (query.statement, query.parameters, query.duration, query.context))
示例10: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
headers = request.headers.get('User-Agent')
app.logger.info('Header info: ' + headers)
return request.accept_languages.best_match(LANGUAGES.keys()) or 'en'
示例11:
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
if sys.platform == 'win32':
pybabel = r"C:\Python27\Scripts\pybabel.exe"
else:
pybabel = "pybabel"
from config import LANGUAGES
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -k T -o messages.pot ..\\')
for lang in LANGUAGES.keys():
os.system('%s init -i messages.pot -d . -l "%s"' % (pybabel, lang.replace("-","_")))
os.system("pause")
os.unlink('messages.pot')
示例12: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
if current_user and current_user.is_authenticated():
return current_user.lang
return request.accept_languages.best_match(LANGUAGES.keys())
示例13: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
try:
return request.cookies["selected_lang"]
except:
return request.accept_languages.best_match(LANGUAGES.keys())
示例14: before
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def before():
if request.view_args and 'lang_code' in request.view_args:
if request.view_args['lang_code'] not in LANGUAGES.keys():
return abort(404)
g.current_lang = request.view_args['lang_code']
request.view_args.pop('lang_code')
示例15: get_locale
# 需要导入模块: from config import LANGUAGES [as 别名]
# 或者: from config.LANGUAGES import keys [as 别名]
def get_locale():
"""Return the best matched language supported."""
return request.accept_languages.best_match(LANGUAGES.keys())