# HG changeset patch # User Goffi # Date 1554923091 -7200 # Node ID 68a7543ebbb3163bfd8333e91ebade04aabd8574 # Parent 9241f9d1f9e51bc7a7bebc2cb4535e2095521303 template: added "locales" variables to templates: "locales" contains an ordered tuple of locales of all available translations. This can be used by templates to diplay language/locale menu. diff -r 9241f9d1f9e5 -r 68a7543ebbb3 sat/tools/common/template.py --- a/sat/tools/common/template.py Wed Apr 10 21:04:51 2019 +0200 +++ b/sat/tools/common/template.py Wed Apr 10 21:04:51 2019 +0200 @@ -414,10 +414,10 @@ def installTranslations(self): # TODO: support multi translation # for now, only translations in sat_templates are handled + self.translations = {} for site_key, site_path in self.sites_paths.iteritems(): site_prefix = u"[{}] ".format(site_key) if site_key else u'' i18n_dir = os.path.join(site_path, "i18n") - self.translations = {} for lang_dir in os.listdir(i18n_dir): lang_path = os.path.join(i18n_dir, lang_dir) if not os.path.isdir(lang_path): @@ -443,7 +443,19 @@ log.info(_(u"{site}loaded {lang} templates translations").format( site = site_prefix, lang=lang_dir)) - self.env.install_null_translations(True) + + default_locale = Locale.parse(self._locale_str) + if default_locale not in self.translations: + # default locale disable gettext, + # so we can use None instead of a Translations instance + self.translations[default_locale] = None + + self.env.install_null_translations(True) + # we generate a tuple of locales ordered by display name that templates can access + # through the "locales" variable + self.locales = tuple(sorted(self.translations.keys(), + key=lambda l: l.language_name.lower())) + def setLocale(self, locale_str): """set current locale @@ -888,6 +900,7 @@ css_files=css_files, css_files_noscript=css_files_noscript, locale=self._locale, + locales=self.locales, gidx=Indexer(), script=scripts_handler, **kwargs