Also support client side translations for plugins
This commit is contained in:
parent
b908ff5821
commit
9f54985ce5
4 changed files with 57 additions and 14 deletions
|
|
@ -478,6 +478,57 @@ def robotsTxt():
|
|||
return send_from_directory(app.static_folder, "robots.txt")
|
||||
|
||||
|
||||
@app.route("/i18n/<string:locale>/<string:domain>.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/<string:name>/<path:filename>")
|
||||
def plugin_assets(name, filename):
|
||||
asset_plugins = pluginManager.get_filtered_implementations(lambda p: p._identifier == name, octoprint.plugin.AssetPlugin)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
1
src/octoprint/templates/i18n.js.jinja2
Normal file
1
src/octoprint/templates/i18n.js.jinja2
Normal file
|
|
@ -0,0 +1 @@
|
|||
window.BABEL_TO_LOAD_{{ catalog.locale }} = {{ catalog | tojson | safe }};
|
||||
|
|
@ -40,12 +40,8 @@
|
|||
<script type="text/javascript" src="{{ url_for('static', filename='js/app/main.js') }}"></script>
|
||||
<!-- /Include OctoPrint files -->
|
||||
|
||||
<!-- Include i18n language files -->
|
||||
{% if g.locale != 'en' %}
|
||||
<script type="text/javascript" src="{{ url_for('static', filename='js/i18n/%s.js' % g.locale) }}"></script>
|
||||
{% endif %}
|
||||
<!-- /Include i18n language files -->
|
||||
|
||||
<script type="text/javascript" src="{{ url_for('static', filename='gcodeviewer/js/ui.js') }}"></script>
|
||||
<script type="text/javascript" src="{{ url_for('static', filename='gcodeviewer/js/gCodeReader.js') }}"></script>
|
||||
<script type="text/javascript" src="{{ url_for('static', filename='gcodeviewer/js/renderer.js') }}"></script>
|
||||
|
||||
<script type="text/javascript" src="{{ url_for('localeJs', locale=g.locale, domain='messages') }}"></script>
|
||||
|
|
|
|||
Loading…
Reference in a new issue