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
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))
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)
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
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
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'
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
def check_locale(locale): if locale: try: Locale.parse(locale) except ValueError: return None else: return locale return None
def load_translations(self): """load all translations for all languages, modules and the app. In order to have everything properly cached we will load all translations into memory. We will also merge module based catalogs into one main catalog. This means that we first fill the catalog with the module catalog and will then merge the app on top so you have the possibility to override certain translations from modules. """ self.all_locales = set() # all locale objects we know about self.catalogs = {} # mapping from locale object to merged translations # go through the modules and try to load their catalogs for module in self.app.modules: dirname = pkg_resources.resource_filename(module.import_name, "translations") if not os.path.exists(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)): l = Locale.parse(folder) self.all_locales.add(str(l)) trans = support.Translations.load(dirname, l) if str(l) not in self.catalogs: self.catalogs[str(l)] = trans else: # we merge if it exists already self.catalogs[str(l)].merge(trans) # now for the app dirname = pkg_resources.resource_filename(self.app.import_name, "translations") 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)): l = Locale.parse(folder) self.all_locales.add(str(l)) # load all domains for f in os.listdir(locale_dir): if f.endswith(".mo"): domain = os.path.splitext(f)[0] trans = support.Translations.load(dirname, l, domain = domain) if str(l) not in self.catalogs: self.catalogs[str(l)] = trans else: # we merge if it exists already self.catalogs[str(l)].merge(trans)
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'])
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)
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
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
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
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
def test_smoke_dates(locale): locale = Locale.parse(locale) instant = datetime.now() for width in ("full", "long", "medium", "short"): assert dates.format_date(instant, format=width, locale=locale) assert dates.format_datetime(instant, format=width, locale=locale) assert dates.format_time(instant, format=width, locale=locale)
def _lang_from_changed_cb(self, combo): lang_iter = combo.get_active_iter() if lang_iter is not None: model = combo.get_model() code, lang = model[lang_iter][:2] # Remove all the old choices self.lang_to.get_model().clear() to_langs = set() for to_lang in self.client.languages_from(from_lang=code): try: to_locale = Locale.parse(to_lang) to_name = to_locale.get_language_name(self.locale) except (babel.UnknownLocaleError, ValueError): # Fall back to language code to_name = to_lang self._logger.error('Failed to get locale for %s', to_lang) to_langs.add((to_lang, to_name)) for lang in sorted(list(to_langs), (lambda x, y: cmp(x[1], y[1]))): self.lang_to.get_model().append(lang) self.lang_to.set_active(0)
def get_languages(): from babel import Locale from babel.core import UnknownLocaleError langs = [] dir_names = [lang for lang in os.listdir(translations_path()) if lang != 'en_US'] for name in dir_names: try: Locale.parse(name) except UnknownLocaleError: pass else: langs.append(name) langs.sort() langs.insert(0, 'en_US') return langs
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')
def language_from_locale(code, default=DEFAULT_LANGUAGE): if not code: return default try: return Locale.parse(code.replace(u'-', u'_').strip()).language except (ValueError, UnknownLocaleError, TypeError): return default
def has_section(language): """Does this language have a section?""" try: language = str(Locale.parse(language)) except UnknownLocaleError: return False return language in settings.LANGUAGE_SECTIONS
def init(self, argv): """Subcommand for creating new message catalogs from a template. :param argv: the command arguments """ parser = OptionParser(usage=self.usage % ('init', ''), description=self.commands['init']) parser.add_option('--domain', '-D', dest='domain', help="domain of PO file (default '%default')") parser.add_option('--input-file', '-i', dest='input_file', metavar='FILE', help='name of the input file') parser.add_option('--output-dir', '-d', dest='output_dir', metavar='DIR', help='path to output directory') parser.add_option('--output-file', '-o', dest='output_file', metavar='FILE', help="name of the output file (default " "'<output_dir>/<locale>/LC_MESSAGES/" "<domain>.po')") parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', help='locale for the new localized catalog') parser.set_defaults(domain='messages') options, args = parser.parse_args(argv) if not options.locale: parser.error('you must provide a locale for the new catalog') try: locale = Locale.parse(options.locale) except UnknownLocaleError, e: parser.error(e)
def get_language_pairs(): languages = get_available_languages() langlist = zip(languages, [Locale.parse(lang).display_name for lang in languages]) # Filter out bogus language codes before they screw us over completely # (Hint: 'no' is not a real language code) return filter(lambda (language, name): name is not None, langlist)
def test_template_debug_conditions(self): request = mock.Mock() request.user = self.user request.locale = Locale.parse('en_US') from django.conf import settings settings.DEBUG = False settings.TEMPLATE_DEBUG = True context = global_context(request) self.assertFalse(context['is_debug']) settings.DEBUG = True settings.TEMPLATE_DEBUG = False context = global_context(request) self.assertFalse(context['is_debug']) settings.DEBUG = False settings.TEMPLATE_DEBUG = False context = global_context(request) self.assertFalse(context['is_debug']) settings.DEBUG = True settings.TEMPLATE_DEBUG = True context = global_context(request) self.assertTrue(context['is_debug'])
def fixed_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. """ 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 dirs = additional_folders + [os.path.join(self.app.root_path, 'translations')] result = [Locale.parse(default_locale)] for dir in dirs: result += list_translations(dir) return result
def finalize_options(self): if not self.input_file: raise DistutilsOptionError('you must specify the input file') if not self.locale: raise DistutilsOptionError('you must provide a locale for the ' 'new catalog') try: self._locale = Locale.parse(self.locale) except UnknownLocaleError as e: raise DistutilsOptionError(e) if not self.output_file and not self.output_dir: raise DistutilsOptionError('you must specify the output directory') if not self.output_file: self.output_file = os.path.join(self.output_dir, self.locale, 'LC_MESSAGES', self.domain + '.po') if not os.path.exists(os.path.dirname(self.output_file)): os.makedirs(os.path.dirname(self.output_file)) if self.no_wrap and self.width: raise DistutilsOptionError("'--no-wrap' and '--width' are mutually " "exclusive") if not self.no_wrap and not self.width: self.width = 76 elif self.width is not None: self.width = int(self.width)
def getLocale(self, request): ## FIXME: implement request.locale() tags = ACCEPT_LANGUAGE.parse(request.environ()) if tags: return Locale.parse(tags[0], sep="-") else: return defaultLocale() # server default
def render_template(self, filename, **kwargs): kwargs.update({ 'google_analytics_code' : config.google_analytics_code, 'app_name': config.app_name, 'user_id': self.user_id, 'username': self.username, 'email': self.email, 'url': self.request.url, 'path': self.request.path, 'query_string': self.request.query_string, 'path_for_language': self.path_for_language, 'is_mobile': self.is_mobile, 'locale': Locale.parse(self.locale), # babel locale object 'locales': self.locales, 'provider_uris': self.provider_uris, 'provider_info': self.provider_info, 'enable_federated_login': config.enable_federated_login }) kwargs.update(self.auth_config) if hasattr(self, 'forms'): kwargs.update(self.forms) elif hasattr(self, 'form'): kwargs['form'] = self.form if self.messages: kwargs['messages'] = self.messages self.response.headers.add_header('X-UA-Compatible', 'IE=Edge,chrome=1') self.response.write(self.jinja2.render_template(filename, **kwargs))
def fixed_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. """ 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)) return result dirs = additional_folders + [os.path.join(self.app.root_path, 'translations')] # translations from plugins plugins = octoprint.plugin.plugin_manager().enabled_plugins for name, plugin in plugins.items(): plugin_translation_dir = os.path.join(plugin.location, 'translations') if not os.path.isdir(plugin_translation_dir): continue dirs.append(plugin_translation_dir) result = [Locale.parse(default_locale)] for dir in dirs: result += list_translations(dir) return result
def user_language(user, fallbacks=[]): # find out the locale locale = None if user and user.locale: locale = user.locale if locale is None: locales = map(str, LOCALES) locale = Locale.parse(Locale.negotiate(fallbacks, locales)) \ or get_default_locale() # determinate from which path we load the translations translations_module = config.get('adhocracy.translations', 'adhocracy') 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 ' 'translation files in a subdirectory ' '"i18n"') % translations_module) translations_root = translations_module_loader.filename translations_config = {'pylons.paths': {'root': translations_root}, 'pylons.package': config.get('pylons.package')} # set language and fallback set_lang(locale.language, pylons_config=translations_config) add_fallback(get_default_locale().language, pylons_config=translations_config) formencode.api.set_stdtranslation(domain="FormEncode", languages=[locale.language]) return locale
def speaking_language(languages=None): if not languages: languages = db.session.query(db.Languages).join(db.books_languages_link).join(db.Books)\ .filter(common_filters())\ .group_by(text('books_languages_link.lang_code')).all() for lang in languages: try: cur_l = LC.parse(lang.lang_code) lang.name = cur_l.get_language_name(get_locale()) except UnknownLocaleError: lang.name = _(isoLanguages.get(part3=lang.lang_code).name) return languages
def _format_date_parts(date: Date, locale: str) -> str: if date is None: raise IncompleteDateError('This date is None.') try: date_parts_format = _FORMAT_DATE_PARTS_FORMATTERS[tuple( map(lambda x: x is not None, date.parts))]() except KeyError: raise IncompleteDateError( 'This date does not have enough parts to be rendered.') parts = map(lambda x: 1 if x is None else x, date.parts) return dates.format_date(datetime.date(*parts), date_parts_format, Locale.parse(locale, '-'))
def get_display_name(self, locale): language_code, _, country_code = locale.partition('_') term_code = languages.get(bibliographic=language_code).terminology available_languages = dict( literal_eval((self.settings.get('available_languages', '[]')))) try: return Locale.parse(term_code).language_name except UnknownLocaleError: # Fallback value is the generated value in English or the code return available_languages.get(locale, locale)
def __init__(self, country='AU', use_sessions=True, **kwargs): self.URL = "https://www.justwatch.com" self.IMG_URL = "https://images.justwatch.com" self.kwargs = kwargs self.country = country self.language = Locale.parse('und_{}'.format(self.country)).language self.locale = self.language + '_' + self.country self.kwargs_cinema = [] if use_sessions: self.requests = requests.Session() else: self.requests = requests
async def __process__(self, scope: Scope, receive: Receive, send: Send): """Load/save the sessions.""" if isinstance(scope, Request): request = scope else: request = scope.get('request') or Request(scope) lang = await self.locale_selector(request) or self.default_locale # type: ignore locale = Locale.parse(lang, sep='-') current_locale.set(locale) return await self.app(scope, receive, send) # type: ignore
def get_locale_list(self): django_locales = set( [i[0].replace('_', '-').lower() for i in settings.LANGUAGES]) babel_locales = set( [j.replace('_', '-').lower() for j in locale_identifiers()]) sphinx_locales = set( [k.replace('_', '-').lower() for k in chm_locales.keys()]) locales = [ str(Locale.parse(identifier=l, sep='-')) for l in django_locales & babel_locales & sphinx_locales ] return filter(None, locales)
def test_getlocales(): config = {'server': {'language': ''}} with pytest.raises(l10n.LocaleError): l10n.get_locales({}) l10n.get_locales(config) config['server']['language'] = 'zz' l10n.get_locales(config) config['server']['language'] = 'en-US' assert l10n.get_locales(config) == [Locale.parse('en_US')] config['server']['language'] = 'de_CH' assert l10n.get_locales(config) == [Locale.parse('de_CH')] config['server']['language'] = ['de', 'en-US'] # noqa assert l10n.get_locales(config) == [ Locale.parse('de'), Locale.parse('en_US') ] # noqa config = {'server': {'languages': []}} with pytest.raises(l10n.LocaleError): l10n.get_locales(config) config['server']['languages'] = [None] with pytest.raises(l10n.LocaleError): l10n.get_locales(config) config['server']['languages'] = ['de', 'en-US'] assert l10n.get_locales(config) == [ Locale.parse('de'), Locale.parse('en_US') ] # noqa
def render_template(self, filename, **kwargs): locales = config.locales or [] locale_iso = None language = '' territory = '' language_id = config.app_lang if self.locale and len(locales) > 1: locale_iso = Locale.parse(self.locale) language_id = locale_iso.language territory_id = locale_iso.territory language = locale_iso.languages[language_id] territory = locale_iso.territories[territory_id] # make all self.view variables available in jinja2 templates if hasattr(self, 'view'): kwargs.update(self.view.__dict__) # set or overwrite special vars for jinja templates kwargs.update({ 'google_analytics_code' : config.google_analytics_code, 'app_name': config.app_name, 'user_id': self.user_id, 'name': self.name, 'username': self.username, 'email': self.email, 'created': self.created, 'url': self.request.url, 'path': self.request.path, 'query_string': self.request.query_string, 'path_for_language': self.path_for_language, 'is_mobile': self.is_mobile, 'locale_iso': locale_iso, # babel locale object 'locale_language': language.capitalize() + " (" + territory.capitalize() + ")", # babel locale object 'locale_language_id': language_id, # babel locale object 'locales': self.locales, 'provider_uris': self.provider_uris, 'provider_info': self.provider_info, 'enable_federated_login': config.enable_federated_login, 'base_layout': self.get_base_layout, 'admin_interface_url': config.admin_interface_url, 'admin': self.is_admin, }) kwargs.update(self.auth_config) if hasattr(self, 'form'): kwargs['form'] = self.form if self.messages: kwargs['messages'] = self.messages self.response.headers.add_header('X-UA-Compatible', 'IE=Edge,chrome=1') self.response.write(self.jinja2.render_template(filename, **kwargs))
def __init__(self, width: int, height: int, storage: Storage = LocalStorage(), style: str = 'newsworthy', language: str = 'en-GB'): """ :param width: width in pixels :param height: height in pixels :param storage: storage object that will handle file saving. Default LocalStorage() class will save a file the working dir. :param style: a predefined style or the path to a custom style file :param language: a BCP 47 language tag (eg `en`, `sv-FI`) """ try: self.api_token = os.environ["DATAWRAPPER_API_KEY"] except KeyError: raise Exception("DATAWRAPPER_API_KEY must be set in environment") # P U B L I C P R O P E R T I E S # The user can alter these at any time # Unlike regular Chart objects Datawrapper does not use the DataList # class for storing data, as DataList does not handle non-numerical # data. DatawrapperCharts will understand the same list-of-list-of-list # structure as DataList builds upon, but prefer consuming list of # dictionaries self.data = [] self.labels = [] # Optionally one label for each dataset self.annotations = [] # Manually added annotations self.caption = None self.highlight = None self.decimals = None self.dw_data = {} # The DW data structure that defines the chart self._dw_id = None # Datawrapper chart id # P R I V A T E P R O P E R T I E S # Properties managed through getters/setters self._title = None self._units = "count" # Calculated properties self._storage = storage self._w, self._h = int(width), int(height) self._style = loadstyle(style) # Standardize and check if language tag is a valid BCP 47 tag self._language = standardize_tag(language) self._locale = Locale.parse(self._language.replace("-", "_")) # For renaming regions to DW conventions self._translations = None
def setup_i18n(self): locales = [] if 'HTTP_ACCEPT_LANGUAGE' in self.environ: # locales looks something like: ['en', 'en-us;q=0.7', 'ja;q=0.3'] client_locales = self.environ['HTTP_ACCEPT_LANGUAGE'].split(',') for locale in client_locales: try: locales.append( Locale.parse(locale.split(';')[0].strip(), sep='-').language) except Exception, e: log.debug("Error parsing locale '%s'. %s: %s", locale, type(e), e)
def _get_locale(self): locale = self.req.locale if not locale: return locale if not locale.territory: # search first locale which has the same `langauge` and territory # in preferred languages for l in self.req.languages: l = l.replace('-', '_').lower() if l.startswith(locale.language.lower() + '_'): try: l = Locale.parse(l) if l.territory: locale = l break except UnknownLocaleError: pass if not locale.territory and locale.language in LOCALE_ALIASES: locale = Locale.parse(LOCALE_ALIASES[locale.language]) return locale or Locale('en', 'US')
def locale_tuple(self): """ Testing locale identifier parsing attributes """ l = Locale.parse('en-AU') print("language:", l.language) print("territory:", l.territory) print("script:", l.script) print("variant:", l.variant) print( get_locale_identifier( (l.language, l.territory, l.script, l.variant), sep='_'))
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 """ dirname = os.path.join( self.app.root_path, self.app.config['BABEL_DEFAULT_LOCALE_DIRECTORY']) 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
def test_i18n_parse_date_iso8609(self): tz = datefmt.timezone('GMT +2:00') dt = datetime.datetime(2010, 8, 28, 13, 45, 56, 0, tz) d = datetime.datetime(2010, 8, 28, 0, 0, 0, 0, tz) en_US = Locale.parse('en_US') vi = Locale.parse('vi') def iso8601(expected, text, tz, locale): self.assertEqual(expected, datefmt.parse_date(text, tz, locale)) iso8601(dt, '2010-08-28T15:45:56+0400', tz, en_US) iso8601(dt, '2010-08-28T11:45:56+0000', tz, vi) iso8601(dt, '2010-08-28T11:45:56Z', tz, vi) iso8601(dt, '20100828T144556+0300', tz, en_US) iso8601(dt, '20100828T114556Z', tz, vi) iso8601(d, '2010-08-28+0200', tz, en_US) # iso8601(d, '2010-08-28+0000', tz, vi) # iso8601(d, '2010-08-28Z', tz, en_US) iso8601(d, '2010-08-28', tz, vi) iso8601(d, '20100828+0200', tz, en_US)
def do_add_translations(target=("t", ""), lang=("l", ""), force=("f", False), i18n_dir=("i", ""), all=("a", False)): """ Add new translations for specified language. """ try: locale = Locale.parse(lang) except (UnknownLocaleError, ValueError), e: print_status("You must specify lang.") sys.exit(1)
def set_locale_button(bot, ctx): language = ctx.text.split(":")[1] try: octobot.localization.set_chat_locale(ctx.chat.id, language) except ValueError as e: logger.error("Failed to set locale: %s", e) ctx.edit(f"Invalid language {language}") ctx.reply("❌") else: ctx.edit( ctx.localize('Language "{locale}" set').format( locale=Locale.parse(language).display_name)) ctx.reply("👌")
def get_locale(): """Returns the locale that should be used for this request as `babel.Locale` object. This returns `None` if used outside of a request. """ ctx = _request_ctx_stack.top locale = ctx and getattr(ctx, 'babel_locale', None) or None if locale is None: if ctx is None: locale = Locale.parse(constants.DEFAULT_LOCALE) else: babel = ctx.app.extensions['babel'] if babel.locale_selector_func is None: locale = babel.default_locale else: rv = babel.locale_selector_func() if rv is None: locale = babel.default_locale else: locale = Locale.parse(rv) ctx.babel_locale = locale return locale
def run(self, argv=sys.argv): """Main entry point of the command-line interface. :param argv: list of arguments passed on the command-line """ self.parser = 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:]) # Configure logging self.log = logging.getLogger('babel') self.log.setLevel(options.loglevel) handler = logging.StreamHandler() handler.setLevel(options.loglevel) formatter = logging.Formatter('%(message)s') handler.setFormatter(formatter) self.log.addHandler(handler) if options.list_locales: identifiers = localedata.list() longest = max([len(identifier) for identifier in identifiers]) format = u'%%-%ds %%s' % (longest + 1) for identifier in localedata.list(): 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('incorrect number of arguments') cmdname = args[0] if cmdname not in self.commands: self.parser.error('unknown command "%s"' % cmdname) return getattr(self, cmdname)(args[1:])
def get_first_week_day_jquery_ui(req): """Get first week day for jQuery date picker""" locale = req.lc_time if locale == 'iso8601': return 1 # Monday if babel and locale: if not locale.territory: # search first locale which has the same `langauge` and territory # in preferred languages for l in req.languages: l = l.replace('-', '_').lower() if l.startswith(locale.language.lower() + '_'): try: l = Locale.parse(l) if l.territory: locale = l break except (UnknownLocaleError, ValueError): pass if not locale.territory and locale.language in LOCALE_ALIASES: locale = Locale.parse(LOCALE_ALIASES[locale.language]) return (locale.first_week_day + 1) % 7 return 0 # Sunday
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()
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
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 """ # XXX: Wouldn't it be better to list the locales from the domain? state = get_state() dirname = os.path.join(state.app.root_path, 'translations') 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
def validate_wagtail_has_no_language(self, main): main = Locale.parse(main).language wagtail_main_language = SiteLanguage.objects.filter( is_main_language=True).first() if (wagtail_main_language and not wagtail_main_language.locale == main): self.errors.append({ 'type': 'wrong_main_language_exist_in_wagtail', 'details': { 'repo': self.repo.name, 'lang': wagtail_main_language.get_locale_display(), 'selected_lang': get_locale_english_name(main) }})
def load_translations(locale): """Return the translations for the locale.""" locale = Locale.parse(locale) key = str(locale) rv = _translations.get(key) if rv is None: catalog = find_catalog(locale) if catalog is None: rv = NullTranslations() else: with open(catalog, 'rb') as f: rv = Translations(fileobj=f, domain=LOCALE_DOMAIN) _translations[key] = rv return rv
def test_translate(language_struct, nonlanguage_struct): assert l10n.translate({}, 'en-US') == {} assert l10n.translate(42, 'fr') == 42 assert l10n.translate(None, 'de') is None assert l10n.translate(['list item'], Locale('en')) == ['list item'] assert l10n.translate({'nested dict': { 'en': 1, 'fr': 2 }}, 'en') == { 'nested dict': { 'en': 1, 'fr': 2 } } # noqa assert l10n.translate(nonlanguage_struct, 'fr') == nonlanguage_struct assert l10n.translate(nonlanguage_struct, 'fla') == 'non-language key' assert l10n.translate(language_struct, 'en') == 'English' assert l10n.translate(language_struct, 'en-US') == 'English (United States)' # noqa assert l10n.translate(language_struct, 'sq_AL') == Locale.parse('alb').display_name # noqa assert l10n.translate(language_struct, 'fr_CH') == Locale.parse('fr').display_name # noqa assert l10n.translate(language_struct, 'nl') == Locale.parse('nl_BE').display_name # noqa assert l10n.translate(language_struct, 'de') == 'English' assert l10n.translate(language_struct, Locale('en')) == 'English' assert l10n.translate( language_struct, Locale.parse('en_US')) == 'English (United States)' # noqa with pytest.raises(l10n.LocaleError): l10n.translate(language_struct, None) # noqa l10n.translate(language_struct, 42) # noqa
def get_locale(request=None): """Returns the locale that should be used for this request as `babel.Locale` object. This returns `Locale.parse('en')` if used outside of a request. """ if request is None: return Locale.parse("en") request_ = get_request_container(request) locale = request_.get("babel_locale", None) if locale is None: babel = request.app.ctx.babel_instance if babel.locale_selector_func is None: locale = babel.default_locale else: rv = babel.locale_selector_func(request) if rv is None: locale = babel.default_locale else: locale = Locale.parse(rv) request_["babel_locale"] = locale return locale
def money_format(price): try: decimal_point = np.modf(price) vf = int(np.around(decimal_point[0] * 100)) cents = "" if vf > 0: cents = "e {} centavos".format(vf) dates.LC_TIME = Locale.parse(current_app.config.get("LOCALE_PARSER")) money_brl_ext = format_currency(price, current_app.config.get("COIN"), locale=dates.LC_TIME) money_ext = re.sub(r",..", cents, money_brl_ext.replace(".", "")) return money_ext, money_brl_ext except ValueError as e: print(e)
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('zh_CN') assert l.language == 'zh' assert l.territory == 'CN' assert l.script == 'Hans' l = Locale.parse('zh_SG') assert l.language == 'zh' assert l.territory == 'SG' assert l.script == 'Hans' l = Locale.parse('und_AT') assert l.language == 'de' assert l.territory == 'AT' l = Locale.parse('und_UK') assert l.language == 'en' assert l.territory == 'GB' assert l.script is None
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(_KNOWN_LOCALES, requested)) if locale is None: locale = get_default_locale() options = [ str(locale), locale.language, str(get_default_locale()), get_default_locale().language ] for language in options: try: set_lang(language) tmpl_context.language = language except: pass
def setup_i18n(self): locales = [] if 'HTTP_ACCEPT_LANGUAGE' in self.environ: # locales looks something like: ['en', 'en-us;q=0.7', 'ja;q=0.3'] client_locales = self.environ['HTTP_ACCEPT_LANGUAGE'].split(',') for locale in client_locales: try: locales.append(Locale.parse(locale.split(';')[0].strip(), sep='-').language) except Exception as e: log.debug("Error parsing locale '%s'. %s: %s", locale, type(e), e) if not locales: # Default to English locales = 'en' t = Translations.load(dirname='locale', locales=locales, domain='ginga') self.template_context.update(dict(_=t.ugettext, n_=t.ugettext, N_=t.ungettext))