def test_without_match_should_return_default(self): preferred_locale = 'de' localizeds = [ self.DummyLocalized('nl'), self.DummyLocalized('en'), self.DummyLocalized('uk') ] self.assertEquals(self.DummyLocalized('nl'), negotiate_localizeds(preferred_locale, localizeds))
def _get_sort_key(x): return get_sort_attr( negotiate_localizeds(locale, get_localized_attr(x)))
def _filter_negotiate_localizeds( context: Context, localizeds: Iterable[Localized]) -> Optional[Localized]: locale = resolve_or_missing(context, 'locale') return negotiate_localizeds(locale, list(localizeds))
def test_with_match_should_return_match(self, expected: Localized, preferred_locale: str, localizeds: List[Localized]): self.assertEquals(expected, negotiate_localizeds(preferred_locale, localizeds))
def test_without_localizeds_should_raise_error(self): with self.assertRaises(ValueError): negotiate_localizeds('nl', [])
def create_environment(site: Site, default_locale: Optional[str] = None) -> Environment: if default_locale is None: default_locale = site.configuration.default_locale url_generator = SiteUrlGenerator(site.configuration) template_directory_paths = list( [join(path, 'templates') for path in site.resources.paths]) environment = Environment( loader=FileSystemLoader(template_directory_paths), undefined=StrictUndefined, autoescape=select_autoescape(['html']), trim_blocks=True, lstrip_blocks=True, extensions=[ 'jinja2.ext.do', 'jinja2.ext.i18n', ], ) environment.install_gettext_translations(site.translations[default_locale]) environment.globals['site'] = site environment.globals['locale'] = default_locale environment.globals['plugins'] = _Plugins(site.plugins) environment.globals['EventType'] = Event.Type environment.globals['PresenceRole'] = Presence.Role environment.globals['urlparse'] = urlparse environment.filters['map'] = _filter_map environment.filters['flatten'] = _filter_flatten environment.filters['walk'] = _filter_walk environment.filters['takewhile'] = _filter_takewhile environment.filters['locale_get_data'] = lambda locale: Locale.parse( locale, '-') environment.filters[ 'negotiate_localizeds'] = lambda localizeds: negotiate_localizeds( default_locale, localizeds) environment.filters['sort_localizeds'] = contextfilter( lambda context, *args, **kwargs: _filter_sort_localizeds( context, default_locale, *args, **kwargs)) # A filter to convert any value to JSON. @contextfilter def _filter_json(context, data, indent=None): return stdjson.dumps(data, indent=indent, cls=JSONEncoder.get_factory( site.configuration, resolve_or_missing(context, 'locale'))) environment.filters['json'] = _filter_json # Override Jinja2's built-in JSON filter, which escapes the JSON for use in HTML, to use Betty's own encoder. @contextfilter def _filter_tojson(context, data, indent=None): return htmlsafe_json_dumps(data, indent=indent, dumper=lambda *args, **kwargs: _filter_json( context, *args, **kwargs)) environment.filters['tojson'] = _filter_tojson environment.tests['resource'] = lambda x: isinstance(x, Resource) environment.tests['identifiable'] = lambda x: isinstance(x, Identifiable) environment.filters['paragraphs'] = _filter_paragraphs def _filter_format_date(date: Datey): with Translations(site.translations[default_locale]): return format_datey(date, default_locale) environment.filters['format_date'] = _filter_format_date environment.filters['format_degrees'] = _filter_format_degrees environment.globals['citer'] = _Citer() def _filter_url(resource, content_type=None, locale=None, **kwargs): content_type = content_type if content_type else 'text/html' locale = locale if locale else default_locale return url_generator.generate(resource, content_type, locale=locale, **kwargs) environment.filters['url'] = _filter_url environment.filters['static_url'] = StaticPathUrlGenerator( site.configuration).generate environment.filters['file'] = lambda *args: _filter_file(site, *args) environment.filters['image'] = lambda *args, **kwargs: _filter_image( site, *args, **kwargs) environment.globals['search_index'] = lambda: index(site, environment) for plugin in site.plugins.values(): if isinstance(plugin, Jinja2Provider): environment.globals.update(plugin.globals) environment.filters.update(plugin.filters) return environment
def get_sort_key(x): return get_sort_attr( negotiate_localizeds(preferred_locale, get_localized_attr(x)))