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.
This commit is contained in:
Goffi 2019-04-10 21:04:51 +02:00
commit 4ab939c46a

View file

@ -414,10 +414,10 @@ class Renderer(object):
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 @@ class Renderer(object):
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 @@ class Renderer(object):
css_files=css_files,
css_files_noscript=css_files_noscript,
locale=self._locale,
locales=self.locales,
gidx=Indexer(),
script=scripts_handler,
**kwargs