本文整理汇总了Python中babel.localedata.locale_identifiers函数的典型用法代码示例。如果您正苦于以下问题:Python locale_identifiers函数的具体用法?Python locale_identifiers怎么用?Python locale_identifiers使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了locale_identifiers函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unique_ids
def test_unique_ids():
# Check all locale IDs are uniques.
all_ids = localedata.locale_identifiers()
assert len(all_ids) == len(set(all_ids))
# Check locale IDs don't collide after lower-case normalization.
lower_case_ids = list(map(methodcaller('lower'), all_ids))
assert len(lower_case_ids) == len(set(lower_case_ids))
示例2: pytest_generate_tests
def pytest_generate_tests(metafunc):
if hasattr(metafunc.function, "pytestmark"):
for mark in metafunc.function.pytestmark:
if mark.name == "all_locales":
from babel.localedata import locale_identifiers
metafunc.parametrize("locale", list(locale_identifiers()))
break
示例3: get_available_languages
def get_available_languages(domain):
"""Lists the available languages for the given translation domain.
:param domain: the domain to get languages for
"""
if domain in _AVAILABLE_LANGUAGES:
return copy.copy(_AVAILABLE_LANGUAGES[domain])
localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
find = lambda x: gettext.find(domain,
localedir=localedir,
languages=[x])
# NOTE(mrodden): en_US should always be available (and first in case
# order matters) since our in-line message strings are en_US
language_list = ['en_US']
locale_identifiers = localedata.locale_identifiers()
language_list.extend(language for language in locale_identifiers
if find(language))
# In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
# locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
# necessary so that they are listed as supported.
aliases = {'zh': 'zh_CN',
'zh_Hant_HK': 'zh_HK',
'zh_Hant': 'zh_TW',
'fil': 'tl_PH'}
language_list.extend(alias for locale, alias in six.iteritems(aliases)
if (locale in language_list and
alias not in language_list))
_AVAILABLE_LANGUAGES[domain] = language_list
return copy.copy(language_list)
示例4: __init__
def __init__(self, queryIndexer):
'''
Construct the query criteria service.
'''
assert isinstance(queryIndexer, QueryIndexer), 'Invalid QueryIndexer %s' % queryIndexer
self.queryIndexer = queryIndexer
self._locales = {code:Locale.parse(code) for code in locale_identifiers()}
示例5: test_activate_unavailable_locale
def test_activate_unavailable_locale(self):
unavailables = sorted(set(locale_identifiers()) -
set(translation.get_available_locales())) or \
('en_US',)
locale_dir = self._get_locale_dir()
translation.add_domain('catalog1', self.env.path, locale_dir)
translation.add_domain('catalog2', self.env.path, locale_dir)
translation.activate(unavailables[0], self.env.path)
示例6: set_available_locales
def set_available_locales():
"""Set available locales for which we have translations"""
global AVAILABLE_LOCALES
locales=['en', 'en_US'] # these are available without translations
for locale in localedata.locale_identifiers():
if gettext.find('mediagoblin', TRANSLATIONS_PATH, [locale]):
locales.append(locale)
AVAILABLE_LOCALES = locales
示例7: __init__
def __init__(self):
'''
Construct the language service.
'''
EntityNQServiceAlchemy.__init__(self, LanguageEntity)
locales = [(code, Locale.parse(code)) for code in locale_identifiers()]
locales.sort(key=lambda pack: pack[0])
self._locales = OrderedDict(locales)
validateProperty(LanguageEntity.Code, self._validateCode)
示例8: run
def run(self, argv=None):
"""Main entry point of the command-line interface.
:param argv: list of arguments passed on the command-line
"""
if argv is None:
argv = sys.argv
self.parser = optparse.OptionParser(usage=self.usage % ("command", "[args]"), version=self.version)
self.parser.disable_interspersed_args()
self.parser.print_help = self._help
self.parser.add_option(
"--list-locales", dest="list_locales", action="store_true", help="print all known locales and exit"
)
self.parser.add_option(
"-v",
"--verbose",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
help="print as much as possible",
)
self.parser.add_option(
"-q",
"--quiet",
action="store_const",
dest="loglevel",
const=logging.ERROR,
help="print as little as possible",
)
self.parser.set_defaults(list_locales=False, loglevel=logging.INFO)
options, args = self.parser.parse_args(argv[1:])
self._configure_logging(options.loglevel)
if options.list_locales:
identifiers = localedata.locale_identifiers()
longest = max([len(identifier) for identifier in identifiers])
identifiers.sort()
format = u"%%-%ds %%s" % (longest + 1)
for identifier in identifiers:
locale = Locale.parse(identifier)
output = format % (identifier, locale.english_name)
print(output.encode(sys.stdout.encoding or getpreferredencoding() or "ascii", "replace"))
return 0
if not args:
self.parser.error("no valid command or option passed. " "Try the -h/--help option for more information.")
cmdname = args[0]
if cmdname not in self.commands:
self.parser.error('unknown command "%s"' % cmdname)
return self._dispatch(cmdname, args[1:])
示例9: test_locale_identifiers_cache
def test_locale_identifiers_cache(monkeypatch):
original_listdir = localedata.os.listdir
listdir_calls = []
def listdir_spy(*args):
rv = original_listdir(*args)
listdir_calls.append((args, rv))
return rv
monkeypatch.setattr(localedata.os, 'listdir', listdir_spy)
# In case we've already run some tests...
if hasattr(localedata.locale_identifiers, 'cache'):
del localedata.locale_identifiers.cache
assert not listdir_calls
assert localedata.locale_identifiers()
assert len(listdir_calls) == 1
assert localedata.locale_identifiers() is localedata.locale_identifiers.cache
assert len(listdir_calls) == 1
localedata.locale_identifiers.cache = None
assert localedata.locale_identifiers()
assert len(listdir_calls) == 2
示例10: run
def run(self, argv=None):
"""Main entry point of the command-line interface.
:param argv: list of arguments passed on the command-line
"""
if argv is None:
argv = sys.argv
self.parser = optparse.OptionParser(usage=self.usage % ('command', '[args]'),
version=self.version)
self.parser.disable_interspersed_args()
self.parser.print_help = self._help
self.parser.add_option('--list-locales', dest='list_locales',
action='store_true',
help="print all known locales and exit")
self.parser.add_option('-v', '--verbose', action='store_const',
dest='loglevel', const=logging.DEBUG,
help='print as much as possible')
self.parser.add_option('-q', '--quiet', action='store_const',
dest='loglevel', const=logging.ERROR,
help='print as little as possible')
self.parser.set_defaults(list_locales=False, loglevel=logging.INFO)
options, args = self.parser.parse_args(argv[1:])
self._configure_logging(options.loglevel)
if options.list_locales:
identifiers = localedata.locale_identifiers()
longest = max([len(identifier) for identifier in identifiers])
identifiers.sort()
format = u'%%-%ds %%s' % (longest + 1)
for identifier in identifiers:
locale = Locale.parse(identifier)
output = format % (identifier, locale.english_name)
print(output.encode(sys.stdout.encoding or
getpreferredencoding() or
'ascii', 'replace'))
return 0
if not args:
self.parser.error('no valid command or option passed. '
'Try the -h/--help option for more information.')
cmdname = args[0]
if cmdname not in self.commands:
self.parser.error('unknown command "%s"' % cmdname)
cmdinst = self._configure_command(cmdname, args[1:])
return cmdinst.run()
示例11: get_available_languages
def get_available_languages(domain):
"""Lists the available languages for the given translation domain.
:param domain: the domain to get languages for
"""
if domain in _AVAILABLE_LANGUAGES:
return copy.copy(_AVAILABLE_LANGUAGES[domain])
localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
find = lambda x: gettext.find(domain,
localedir=localedir,
languages=[x])
# NOTE(mrodden): en_US should always be available (and first in case
# order matters) since our in-line message strings are en_US
language_list = ['en_US']
locale_identifiers = localedata.locale_identifiers()
language_list.extend(language for language in locale_identifiers
if find(language))
# In Babel 1.3, locale_identifiers() doesn't list some OpenStack supported
# locales (e.g. 'zh_CN', and 'zh_TW') so we add the locales explicitly if
# necessary so that they are listed as supported.
aliases = {'zh': 'zh_CN',
'zh_Hant_HK': 'zh_HK',
'zh_Hant': 'zh_TW',
'fil': 'tl_PH'}
language_list.extend(alias for locale, alias in aliases.items()
if (locale in language_list and
alias not in language_list))
language_list.extend(alias for locale, alias in aliases.items()
if (locale not in language_list and
find(alias)))
# In webob.acceptparse, the best_match is just match the first element in
# the language_list, so make the precise element in front
result = ['en_US']
for i in language_list[1:]:
if '_' in i:
result.insert(1, i)
else:
result.append(i)
_AVAILABLE_LANGUAGES[domain] = result
return copy.copy(result)
示例12: get_cldr_languages
def get_cldr_languages(self):
"""
Helper function to extract CLDR information. Heavily relied on babel functionality
"""
from babel import Locale, localedata, plural
for lang in localedata.locale_identifiers():
locale = Locale(lang)
if not locale.english_name:
continue
plurals_str = plural.to_gettext(locale.plural_form)
nplurals, pluralequation = re_plurals.match(plurals_str).groups()
lang_aliases = set(CLDR_FALLBACK_ALIASES.get(lang, []))
lang_aliases.add(lang)
for alias in lang_aliases:
yield {
'code': alias,
'fullname': locale.english_name,
'nplurals': int(nplurals),
'pluralequation': pluralequation,
}
示例13: join
import sys
import os
from decimal import Decimal
from collections import defaultdict
from gettext import translation
from babel import Locale, UnknownLocaleError, localedata
from babel.numbers import parse_decimal, format_currency, format_decimal
from bottle import request
from functools import lru_cache
from os.path import join
from lib import root_directory
LOCALE_DIR = join(root_directory(), 'backend', 'locale')
DEFAULT_LANGUAGE = conf.ui.default_language
SUPPORTED_LOCALES = frozenset(localedata.locale_identifiers())
APPLICATION_NAME = "boss"
def _(message):
return message
def acceptable_languages():
languages = languages_set_from_headers()
languages.intersection_update(available_languages())
languages -= {DEFAULT_LANGUAGE}
return list(languages) if languages else [DEFAULT_LANGUAGE]
示例14: test_format_scientific_quantization
def test_format_scientific_quantization():
# Test all locales.
for locale_code in localedata.locale_identifiers():
assert numbers.format_scientific(
'0.9999999999', locale=locale_code, decimal_quantization=False).find('999999999') > -1
示例15: test_format_currency_long_display_name_all
def test_format_currency_long_display_name_all():
for locale_code in localedata.locale_identifiers():
assert numbers.format_currency(
1, 'USD', locale=locale_code, format_type='name').find('1') > -1
assert numbers.format_currency(
'1', 'USD', locale=locale_code, format_type='name').find('1') > -1