本文整理汇总了Python中babel.localedata.exists函数的典型用法代码示例。如果您正苦于以下问题:Python exists函数的具体用法?Python exists怎么用?Python exists使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了exists函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_locale_argument_acceptance
def test_locale_argument_acceptance():
# Testing None input.
normalized_locale = localedata.normalize_locale(None)
assert normalized_locale == None
locale_exist = localedata.exists(None)
assert locale_exist == False
# # Testing list input.
normalized_locale = localedata.normalize_locale(['en_us', None])
assert normalized_locale == None
locale_exist = localedata.exists(['en_us', None])
assert locale_exist == False
示例2: get_locale
def get_locale():
locale = flask.request.cookies.get('locale', config.LOCALE_DEFAULT)
if locale not in config.LOCALE:
locale = config.LOCALE_DEFAULT
if not localedata.exists(locale):
locale = 'en'
return locale
示例3: __init__
def __init__(self, language, territory=None, script=None, variant=None):
"""Initialize the locale object from the given identifier components.
>>> locale = Locale('en', 'US')
>>> locale.language
'en'
>>> locale.territory
'US'
:param language: the language code
:param territory: the territory (country or region) code
:param script: the script code
:param variant: the variant code
:raise `UnknownLocaleError`: if no locale data is available for the
requested locale
"""
self.language = language
self.territory = territory
self.script = script
self.variant = variant
self.__data = None
identifier = str(self)
if not localedata.exists(identifier):
raise UnknownLocaleError(identifier)
示例4: get_jinja_env
def get_jinja_env(template_loader, locale):
"""
Set up the Jinja environment,
(In the future we may have another system for providing theming;
for now this is good enough.)
"""
setup_gettext(locale)
# If we have a jinja environment set up with this locale, just
# return that one.
if SETUP_JINJA_ENVS.has_key(locale):
return SETUP_JINJA_ENVS[locale]
template_env = jinja2.Environment(
loader=template_loader, autoescape=True,
extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
template_env.install_gettext_callables(
mg_globals.translations.gettext,
mg_globals.translations.ngettext)
# All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue
template_env.globals['fetch_messages'] = messages.fetch_messages
if exists(locale):
SETUP_JINJA_ENVS[locale] = template_env
return template_env
示例5: _contextual_locale
def _contextual_locale(context):
"""Return locale from the context, falling back to a default if invalid."""
request = context.get("request")
locale = request.LANGUAGE_CODE
if not localedata.exists(locale):
locale = settings.LANGUAGE_CODE
return locale
示例6: __init__
def __init__(self, language, territory = None, script = None, variant = None):
self.language = language
self.territory = territory
self.script = script
self.variant = variant
self.__data = None
identifier = str(self)
if not localedata.exists(identifier):
raise UnknownLocaleError(identifier)
示例7: _format_decimal
def _format_decimal(num, format=None):
"""Returns the string of a number formatted for the current language.
Uses django's translation.get_language() to find the current language from
the request.
Falls back to the default language if babel does not support the current.
"""
lang = translation.get_language()
if not localedata.exists(lang):
lang = settings.LANGUAGE_CODE
locale = Locale(translation.to_locale(lang))
return Format(locale).decimal(num, format)
示例8: get_jinja_env
def get_jinja_env(template_loader, locale):
"""
Set up the Jinja environment,
(In the future we may have another system for providing theming;
for now this is good enough.)
"""
set_thread_locale(locale)
# If we have a jinja environment set up with this locale, just
# return that one.
if locale in SETUP_JINJA_ENVS:
return SETUP_JINJA_ENVS[locale]
# jinja2.StrictUndefined will give exceptions on references
# to undefined/unknown variables in templates.
template_env = jinja2.Environment(
loader=template_loader,
autoescape=True,
undefined=jinja2.StrictUndefined,
extensions=["jinja2.ext.i18n", "jinja2.ext.autoescape", TemplateHookExtension],
)
template_env.install_gettext_callables(
mg_globals.thread_scope.translations.ugettext, mg_globals.thread_scope.translations.ungettext
)
# All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue
# ... construct a grid of thumbnails or other media
# ... have access to the global and app config
template_env.globals["fetch_messages"] = messages.fetch_messages
template_env.globals["app_config"] = mg_globals.app_config
template_env.globals["global_config"] = mg_globals.global_config
template_env.globals["version"] = _version.__version__
template_env.globals["auth"] = mg_globals.app.auth
template_env.filters["urlencode"] = url_quote_plus
# add human readable fuzzy date time
template_env.globals["timesince"] = timesince
# allow for hooking up plugin templates
template_env.globals["get_hook_templates"] = get_hook_templates
template_env.globals = hook_transform("template_global_context", template_env.globals)
if exists(locale):
SETUP_JINJA_ENVS[locale] = template_env
return template_env
示例9: __init__
def __init__(self):
from pylons import config
# Get names of the locales
# (must be a better way than scanning for i18n directory?)
known_locales = ["en"] + [
locale_name for locale_name in os.listdir(i18n_path) if localedata.exists(locale_name)
]
self._locale_names, self._default_locale_name = self._work_out_locales(known_locales, config)
self._locale_objects = map(Locale.parse, self._locale_names)
self._default_locale_object = Locale.parse(self._default_locale_name)
self._aliases = LOCALE_ALIASES
self._aliases["pt"] = "pt_BR" # Default Portuguese language to
示例10: _get_locales
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__)
locales += [l for l in os.listdir(i18n_path) if localedata.exists(l)]
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
示例11: get_gettext_translation
def get_gettext_translation(locale):
"""
Return the gettext instance based on this locale
"""
# Later on when we have plugins we may want to enable the
# multi-translations system they have so we can handle plugin
# translations too
# TODO: fallback nicely on translations from pt_PT to pt if not
# available, etc.
if locale in SETUP_GETTEXTS:
this_gettext = SETUP_GETTEXTS[locale]
else:
this_gettext = gettext.translation(
'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True)
if localedata.exists(locale):
SETUP_GETTEXTS[locale] = this_gettext
return this_gettext
示例12: setup_gettext
def setup_gettext(locale):
"""
Setup the gettext instance based on this locale
"""
# Later on when we have plugins we may want to enable the
# multi-translations system they have so we can handle plugin
# translations too
# TODO: fallback nicely on translations from pt_PT to pt if not
# available, etc.
if SETUP_GETTEXTS.has_key(locale):
this_gettext = SETUP_GETTEXTS[locale]
else:
this_gettext = gettext.translation(
'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True)
if exists(locale):
SETUP_GETTEXTS[locale] = this_gettext
mg_globals.setup_globals(
translations=this_gettext)
示例13: format_price
def format_price(value, currency):
"""
Format decimal value as currency
"""
try:
value = Decimal(value)
except (TypeError, InvalidOperation):
return ''
language = get_language()
if not language:
language = settings.LANGUAGE_CODE
locale_code = to_locale(language)
if not localedata.exists(locale_code):
localedata.load(locale_code)
locale = Locale(locale_code)
currency_format = locale.currency_formats.get('standard')
pattern = currency_format.pattern
pattern = re.sub(
'(\xa4+)', '<span class="currency">\\1</span>', pattern)
result = format_currency(value, currency, pattern, locale=locale_code)
return mark_safe(result)
示例14: get_jinja_env
def get_jinja_env(template_loader, locale):
"""
Set up the Jinja environment,
(In the future we may have another system for providing theming;
for now this is good enough.)
"""
mg_globals.thread_scope.translations = get_gettext_translation(locale)
# If we have a jinja environment set up with this locale, just
# return that one.
if SETUP_JINJA_ENVS.has_key(locale):
return SETUP_JINJA_ENVS[locale]
# jinja2.StrictUndefined will give exceptions on references
# to undefined/unknown variables in templates.
template_env = jinja2.Environment(
loader=template_loader, autoescape=True,
undefined=jinja2.StrictUndefined,
extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
template_env.install_gettext_callables(
mg_globals.thread_scope.translations.ugettext,
mg_globals.thread_scope.translations.ungettext)
# All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue
# ... construct a grid of thumbnails or other media
# ... have access to the global and app config
template_env.globals['fetch_messages'] = messages.fetch_messages
template_env.globals['app_config'] = mg_globals.app_config
template_env.globals['global_config'] = mg_globals.global_config
template_env.filters['urlencode'] = url_quote_plus
if exists(locale):
SETUP_JINJA_ENVS[locale] = template_env
return template_env
示例15: entry
def entry():
if not request.args.get('id'):
return jsonerror("Missing required argument 'id'.", 400)
if request.args['id'] not in db['entries']:
return jsonerror('Undefined entry.', 404)
if 'locale' in request.args and \
not localedata.exists(request.args['locale']):
return jsonerror('Invalid locale.', 400)
entry = db['entries'][request.args['id']]
data = dict(id=entry.id, type=entry.type)
if hasattr(entry, 'affixes'):
data['affixes'] = [affix.encode('utf-8') for affix in entry.affixes]
if hasattr(entry, 'class_'):
data['class'] = entry.class_
locale = request.args.get('locale')
if entry.history(locale):
definition = entry.history(locale).newest.object
data['definition'] = definition.definition
data['notes'] = definition.notes
return jsonify(data)