本文整理汇总了Python中babel.Locale.negotiate方法的典型用法代码示例。如果您正苦于以下问题:Python Locale.negotiate方法的具体用法?Python Locale.negotiate怎么用?Python Locale.negotiate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.Locale
的用法示例。
在下文中一共展示了Locale.negotiate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_negotiate
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.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: set_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def set_locale(cls, force=None):
"""
retrieve locale from a prioritized list of sources and then set locale and save it
cls: self object
force: a locale to force set (ie 'en_US')
return: locale as string or None if i18n should be disabled
"""
# disable i18n if config.locales array is empty or None
if not config.locales:
return None
# 1. force locale if provided
locale = force
if locale not in config.locales:
# 2. retrieve locale from url query string
locale = cls.request.get("hl", None)
if locale not in config.locales:
# 3. retrieve locale from cookie
locale = cls.request.cookies.get('hl', None)
if locale not in config.locales:
# 4. retrieve locale from accept language header
locale = get_locale_from_accept_header(cls.request)
if locale not in config.locales:
# 5. detect locale from IP address location
territory = get_territory_from_ip(cls) or 'ZZ'
locale = str(Locale.negotiate(territory, config.locales))
if locale not in config.locales:
# 6. use default locale
locale = i18n.get_store().default_locale
i18n.get_i18n().set_locale(locale)
# save locale in cookie with 26 weeks expiration (in seconds)
cls.response.set_cookie('hl', locale, max_age = 15724800)
return locale
示例3: handle_request
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def handle_request(request, tmpl_context):
from pylons import session
tmpl_context.language = locale = None
if 'locale' in session:
locale = Locale.parse(session.get('locale'))
else:
requested = [l.replace('-', '_') for l in request.languages]
locale = Locale.parse(Locale.negotiate(get_available_languages(), requested))
if locale is None:
locale = get_default_locale()
tmpl_context.locale = locale
options = [str(locale), locale.language, str(get_default_locale()),
get_default_locale().language]
for language in options:
try:
set_lang(language)
# Lose the territory part of the locale string
tmpl_context.language = get_lang()[0].split('_')[0]
break
except:
pass
示例4: user_language
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def user_language(user, fallbacks=[]):
# find out the locale
locale = None
if user and user.locale:
locale = user.locale
if locale is None:
locales = map(str, LOCALES)
locale = Locale.parse(Locale.negotiate(fallbacks, locales)) \
or get_default_locale()
# determinate from which path we load the translations
translations_module = config.get('adhocracy.translations', 'adhocracy')
translations_module_loader = pkgutil.get_loader(translations_module)
if translations_module_loader is None:
raise ValueError(('Cannot import the module "%s" configured for '
'"adhocracy.translations". Make sure it is an '
'importable module (and contains the '
'translation files in a subdirectory '
'"i18n"') % translations_module)
translations_root = translations_module_loader.filename
translations_config = {'pylons.paths': {'root': translations_root},
'pylons.package': config.get('pylons.package')}
# set language and fallback
set_lang(locale.language, pylons_config=translations_config)
add_fallback(get_default_locale().language,
pylons_config=translations_config)
formencode.api.set_stdtranslation(domain="FormEncode",
languages=[locale.language])
return locale
示例5: set_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def set_locale(self, force=None):
locales = self.app.config.get('locales')
# disable i18n if config.locales array is empty or None
if not locales:
return None
# 1. force locale if provided
locale = force
if locale not in locales:
# 2. retrieve locale from url query string
locale = self.request.get("hl", None)
if locale not in locales:
# 3. retrieve locale from cookie
locale = self.request.cookies.get('hl', None)
if locale not in locales:
# 4. retrieve locale from accept language header
#locale = get_locale_from_accept_header(self.request)
if locale not in locales:
# 5. detect locale from IP address location
territory = get_territory_from_ip(self) or 'ZZ'
locale = str(Locale.negotiate(territory, locales))
if locale not in locales:
# 6. use default locale
locale = self.request.GET.get('locale', 'en_US')
i18n.get_i18n().set_locale(locale)
logger.info('locale is {0}'.format(locale))
# save locale in cookie with 26 weeks expiration (in seconds)
self.response.set_cookie('hl', locale, max_age=15724800)
return locale
示例6: pull_lang_code
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def pull_lang_code(endpoint, values):
'''
Carga el código de idioma en la variable global.
'''
# obtiene el idioma de la URL
g.url_lang = None
if values is not None:
g.url_lang = values.pop('lang', None)
# si esta lista de idiomas permitidos
if g.url_lang and g.url_lang in app.config["ALL_LANGS"]:
g.lang = g.url_lang
# si el usuario esta logueado y tiene establecido el idioma se asigna ese
elif "user" in session and "lang" in session["user"]:
g.lang = session["user"]["lang"]
# si no esta logueado y ha elegido un idioma
elif "lang" in session:
g.lang = session["lang"]
else:
accept = request.accept_languages.values()
# si viene, se coge el que mas convenga dependiendo del que tenga establecido en su navegador o el idioma por defecto
locale = Locale.negotiate((option.replace("-","_") for option in accept), app.config["ALL_LANGS"]) if accept else None
if locale:
g.lang = locale.language
else:
g.lang = app.config["LANGS"][0] # valor por defecto si todo falla
# se carga la lista de idiomas como se dice en cada idioma
g.languages = pull_lang_code_languages
g.beta_lang = g.lang in app.config["BETA_LANGS"]
示例7: get_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def get_locale():
userLanguage = current_user.config.language if current_user.is_authenticated() else None
if userLanguage and userLanguage in LANGUAGES:
return userLanguage
else:
return Locale.negotiate(request.accept_languages.values(), LANGUAGES)
示例8: negotiate_known_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def negotiate_known_locale(self, preferred_locales):
"""Given a list of preferred locales, this method returns the best
match locale object from the known ones."""
assert isinstance(preferred_locales, (tuple, list))
preferred_locales = [str(l).replace("-", "_") for l in preferred_locales]
return Locale.parse(
Locale.negotiate(preferred_locales, self.get_available_locale_names(), aliases=self.get_aliases())
)
示例9: get_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def get_locale():
if 'locale' in session:
return Locale.parse(session.get('locale'))
else:
requested = request.accept_languages.values()
requested = [l.replace('-', '_') for l in requested]
available = map(unicode, babel.list_translations())
return Locale.negotiate(available, requested)
示例10: select_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def select_locale():
available = available_locales.keys()
preferred = _get_preferred_languages()
# Find best match
best_match = Locale.negotiate(preferred, available)
# Return best match
if best_match:
return str(best_match)
示例11: _get_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def _get_locale(self):
global LANGUAGES
if "l10n" in request.values:
return Locale.negotiate([request.values["l10n"]], LANGUAGES)
if hasattr(g, "identity") and g.identity and userManager.enabled:
userid = g.identity.id
try:
user_language = userManager.getUserSetting(userid, ("interface", "language"))
if user_language is not None and not user_language == "_default":
return Locale.negotiate([user_language], LANGUAGES)
except octoprint.users.UnknownUser:
pass
default_language = settings().get(["appearance", "defaultLanguage"])
if default_language is not None and not default_language == "_default" and default_language in LANGUAGES:
return Locale.negotiate([default_language], LANGUAGES)
return Locale.parse(request.accept_languages.best_match(LANGUAGES))
示例12: _set_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def _set_locale(request, force=None):
"""
retrieve locale from a prioritized list of sources and then set locale and save it
cls: self object
force: a locale to force set (ie 'en_US')
return: locale as string or None if i18n should be disabled
"""
try:
webapp2_instance = webapp2.get_app()
except AssertionError:
logging.debug('No webapp2 global set; skipping registry lookup for jinja2 template engine.')
locales = []
else:
locales = webapp2_instance.config.get(I18N_LOCALES_KEY) or []
# disable i18n if config.locales array is empty or None
if not locales:
return None
# 1. force locale if provided
locale = force
if locale not in locales:
# 2. retrieve locale from url query string
locale = request.get("hl", None)
if locale not in locales:
# 3. retrieve locale from user preferences
user_session_info = request.registry['user_session_info']
if user_session_info:
user_id = user_session_info.get('user_id', None)
if user_id:
stored_locale = request.session.get('inferred_locale', None)
if not stored_locale:
lower = str(request.registry['user_session_info'].get('language_preference', None))
upper = str(lower).upper()
stored_locale = '{0}_{1}'.format(lower, upper)
request.session['inferred_locale'] = stored_locale
locale = stored_locale
else:
locale = None
if locale not in locales:
# 4. retrieve locale from accept language header
locale = get_locale_from_accept_header(request)
if locale not in locales:
# 5. detect locale from IP address location
territory = get_country_code(request) or 'ZZ'
locale = str(Locale.negotiate(territory, locales))
if locale not in locales:
# 6. use default locale
locale = i18n.get_store().default_locale
i18n.get_i18n(request=request).set_locale(locale)
request.registry['locale'] = locale
示例13: sys_localizer
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def sys_localizer(reg):
cur_locale = reg.settings.get('pyramid.default_locale_name', 'en')
sys_locale = locale.getlocale()[0]
if sys_locale:
new_locale = Locale.negotiate(
(sys_locale,),
reg.settings.get('pyramid.available_languages', '').split())
if new_locale:
cur_locale = str(new_locale)
else:
cur_locale = 'en'
tdirs = reg.queryUtility(ITranslationDirectories, default=[])
return make_localizer(cur_locale, tdirs)
示例14: get_locale_from_accept_header
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def get_locale_from_accept_header(request, localeTags):
""" Detect a locale from request.header 'Accept-Language'
The locale with the highest quality factor (q) that most nearly matches our config.locales is returned.
rh: webapp2.RequestHandler
Note that in the future if all User Agents adopt the convention of sorting quality factors in descending order
then the first can be taken without needing to parse or sort the accept header leading to increased performance.
(see http://lists.w3.org/Archives/Public/ietf-http-wg/2012AprJun/0473.html)
"""
header = request.headers.get("Accept-Language", '')
parsed = parse_accept_language_header(header)
if parsed is None:
return None
pairs_sorted_by_q = sorted(parsed.items(), key=lambda (lang, q): q, reverse=True)
locale = Locale.negotiate([lang for (lang, q) in pairs_sorted_by_q], request.app.config.get('locales'), sep='_')
return str(locale)
示例15: get_negotiated_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import negotiate [as 别名]
def get_negotiated_locale(preferred_locales):
def normalize(locale_ids):
return [id.replace("-", "_") for id in locale_ids if id]
available_locales = get_available_locales()
if "en_US" not in available_locales:
available_locales.append("en_US")
locale = Locale.negotiate(normalize(preferred_locales), normalize(available_locales))
if locale and str(locale) not in available_locales:
# The list of get_available_locales() must include locale
# identifier from str(locale), but zh_* don't be included after
# Babel 1.0. Avoid expanding zh_* to zh_Hans_CN and zh_Hant_TW
# to clear "script" property of Locale instance. See #11258.
locale._data # load localedata before clear script property
locale.script = None
assert str(locale) in available_locales
return locale