本文整理汇总了Python中babel.Locale.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Locale.parse方法的具体用法?Python Locale.parse怎么用?Python Locale.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.Locale
的用法示例。
在下文中一共展示了Locale.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: list_translations
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def list_translations(self):
"""Returns a list of all the locales translations exist for. The
list returned will be filled with actual locale objects and not just
strings.
.. versionadded:: 0.6
"""
result = []
for dirname in self.translation_directories:
if not os.path.isdir(dirname):
continue
for folder in os.listdir(dirname):
locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
if not os.path.isdir(locale_dir):
continue
if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
result.append(Locale.parse(folder))
# If not other translations are found, add the default locale.
if not result:
result.append(Locale.parse(self._default_locale))
return result
示例2: _set_language_dropdown_values
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def _set_language_dropdown_values(self, initial_locale=None):
log.debug('Setting languages dropdown menu for %r' % initial_locale)
available_locales = self._find_available_locales()
locale = Locale(initial_locale)
if not locale:
locale = Locale.parse(initial_locale)
languages = []
current_lang = h.get_lang()[0]
for loc in available_locales:
selected = False
if loc == current_lang:
selected = True
local_locale = Locale(loc)
if not local_locale:
local_locale = Locale.parse(loc)
languages.append((loc, local_locale.get_display_name(locale), selected))
return locale, languages
for loc, territory in available_locales:
selected = False
language = locale.languages[loc].capitalize()
if territory:
country = u'(%s)' % locale.territories[territory]
value = ['%s_%s' % (loc, territory),
u'%s %s' % (language, country)]
if value[0] == current_lang:
selected = True
else:
value = [loc, language]
if value[0] == current_lang:
selected = True
languages.append( value + [selected])
return locale, languages
示例3: __call__
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def __call__(self, value):
normalized_value = value.replace(u"-", u"_")
try:
BabelLocale.parse(normalized_value)
return normalized_value
except (ValueError, UnknownLocaleError):
raise ValidateError(_(u"Incorrect locale {}"), value)
示例4: test_i18n_parse_date_datetime_meridiem
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def test_i18n_parse_date_datetime_meridiem(self):
tz = datefmt.timezone('GMT +2:00')
expected_am = datetime.datetime(2011, 2, 22, 0, 45, 56, 0, tz)
expected_pm = datetime.datetime(2011, 2, 22, 12, 45, 56, 0, tz)
en_US = Locale.parse('en_US')
zh_CN = Locale.parse('zh_CN')
self.assertEqual(expected_am,
datefmt.parse_date('Feb 22, 2011 0:45:56 AM', tz,
en_US))
self.assertEqual(expected_am,
datefmt.parse_date('Feb 22, 2011 12:45:56 AM', tz,
en_US))
self.assertEqual(expected_am,
datefmt.parse_date(u'2011-2-22 上午0:45:56', tz,
zh_CN))
self.assertEqual(expected_am,
datefmt.parse_date(u'2011-2-22 上午12:45:56', tz,
zh_CN))
self.assertEqual(expected_pm,
datefmt.parse_date('Feb 22, 2011 0:45:56 PM', tz,
en_US))
self.assertEqual(expected_pm,
datefmt.parse_date('Feb 22, 2011 12:45:56 PM', tz,
en_US))
self.assertEqual(expected_pm,
datefmt.parse_date(u'2011-2-22 下午0:45:56', tz,
zh_CN))
self.assertEqual(expected_pm,
datefmt.parse_date(u'2011-2-22 下午12:45:56', tz,
zh_CN))
示例5: handle_request
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [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
示例6: _get_locales
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def _get_locales():
# FIXME this wants cleaning up and merging with get_locales_from_config()
assert not config.get('lang'), \
('"lang" config option not supported - please use ckan.locale_default '
'instead.')
locales_offered = config.get('ckan.locales_offered', '').split()
filtered_out = config.get('ckan.locales_filtered_out', '').split()
locale_default = config.get('ckan.locale_default', 'en')
locale_order = config.get('ckan.locale_order', '').split()
locales = ['en']
if config.get('ckan.i18n_directory'):
i18n_path = os.path.join(config.get('ckan.i18n_directory'), 'i18n')
else:
i18n_path = os.path.dirname(ckan.i18n.__file__)
# For every file in the ckan i18n directory see if babel can understand
# the locale. If yes, add it to the available locales
for locale in os.listdir(i18n_path):
try:
Locale.parse(locale)
locales.append(locale)
except (ValueError, UnknownLocaleError):
# Babel does not know how to make a locale out of this.
# This is fine since we are passing all files in the
# ckan.i18n_directory here which e.g. includes the __init__.py
pass
assert locale_default in locales, \
'default language "%s" not available' % locale_default
locale_list = []
for locale in locales:
# no duplicates
if locale in locale_list:
continue
# if offered locales then check locale is offered
if locales_offered and locale not in locales_offered:
continue
# remove if filtered out
if locale in filtered_out:
continue
# ignore the default as it will be added first
if locale == locale_default:
continue
locale_list.append(locale)
# order the list if specified
ordered_list = [locale_default]
for locale in locale_order:
if locale in locale_list:
ordered_list.append(locale)
# added so remove from our list
locale_list.remove(locale)
# add any remaining locales not ordered
ordered_list += locale_list
return ordered_list
示例7: test_parse_likely_subtags
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def test_parse_likely_subtags(self):
l = Locale.parse('zh-TW', sep='-')
assert l.language == 'zh'
assert l.territory == 'TW'
assert l.script == 'Hant'
l = Locale.parse('und_AT')
assert l.language == 'de'
assert l.territory == 'AT'
示例8: check_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def check_locale(locale):
if locale:
try:
Locale.parse(locale)
except ValueError:
return None
else:
return locale
return None
示例9: select_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def select_locale(choices):
"""Selects a locale."""
enabled = set(settings.LANGUAGE_SECTIONS)
for locale, quality in choices:
try:
locale = Locale.parse(locale, sep='-')
except UnknownLocaleError:
continue
if str(locale) in enabled and \
find_catalog(locale) is not None:
return locale
return Locale.parse(settings.DEFAULT_LANGUAGE)
示例10: select_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def select_locale(choices):
"""Selects a locale."""
enabled = set(_settings['sections'])
for locale, quality in choices:
try:
locale = Locale.parse(locale, sep='-')
except UnknownLocaleError:
continue
if str(locale) in enabled and \
find_catalog(locale) is not None:
return locale
return Locale.parse(_settings['default_language'])
示例11: list_translations
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def list_translations(dirname):
if not os.path.isdir(dirname):
return []
result = []
for entry in scandir(dirname):
locale_dir = os.path.join(entry.path, 'LC_MESSAGES')
if not os.path.isdir(locale_dir):
continue
if filter(lambda x: x.name.endswith('.mo'), scandir(locale_dir)):
result.append(Locale.parse(entry.name))
if not result:
result.append(Locale.parse(self._default_locale))
return result
示例12: list_translations
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def list_translations(dirname):
if not os.path.isdir(dirname):
return []
result = []
for folder in os.listdir(dirname):
locale_dir = os.path.join(dirname, folder, 'LC_MESSAGES')
if not os.path.isdir(locale_dir):
continue
if filter(lambda x: x.endswith('.mo'), os.listdir(locale_dir)):
result.append(Locale.parse(folder))
if not result:
result.append(Locale.parse(self._default_locale))
return result
示例13: locales
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def locales(self):
"""
returns a dict of locale codes to locale display names in both the current locale and the localized locale
example: if the current locale is es_ES then locales['en_US'] = 'Ingles (Estados Unidos) - English (United States)'
"""
locales = {}
for l in config.locales:
current_locale = Locale.parse(self.locale)
language = current_locale.languages[l.split('_')[0]]
territory = current_locale.territories[l.split('_')[1]]
localized_locale_name = Locale.parse(l).display_name.capitalize()
locales[l] = language.capitalize() + " (" + territory.capitalize() + ") - " + localized_locale_name
return locales
示例14: check_locale
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def check_locale(locale):
if locale:
try:
Locale.parse(locale)
except ValueError:
return None
except UnknownLocaleError:
return None
except Exception:
traceback.print_exc()
return None
else:
return locale
return None
示例15: test_parse_invalid_date
# 需要导入模块: from babel import Locale [as 别名]
# 或者: from babel.Locale import parse [as 别名]
def test_parse_invalid_date(self):
tz = datefmt.timezone('GMT +2:00')
en_US = Locale.parse('en_US')
self.assertRaises(TracError, datefmt.parse_date,
'',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'2011 Apr Mar',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'29 Feb',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'Feb 2011',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'29 Feb 2010',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'29 Xxx 2012',
tzinfo=tz, locale=en_US, hint='date')
self.assertRaises(TracError, datefmt.parse_date,
'29 Xxx 2012 4:00:00 AM',
tzinfo=tz, locale=en_US, hint='datetime')
self.assertRaises(TracError, datefmt.parse_date,
'29 2012 4:01:02 AM Feb',
tzinfo=tz, locale=en_US, hint='datetime')
self.assertRaises(TracError, datefmt.parse_date,
'29 2012 4:00 Feb',
tzinfo=tz, locale=en_US, hint='datetime')