本文整理汇总了Python中django.conf.locale.LANG_INFO属性的典型用法代码示例。如果您正苦于以下问题:Python locale.LANG_INFO属性的具体用法?Python locale.LANG_INFO怎么用?Python locale.LANG_INFO使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类django.conf.locale
的用法示例。
在下文中一共展示了locale.LANG_INFO属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_language_info
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
lang_info = LANG_INFO[lang_code]
if 'fallback' in lang_info and 'name' not in lang_info:
info = get_language_info(lang_info['fallback'][0])
else:
info = lang_info
except KeyError:
if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code)
generic_lang_code = lang_code.split('-')[0]
try:
info = LANG_INFO[generic_lang_code]
except KeyError:
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code))
if info:
info['name_translated'] = gettext_lazy(info['name'])
return info
示例2: get_language_info
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
lang_info = LANG_INFO[lang_code]
if 'fallback' in lang_info and 'name' not in lang_info:
info = get_language_info(lang_info['fallback'][0])
else:
info = lang_info
except KeyError:
if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code)
generic_lang_code = lang_code.split('-')[0]
try:
info = LANG_INFO[generic_lang_code]
except KeyError:
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code))
if info:
info['name_translated'] = ugettext_lazy(info['name'])
return info
示例3: get_language_info
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
lang_info = LANG_INFO[lang_code]
if 'fallback' in lang_info and 'name' not in lang_info:
return get_language_info(lang_info['fallback'][0])
return lang_info
except KeyError:
if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code)
generic_lang_code = lang_code.split('-')[0]
try:
return LANG_INFO[generic_lang_code]
except KeyError:
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code))
示例4: get_supported_language_variant
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_supported_language_variant(lang_code, strict=False):
"""
Returns the language-code that's listed in supported languages, possibly
selecting a more generic variant. Raises LookupError if nothing found.
If `strict` is False (the default), the function will look for an alternative
country-specific variant when the currently checked is not found.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
if lang_code:
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
possible_lang_codes = [lang_code]
try:
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
except KeyError:
pass
generic_lang_code = lang_code.split('-')[0]
possible_lang_codes.append(generic_lang_code)
supported_lang_codes = get_languages()
for code in possible_lang_codes:
if code in supported_lang_codes and check_for_language(code):
return code
if not strict:
# if fr-fr is not supported, try fr-ca.
for supported_code in supported_lang_codes:
if supported_code.startswith(generic_lang_code + '-'):
return supported_code
raise LookupError(lang_code)
示例5: get_supported_language_variant
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_supported_language_variant(lang_code, strict=False):
"""
Return the language-code that's listed in supported languages, possibly
selecting a more generic variant. Raise LookupError if nothing is found.
If `strict` is False (the default), look for an alternative
country-specific variant when the currently checked is not found.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
if lang_code:
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
possible_lang_codes = [lang_code]
try:
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
except KeyError:
pass
generic_lang_code = lang_code.split('-')[0]
possible_lang_codes.append(generic_lang_code)
supported_lang_codes = get_languages()
for code in possible_lang_codes:
if code in supported_lang_codes and check_for_language(code):
return code
if not strict:
# if fr-fr is not supported, try fr-ca.
for supported_code in supported_lang_codes:
if supported_code.startswith(generic_lang_code + '-'):
return supported_code
raise LookupError(lang_code)
示例6: get_supported_language_variant
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_supported_language_variant(lang_code, strict=False):
"""
Return the language code that's listed in supported languages, possibly
selecting a more generic variant. Raise LookupError if nothing is found.
If `strict` is False (the default), look for a country-specific variant
when neither the language code nor its generic variant is found.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
if lang_code:
# If 'fr-ca' is not supported, try special fallback or language-only 'fr'.
possible_lang_codes = [lang_code]
try:
possible_lang_codes.extend(LANG_INFO[lang_code]['fallback'])
except KeyError:
pass
generic_lang_code = lang_code.split('-')[0]
possible_lang_codes.append(generic_lang_code)
supported_lang_codes = get_languages()
for code in possible_lang_codes:
if code in supported_lang_codes and check_for_language(code):
return code
if not strict:
# if fr-fr is not supported, try fr-ca.
for supported_code in supported_lang_codes:
if supported_code.startswith(generic_lang_code + '-'):
return supported_code
raise LookupError(lang_code)
示例7: get_language_info
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def get_language_info(lang_code):
from django.conf.locale import LANG_INFO
try:
return LANG_INFO[lang_code]
except KeyError:
raise KeyError("Unknown language code %r." % lang_code)
示例8: extract_language_options
# 需要导入模块: from django.conf import locale [as 别名]
# 或者: from django.conf.locale import LANG_INFO [as 别名]
def extract_language_options(self) -> None:
locale_path = f"{settings.DEPLOY_ROOT}/locale"
output_path = f"{locale_path}/language_options.json"
data: Dict[str, List[Dict[str, Any]]] = {'languages': []}
try:
locales = self.get_locales()
except CalledProcessError:
# In case we are not under a Git repo, fallback to getting the
# locales using listdir().
locales = os.listdir(locale_path)
locales.append('en')
locales = list(set(locales))
for locale in locales:
if locale == 'en':
data['languages'].append({
'name': 'English',
'name_local': 'English',
'code': 'en',
'locale': 'en',
})
continue
lc_messages_path = os.path.join(locale_path, locale, 'LC_MESSAGES')
if not os.path.exists(lc_messages_path):
# Not a locale.
continue
info: Dict[str, Any] = {}
code = to_language(locale)
percentage = self.get_translation_percentage(locale_path, locale)
try:
name = LANG_INFO[code]['name']
name_local = LANG_INFO[code]['name_local']
except KeyError:
# Fallback to getting the name from PO file.
filename = self.get_po_filename(locale_path, locale)
name = self.get_name_from_po_file(filename, locale)
name_local = with_language(name, code)
info['name'] = name
info['name_local'] = name_local
info['code'] = code
info['locale'] = locale
info['percent_translated'] = percentage
data['languages'].append(info)
with open(output_path, 'w') as writer:
json.dump(data, writer, indent=2, sort_keys=True)
writer.write('\n')