diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 9a298328..c9ececdf 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -478,6 +478,57 @@ def robotsTxt(): return send_from_directory(app.static_folder, "robots.txt") +@app.route("/i18n//.js") +@util.flask.cached(refreshif=lambda: util.flask.cache_check_headers() or "_refresh" in request.values, key=lambda: "view/%s/%s" % (request.path, g.locale)) +def localeJs(locale, domain): + from flask import _request_ctx_stack + from babel.messages.pofile import read_po + + def messages_from_po(base_path, locale, domain): + path = os.path.join(base_path, locale) + if not os.path.isdir(path): + return dict(), None + + path = os.path.join(path, "LC_MESSAGES", "{domain}.po".format(**locals())) + if not os.path.isfile(path): + return dict(), None + + messages = dict() + with file(path) as f: + catalog = read_po(f, locale=locale, domain=domain) + + for message in catalog: + message_id = message.id + if isinstance(message_id, (list, tuple)): + message_id = message_id[0] + messages[message_id] = message.string + + return messages, catalog.plural_expr + + messages = dict() + + ctx = _request_ctx_stack.top + base_path = os.path.join(ctx.app.root_path, "translations") + + plugins = octoprint.plugin.plugin_manager().enabled_plugins + for name, plugin in plugins.items(): + base_path = os.path.join(plugin.location, 'translations') + plugin_messages, _ = messages_from_po(base_path, locale, domain) + messages = octoprint.util.dict_merge(messages, plugin_messages) + + core_messages, plural_expr = messages_from_po(base_path, locale, domain) + messages = octoprint.util.dict_merge(messages, core_messages) + + catalog = dict( + messages=messages, + plural_expr=plural_expr, + locale=locale, + domain=domain + ) + + return render_template("i18n.js.jinja2", catalog=catalog) + + @app.route("/plugin_assets//") def plugin_assets(name, filename): asset_plugins = pluginManager.get_filtered_implementations(lambda p: p._identifier == name, octoprint.plugin.AssetPlugin) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 5dc90cf4..57501908 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -48,6 +48,7 @@ def enable_plugin_translations(): translations = getattr(ctx, 'babel_translations', None) if translations is None: locale = flask.ext.babel.get_locale() + translations = support.Translations() plugins = octoprint.plugin.plugin_manager().enabled_plugins for name, plugin in plugins.items(): @@ -60,17 +61,11 @@ def enable_plugin_translations(): except: logger.exception("Error while trying to load translations for plugin {name}".format(**locals())) else: - if translations is None: - translations = plugin_translations - else: - translations = translations.merge(plugin_translations) + translations = translations.merge(plugin_translations) dirname = os.path.join(ctx.app.root_path, 'translations') core_translations = support.Translations.load(dirname, [locale]) - if translations is None: - translations = core_translations - else: - translations = translations.merge(core_translations) + translations = translations.merge(core_translations) ctx.babel_translations = translations return translations diff --git a/src/octoprint/templates/i18n.js.jinja2 b/src/octoprint/templates/i18n.js.jinja2 new file mode 100644 index 00000000..b416d2a2 --- /dev/null +++ b/src/octoprint/templates/i18n.js.jinja2 @@ -0,0 +1 @@ +window.BABEL_TO_LOAD_{{ catalog.locale }} = {{ catalog | tojson | safe }}; \ No newline at end of file diff --git a/src/octoprint/templates/javascripts.jinja2 b/src/octoprint/templates/javascripts.jinja2 index f7ab855b..d295117d 100644 --- a/src/octoprint/templates/javascripts.jinja2 +++ b/src/octoprint/templates/javascripts.jinja2 @@ -40,12 +40,8 @@ - -{% if g.locale != 'en' %} - -{% endif %} - - + +