changeset 2903:68a7543ebbb3

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.
author Goffi <goffi@goffi.org>
date Wed, 10 Apr 2019 21:04:51 +0200
parents 9241f9d1f9e5
children 5bba8953061e
files sat/tools/common/template.py
diffstat 1 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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