本文整理汇总了Python中babel.core.Locale类的典型用法代码示例。如果您正苦于以下问题:Python Locale类的具体用法?Python Locale怎么用?Python Locale使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Locale类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_negotiate
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: get_metadata
def get_metadata(app, docname):
'''
Extracts metadata from a document.
'''
env = app.builder.env
language = app.config.language
locale = Locale.parse(language) if language else Locale.default()
format_ui_date = partial(
format_date, format=UIStr.TIMESTAMP_FMT, locale=locale)
format_short_ui_short = partial(
format_date, format=UIStr.TIMESTAMP_FMT_SHORT, locale=locale)
env.blog_metadata[docname] = Metadata()
metadata = env.blog_metadata[docname]
# if it's a page
if docname.startswith("pages/"):
metadata.is_page = True
return
# posts are identified by ($YEAR)/($MONTH)/($DAY) paths
match = re.match(r"\d{4}/\d{2}/\d{2}/", docname)
# if not post return
if not match:
return
metadata.is_post = True
metadata.link = docname
metadata.date = datetime.datetime.strptime(match.group(), "%Y/%m/%d/")
# we format date here instead of inside template due to localization issues
# and Python2 vs Python3 incompatibility
metadata.formatted_date = format_ui_date(metadata.date)
metadata.formatted_date_short = format_short_ui_short(metadata.date)
示例3: test_value_error
def test_value_error(self, Locale):
"""
If Locale.parse raises a ValueError, return the en-US locale
object.
"""
Locale.parse.side_effect = ValueError
eq_(helpers.current_locale(), Locale.return_value)
Locale.assert_called_with('en', 'US')
示例4: test_unknown_locale
def test_unknown_locale(self, Locale):
"""
If Locale.parse raises an UnknownLocaleError, return the en-US
locale object.
"""
Locale.parse.side_effect = UnknownLocaleError('foo')
eq_(helpers.current_locale(), Locale.return_value)
Locale.assert_called_with('en', 'US')
示例5: getLocale
def getLocale(self, request):
## FIXME: implement request.locale()
tags = ACCEPT_LANGUAGE.parse(request.environ())
if tags:
try:
return Locale.parse(tags[0], sep='-')
except UnknownLocaleError, e:
try:
return Locale.parse(tags[0])
except UnknownLocaleError, e:
logger.error('Locale parsing error: %s' % e)
return defaultLocale()
示例6: negotiate
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)
示例7: defaultLocale
def defaultLocale():
try:
lang, _ = locale.getdefaultlocale()
except Exception:
lang = None
if lang is not None:
return Locale.parse(lang)
else:
try:
return Locale.default()
except UnknownLocaleError:
return Locale('en', 'US')
示例8: get_locale_data
def get_locale_data():
language = get_language()
if not language:
language = settings.LANGUAGE_CODE
locale_code = to_locale(language)
locale = None
try:
locale = Locale.parse(locale_code)
except (ValueError, UnknownLocaleError):
# Invalid format or unknown locale
# Fallback to the default language
language = settings.LANGUAGE_CODE
locale_code = to_locale(language)
locale = Locale.parse(locale_code)
return locale, locale_code
示例9: format_percent
def format_percent(number, format=None, locale=LC_NUMERIC):
"""Return formatted percent value for a specific locale.
>>> format_percent(0.34, locale='en_US') == u('34%')
True
>>> format_percent(25.1234, locale='en_US') == u('2,512%')
True
>>> format_percent(25.1234, locale='sv_SE') == u('2\\xa0512\\xa0%')
True
The format pattern can also be specified explicitly:
>>> format_percent(25.1234, u('#,##0\u2030'), locale='en_US') == u('25,123\u2030')
True
:param number: the percent number to format
:param format:
:param locale: the `Locale` object or locale identifier
:return: the formatted percent number
:rtype: `unicode`
"""
locale = Locale.parse(locale)
if not format:
format = locale.percent_formats.get(format)
pattern = parse_pattern(format)
return pattern.apply(number, locale)
示例10: format_currency
def format_currency(number, currency, format=None, locale=LC_NUMERIC):
"""Return formatted currency value.
>>> format_currency(1099.98, 'USD', locale='en_US') == u('$1,099.98')
True
>>> format_currency(1099.98, 'USD', locale='es_CO') == u('US$\\xa01.099,98')
True
>>> format_currency(1099.98, 'EUR', locale='de_DE') == u('1.099,98\\xa0\\u20ac')
True
The pattern can also be specified explicitly:
>>> format_currency(1099.98, 'EUR', u('\u00a4\u00a4 #,##0.00'), locale='en_US') == u('EUR 1,099.98')
True
:param number: the number to format
:param currency: the currency code
:param locale: the `Locale` object or locale identifier
:return: the formatted currency value
:rtype: `unicode`
"""
locale = Locale.parse(locale)
if not format:
format = locale.currency_formats.get(format)
pattern = parse_pattern(format)
return pattern.apply(number, locale, currency=currency)
示例11: _language_select
def _language_select(self):
out = {'automatic': _('automatic')}
i18n_dir = os.path.join(xdm.APP_PATH, 'i18n')
# http://stackoverflow.com/questions/800197/get-all-of-the-immediate-subdirectories-in-python
for language in [name for name in os.listdir(i18n_dir) if os.path.isdir(os.path.join(i18n_dir, name))]:
out[language] = Locale.parse(language, sep='_').display_name
return out
示例12: getGlobalPOTimestamp
def getGlobalPOTimestamp(self, locale):
'''
@see: IPOFileManager.getGlobalPOTimestamp
'''
try: locale = Locale.parse(locale)
except UnknownLocaleError: raise InvalidLocaleError(locale)
return self._lastModified(locale)
示例13: format_skeleton
def format_skeleton(skeleton, datetime=None, tzinfo=None, locale=LC_TIME):
r"""Return a time and/or date formatted according to the given pattern.
The skeletons are defined in the CLDR data and provide more flexibility
than the simple short/long/medium formats, but are a bit harder to use.
The are defined using the date/time symbols without order or punctuation
and map to a suitable format for the given locale.
>>> t = datetime(2007, 4, 1, 15, 30)
>>> format_skeleton('MMMEd', t, locale='fr')
u'dim. 1 avr.'
>>> format_skeleton('MMMEd', t, locale='en')
u'Sun, Apr 1'
After the skeleton is resolved to a pattern `format_datetime` is called so
all timezone processing etc is the same as for that.
:param skeleton: A date time skeleton as defined in the cldr data.
:param datetime: the ``time`` or ``datetime`` object; if `None`, the current
time in UTC is used
:param tzinfo: the time-zone to apply to the time for display
:param locale: a `Locale` object or a locale identifier
"""
locale = Locale.parse(locale)
format = locale.datetime_skeletons[skeleton]
return format_datetime(datetime, format, tzinfo, locale)
示例14: babel_date
def babel_date(date, format='long'):
"""
Format a date properly for the current locale. Format can be one of
'short', 'medium', 'long', or 'full'.
"""
locale = Locale.parse(get_language(), sep='-')
return format_date(date, format, locale)
示例15: _babel_locale
def _babel_locale():
"""Return the current locale in Babel's format."""
try:
return Locale.parse(get_language(), sep='-')
except UnknownLocaleError:
# Default to en-US
return Locale('en', 'US')