def test_locale(self): counter = 1 for lang in LOCALE_ALIASES.keys(): country = LOCALE_ALIASES[lang][3:] language = Language(id=counter, lang=lang, country=country, enabled=True) self.session.add(language) counter = counter + 1 try: loc = Locale(lang, country) except UnknownLocaleError: try: loc = Locale(lang) except UnknownLocaleError: loc = None self.assertEqual(loc, language.locale) for i in xrange(0, 1000): generated_lang = LOCALE_ALIASES.keys()[0] while generated_lang in LOCALE_ALIASES.keys(): generated_lang = "".join(random.sample(string.letters, 2)) generated_lang = generated_lang.lower() language = Language(id=counter, lang=generated_lang, country=generated_lang, enabled=True) self.session.add(language) counter = counter + 1 loc = None try: loc = Locale(generated_lang, generated_lang.upper()) except UnknownLocaleError: try: loc = Locale(generated_lang) except UnknownLocaleError: loc = None self.assertEqual(loc, language.locale)
babel.Locale('ru', 'RU'), babel.Locale.parse('zh_TW') ] LOCALE_STRINGS = map(str, LOCALES) # Babel language negotiation can only compare language codes using the same # separator. As we use underscores and browsers send dashes, we convert our # static list to use dashes as well for the negotiation. LOCALE_STRINGS_DASH = map(lambda l: string.replace(l, '_', '-'), LOCALE_STRINGS) # We have only Brazilian Portuguese, so we show that when pt is requested. # We have only Traditional Chinese (Taiwan), so we show that when zh is # requested. A2_LOCALE_ALIASES = LOCALE_ALIASES.copy() A2_LOCALE_ALIASES['pt'] = 'pt_BR' A2_LOCALE_ALIASES['zh'] = 'zh_Hant_TW' A2_LOCALE_ALIASES['zh-tw'] = 'zh_Hant_TW' FALLBACK_TZ = 'Europe/Berlin' def get_enabled_locales(): enabled_locales = config.get_list('adhocracy.enabled_locales') if enabled_locales is None: return LOCALES else: return filter(lambda x: x.language in enabled_locales, LOCALES)
from unicodedata import combining, normalize from aspen import Response from aspen.simplates.pagination import parse_specline, split_and_escape from aspen.utils import utcnow from babel.core import LOCALE_ALIASES, Locale from babel.dates import format_timedelta from babel.messages.extract import extract_python from babel.messages.pofile import Catalog from babel.numbers import (format_currency, format_decimal, format_number, format_percent, get_decimal_symbol, parse_decimal) from collections import OrderedDict from dependency_injection import resolve_dependencies import jinja2.ext ALIASES = {k: v.lower() for k, v in LOCALE_ALIASES.items()} ALIASES_R = {v: k for k, v in ALIASES.items()} def strip_accents(s): return ''.join(c for c in normalize('NFKD', s) if not combining(c)) def make_sorted_dict(keys, d): items = ((k, d[k]) for k in keys) return OrderedDict(sorted(items, key=lambda t: strip_accents(t[1]))) COUNTRY_CODES = """ AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR
import re from unicodedata import combining, normalize from aspen.resources.pagination import parse_specline, split_and_escape from aspen.utils import utcnow from babel.core import LOCALE_ALIASES from babel.dates import format_timedelta from babel.messages.extract import extract_python from babel.numbers import ( format_currency, format_decimal, format_number, format_percent, get_decimal_symbol, parse_decimal ) import jinja2.ext ALIASES = {k: v.lower() for k, v in LOCALE_ALIASES.items()} ALIASES_R = {v: k for k, v in ALIASES.items()} LOCALES = {} LOCALE_EN = None ternary_re = re.compile(r'^\(? *(.+?) *\? *(.+?) *: *(.+?) *\)?$') and_re = re.compile(r' *&& *') or_re = re.compile(r' *\|\| *') def ternary_sub(m): g1, g2, g3 = m.groups() return '%s if %s else %s' % (g2, g1, ternary_re.sub(ternary_sub, g3))
babel.Locale('ro', 'RO'), babel.Locale('ru', 'RU'), babel.Locale.parse('zh_TW')] LOCALE_STRINGS = map(str, LOCALES) # Babel language negotiation can only compare language codes using the same # separator. As we use underscores and browsers send dashes, we convert our # static list to use dashes as well for the negotiation. LOCALE_STRINGS_DASH = map(lambda l: string.replace(l, '_', '-'), LOCALE_STRINGS) # We have only Brazilian Portuguese, so we show that when pt is requested. # We have only Traditional Chinese (Taiwan), so we show that when zh is # requested. A2_LOCALE_ALIASES = LOCALE_ALIASES.copy() A2_LOCALE_ALIASES['pt'] = 'pt_BR' A2_LOCALE_ALIASES['zh'] = 'zh_Hant_TW' A2_LOCALE_ALIASES['zh-tw'] = 'zh_Hant_TW' FALLBACK_TZ = 'Europe/Berlin' @cache.memoize('_translations_root') def _get_translations_root(): translations_module = config.get('adhocracy.translations') translations_module_loader = pkgutil.get_loader(translations_module) if translations_module_loader is None: raise ValueError(('Cannot import the module "%s" configured for ' '"adhocracy.translations". Make sure it is an ' 'importable module (and contains the '
def test_locales(self): l = Language(id=1, lang=u'it', country=u'it', enabled=True) locales = [] for locale in l.locales: locales.append(locale) self.assertIn(Locale(u'it', u'IT'), locales) self.assertIn(Locale(u'it'), locales) counter = 1 for lang in LOCALE_ALIASES.keys(): country = LOCALE_ALIASES[lang][3:] language = Language(id=counter, lang=lang, country=country, enabled=True) self.session.add(language) counter = counter + 1 locale_list = [] try: loc = Locale(lang, country) locale_list.append(loc) except UnknownLocaleError: pass try: loc = Locale(lang) locale_list.append(loc) except UnknownLocaleError: pass locales = [] for l in language.locales: locales.append(l) for loc in locale_list: self.assertIn(loc, locales) for i in xrange(0, 1000): generated_lang = LOCALE_ALIASES.keys()[0] while generated_lang in LOCALE_ALIASES.keys(): generated_lang = "".join(random.sample(string.letters, 2)) generated_lang = generated_lang.lower() language = Language(id=counter, lang=generated_lang, country=generated_lang, enabled=True) self.session.add(language) counter = counter + 1 locale_list = [] try: loc = Locale(generated_lang, generated_lang.upper()) locale_list.append(loc) except UnknownLocaleError: pass try: loc = Locale(generated_lang) locale_list.append(loc) except UnknownLocaleError: pass locales = [] for l in language.locales: locales.append(l) for loc in locale_list: self.assertIn(loc, locales)