From 29a2c6abeca6d605fc407d20546a3eeec009d044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 25 Jan 2017 16:02:25 +0100 Subject: [PATCH] Allow new locales to be provided by plugins Core translations still win over plugin translations, but at least this way it's possible to provide full blown translations via plugins. --- src/octoprint/server/util/flask.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 56590bf8..1983e9d9 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -62,12 +62,18 @@ def enable_additional_translations(default_locale="en", additional_folders=None) continue if filter(lambda x: x.name.endswith('.mo'), scandir(locale_dir)): result.append(Locale.parse(entry.name)) - if not result: - result.append(Locale.parse(self._default_locale)) return result dirs = additional_folders + [os.path.join(self.app.root_path, 'translations')] + # translations from plugins + plugins = octoprint.plugin.plugin_manager().enabled_plugins + for name, plugin in plugins.items(): + plugin_translation_dir = os.path.join(plugin.location, 'translations') + if not os.path.isdir(plugin_translation_dir): + continue + dirs.append(plugin_translation_dir) + result = [Locale.parse(default_locale)] for dir in dirs: @@ -104,20 +110,20 @@ def enable_additional_translations(default_locale="en", additional_folders=None) else: if isinstance(plugin_translations, support.Translations): translations = translations.merge(plugin_translations) - logger.debug("Using translation folder {dirname} for locale {locale} of plugin {name}".format(**locals())) + logger.debug("Using translation plugin folder {dirname} from plugin {name} for locale {locale}".format(**locals())) break else: - logger.debug("No translations for locale {locale} for plugin {name}".format(**locals())) + logger.debug("No translations for locale {locale} from plugin {name}".format(**locals())) # core translations dirs = additional_folders + [os.path.join(ctx.app.root_path, 'translations')] for dirname in dirs: core_translations = support.Translations.load(dirname, [locale]) if isinstance(core_translations, support.Translations): - logger.debug("Using translation folder {dirname} for locale {locale} of core translations".format(**locals())) + logger.debug("Using translation core folder {dirname} for locale {locale}".format(**locals())) break else: - logger.debug("No core translations for locale {locale}") + logger.debug("No translations for locale {} in core folders".format(locale)) translations = translations.merge(core_translations) ctx.babel_translations = translations