Mercurial > libervia-backend
comparison sat/tools/common/template.py @ 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 | f8427bf8c072 |
children | 7dbdbd132649 |
comparison
equal
deleted
inserted
replaced
2902:9241f9d1f9e5 | 2903:68a7543ebbb3 |
---|---|
412 path or template_data.path) | 412 path or template_data.path) |
413 | 413 |
414 def installTranslations(self): | 414 def installTranslations(self): |
415 # TODO: support multi translation | 415 # TODO: support multi translation |
416 # for now, only translations in sat_templates are handled | 416 # for now, only translations in sat_templates are handled |
417 self.translations = {} | |
417 for site_key, site_path in self.sites_paths.iteritems(): | 418 for site_key, site_path in self.sites_paths.iteritems(): |
418 site_prefix = u"[{}] ".format(site_key) if site_key else u'' | 419 site_prefix = u"[{}] ".format(site_key) if site_key else u'' |
419 i18n_dir = os.path.join(site_path, "i18n") | 420 i18n_dir = os.path.join(site_path, "i18n") |
420 self.translations = {} | |
421 for lang_dir in os.listdir(i18n_dir): | 421 for lang_dir in os.listdir(i18n_dir): |
422 lang_path = os.path.join(i18n_dir, lang_dir) | 422 lang_path = os.path.join(i18n_dir, lang_dir) |
423 if not os.path.isdir(lang_path): | 423 if not os.path.isdir(lang_path): |
424 continue | 424 continue |
425 po_path = os.path.join(lang_path, "LC_MESSAGES/sat.mo") | 425 po_path = os.path.join(lang_path, "LC_MESSAGES/sat.mo") |
441 site=site_prefix, msg=e)) | 441 site=site_prefix, msg=e)) |
442 else: | 442 else: |
443 log.info(_(u"{site}loaded {lang} templates translations").format( | 443 log.info(_(u"{site}loaded {lang} templates translations").format( |
444 site = site_prefix, | 444 site = site_prefix, |
445 lang=lang_dir)) | 445 lang=lang_dir)) |
446 self.env.install_null_translations(True) | 446 |
447 default_locale = Locale.parse(self._locale_str) | |
448 if default_locale not in self.translations: | |
449 # default locale disable gettext, | |
450 # so we can use None instead of a Translations instance | |
451 self.translations[default_locale] = None | |
452 | |
453 self.env.install_null_translations(True) | |
454 # we generate a tuple of locales ordered by display name that templates can access | |
455 # through the "locales" variable | |
456 self.locales = tuple(sorted(self.translations.keys(), | |
457 key=lambda l: l.language_name.lower())) | |
458 | |
447 | 459 |
448 def setLocale(self, locale_str): | 460 def setLocale(self, locale_str): |
449 """set current locale | 461 """set current locale |
450 | 462 |
451 change current translation locale and self self._locale and self._locale_str | 463 change current translation locale and self self._locale and self._locale_str |
886 template_data=template_data, | 898 template_data=template_data, |
887 media_path=media_path, | 899 media_path=media_path, |
888 css_files=css_files, | 900 css_files=css_files, |
889 css_files_noscript=css_files_noscript, | 901 css_files_noscript=css_files_noscript, |
890 locale=self._locale, | 902 locale=self._locale, |
903 locales=self.locales, | |
891 gidx=Indexer(), | 904 gidx=Indexer(), |
892 script=scripts_handler, | 905 script=scripts_handler, |
893 **kwargs | 906 **kwargs |
894 ) | 907 ) |
895 self.env._template_data = None | 908 self.env._template_data = None |