本文整理汇总了Python中babel.core.Locale.negotiate方法的典型用法代码示例。如果您正苦于以下问题:Python Locale.negotiate方法的具体用法?Python Locale.negotiate怎么用?Python Locale.negotiate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.core.Locale
的用法示例。
在下文中一共展示了Locale.negotiate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_negotiate
# 需要导入模块: from babel.core import Locale [as 别名]
# 或者: from babel.core.Locale import negotiate [as 别名]
def test_negotiate(self):
de_DE = Locale.negotiate(['de_DE', 'en_US'], ['de_DE', 'de_AT'])
assert (de_DE.language, de_DE.territory) == ('de', 'DE')
de = Locale.negotiate(['de_DE', 'en_US'], ['en', 'de'])
assert (de.language, de.territory) == ('de', None)
nothing = Locale.negotiate(['de_DE', 'de'], ['en_US'])
assert nothing is None
示例2: negotiate
# 需要导入模块: from babel.core import Locale [as 别名]
# 或者: from babel.core.Locale import negotiate [as 别名]
def negotiate(cls, locale):
"""
Negotiate proper RBNF rules based on global data
item `rbnf_locales`
Caching is not necessary the Locale object does that pretty well
"""
loc = Locale.negotiate([str(Locale.parse(locale))], get_global('rbnf_locales'))
print('TL', type(loc))
return cls(loc)
示例3: _check_locale
# 需要导入模块: from babel.core import Locale [as 别名]
# 或者: from babel.core.Locale import negotiate [as 别名]
def _check_locale():
""" Determine locale from request if non set """
if not request.environ.get('LOCALE'):
accept_language = request.get_header('Accept-Language')
if not accept_language:
return
accepted = []
for language in accept_language.split(','):
if language.split(';')[0] == language:
accepted.append(language.strip())
else:
accepted.append(language.split(";")[0].strip())
# fine tuning order of locale_q_pair according to q-value necessary?!
lang = Locale.negotiate(accepted, [l[0] for l in LANGS])
if lang:
request.environ['LOCALE'] = str(lang)
示例4: init_gettext
# 需要导入模块: from babel.core import Locale [as 别名]
# 或者: from babel.core.Locale import negotiate [as 别名]
def init_gettext():
locale_dir = os.path.join(basedir, 'cddagl', 'locale')
preferred_locales = []
selected_locale = get_config_value('locale', None)
if selected_locale == 'None':
selected_locale = None
if selected_locale is not None:
preferred_locales.append(selected_locale)
system_locale = get_ui_locale()
if system_locale is not None:
preferred_locales.append(system_locale)
if os.path.isdir(locale_dir):
entries = scandir(locale_dir)
for entry in entries:
if entry.is_dir():
available_locales.append(entry.name)
available_locales.sort(key=lambda x: 0 if x == 'en' else 1)
app_locale = Locale.negotiate(preferred_locales, available_locales)
if app_locale is None:
app_locale = 'en'
else:
app_locale = str(app_locale)
try:
t = gettext.translation('cddagl', localedir=locale_dir,
languages=[app_locale])
global _
_ = t.gettext
except FileNotFoundError as e:
pass
return app_locale
示例5: test_negotiate_custom_separator
# 需要导入模块: from babel.core import Locale [as 别名]
# 或者: from babel.core.Locale import negotiate [as 别名]
def test_negotiate_custom_separator(self):
de_DE = Locale.negotiate(['de-DE', 'de'], ['en-us', 'de-de'], sep='-')
assert (de_DE.language, de_DE.territory) == ('de', 'DE')