本文整理汇总了Python中django.utils.translation.to_locale方法的典型用法代码示例。如果您正苦于以下问题:Python translation.to_locale方法的具体用法?Python translation.to_locale怎么用?Python translation.to_locale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.translation
的用法示例。
在下文中一共展示了translation.to_locale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: currency
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def currency(value, currency=None):
"""
Format decimal value as currency
"""
try:
value = D(value)
except (TypeError, InvalidOperation):
return ""
# Using Babel's currency formatting
# http://babel.pocoo.org/en/latest/api/numbers.html#babel.numbers.format_currency
kwargs = {
'currency': currency or CURRENCY,
'locale': to_locale(get_language() or settings.LANGUAGE_CODE)
}
return format_currency(value, **kwargs)
示例2: currency_formatter
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def currency_formatter(value, currency=None):
"""
Format decimal value as currency
"""
try:
value = D(value)
except (TypeError, InvalidOperation):
return ""
# Using Babel's currency formatting
# http://babel.pocoo.org/docs/api/numbers/#babel.numbers.format_currency
currency = currency or settings.ACCOUNTING_DEFAULT_CURRENCY
kwargs = {
'currency': currency,
'format': getattr(settings, 'CURRENCY_FORMAT', None),
'locale': to_locale(get_language()),
}
return format_currency(value, **kwargs)
示例3: react_locale
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def react_locale(value):
"""
Convert a language (simple ISO639-1 or full language with regions) to an ISO15897 locale.
This locale is supported by the react frontend.
"""
value_locale = to_locale(value)
# pylint: disable=unsupported-membership-test
if value_locale in settings.REACT_LOCALES:
return value_locale
# pylint: disable=not-an-iterable
for locale in settings.REACT_LOCALES:
if locale[:2] == value_locale:
return locale
raise ImproperlyConfigured(
f"{value:s} does not correspond to any locale supported by the React frontend."
)
示例4: iter_format_modules
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def iter_format_modules(lang):
"""
Does the heavy lifting of finding format modules.
"""
if check_for_language(lang):
format_locations = ['django.conf.locale.%s']
if settings.FORMAT_MODULE_PATH:
format_locations.append(settings.FORMAT_MODULE_PATH + '.%s')
format_locations.reverse()
locale = to_locale(lang)
locales = [locale]
if '_' in locale:
locales.append(locale.split('_')[0])
for location in format_locations:
for loc in locales:
try:
yield import_module('.formats', location % loc)
except ImportError:
pass
示例5: percentage_formatter
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def percentage_formatter(value):
if value or value == 0:
kwargs = {
'locale': to_locale(get_language()),
'format': "#,##0.00 %",
}
return format_percent(value, **kwargs)
示例6: get_current_locale
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def get_current_locale():
"""Return the locale for the current language."""
return to_locale(get_language())
示例7: date_filter
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def date_filter(value):
return format_date(
value.astimezone(get_current_timezone()),
format='full',
locale=to_locale(get_language()),
)
示例8: time_filter
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def time_filter(value):
return format_time(
value,
format='short',
locale=to_locale(get_language()),
tzinfo=get_current_timezone(),
)
示例9: prepare_activity_conversation_message_notification
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def prepare_activity_conversation_message_notification(user, messages):
activity = target_from_messages(messages)
language = language_for_user(user)
with translation.override(language):
with timezone.override(activity.place.group.timezone):
weekday = format_date(
activity.date.start.astimezone(timezone.get_current_timezone()),
'EEEE',
locale=translation.to_locale(language),
)
time = format_time(
activity.date.start,
format='short',
locale=translation.to_locale(language),
tzinfo=timezone.get_current_timezone(),
)
date = format_date(
activity.date.start.astimezone(timezone.get_current_timezone()),
format='long',
locale=translation.to_locale(language),
)
long_date = '{} {}, {}'.format(weekday, time, date)
short_date = '{} {}'.format(weekday, time)
reply_to_name = _('Pickup %(date)s') % {
'date': short_date,
}
conversation_name = _('Pickup %(date)s') % {
'date': long_date,
}
return prepare_message_notification(
user,
messages,
group=activity.place.group,
reply_to_name=reply_to_name,
conversation_name=conversation_name,
conversation_url=activity_detail_url(activity),
stats_category='activity_conversation_message'
)
示例10: render_lang_template
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def render_lang_template(template_name):
loc = to_locale(get_language())
lst = [
template_name + '_' + loc + '.html',
template_name + '_' + settings.LANGUAGES[0][0] + '.html',
template_name + '_en.html',
template_name + '.html'
]
for el in lst:
try:
t = get_template(el)
return t.render()
except TemplateDoesNotExist:
pass
return ''
示例11: get_alt_src_langs
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def get_alt_src_langs(request, user, translation_project):
language = translation_project.language
project = translation_project.project
source_language = project.source_language
langs = user.alt_src_langs.exclude(id__in=(language.id, source_language.id)).filter(
translationproject__project=project
)
if not user.alt_src_langs.count():
from pootle_language.models import Language
accept = request.META.get("HTTP_ACCEPT_LANGUAGE", "")
for accept_lang, __ in parse_accept_lang_header(accept):
if accept_lang == "*":
continue
simplified = data.simplify_to_common(accept_lang)
normalized = to_locale(data.normalize_code(simplified))
code = to_locale(accept_lang)
if normalized in (
"en",
"en_US",
source_language.code,
language.code,
) or code in ("en", "en_US", source_language.code, language.code):
continue
langs = Language.objects.filter(
code__in=(normalized, code), translationproject__project=project,
)
if langs.count():
break
return langs
#
# Views used with XMLHttpRequest requests.
#
示例12: tr_lang
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def tr_lang(language_name):
"""Translates language names."""
language_code = translation.get_language()
if language_code is None:
language_code = settings.LANGUAGE_CODE
language_code = translation.to_locale(language_code)
return langdata.tr_lang(language_code)(language_name)
示例13: locale
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def locale(request):
"""Convert the language string to a locale"""
"""Copied from: http://stackoverflow.com/a/6362929 """
return {'LOCALE': to_locale(get_language())}
示例14: format_currency
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def format_currency(currency, amount, format=None, locale=None): # pylint: disable=redefined-builtin
locale = locale or to_locale(get_language())
format = format or getattr(settings, 'OSCAR_CURRENCY_FORMAT', None)
return default_format_currency(
amount,
currency,
format=format,
locale=locale
)
示例15: get_user_locale
# 需要导入模块: from django.utils import translation [as 别名]
# 或者: from django.utils.translation import to_locale [as 别名]
def get_user_locale():
lang = get_user_language()
return to_locale(lang)