FIX: Fallback locale was not available for extra translations

Translations from fallback locales were not sent to the client
for admin_js and wizard_js.
This commit is contained in:
Gerhard Schlager
2019-05-23 21:23:31 +02:00
parent 0e24cb0f78
commit c1e9a70d59
8 changed files with 135 additions and 66 deletions

View File

@@ -28,24 +28,6 @@ I18n.isValidNode = function(obj, node, undefined) {
return obj[node] !== null && obj[node] !== undefined;
};
function checkExtras(origScope, sep, extras) {
if (!extras || extras.length === 0) { return; }
for (var i = 0; i < extras.length; i++) {
var messages = extras[i];
scope = origScope.split(sep);
if (scope[0] === 'js') { scope.shift(); }
while (messages && scope.length > 0) {
currentScope = scope.shift();
messages = messages[currentScope];
}
if (messages !== undefined) { return messages; }
}
}
I18n.lookup = function(scope, options) {
options = options || {};
@@ -64,17 +46,26 @@ I18n.lookup = function(scope, options) {
scope = options.scope.toString() + this.SEPARATOR + scope;
}
var origScope = "" + scope;
var originalScope = scope;
scope = scope.split(this.SEPARATOR);
scope = origScope.split(this.SEPARATOR);
if (scope.length > 0 && scope[0] !== "js") {
scope.unshift("js");
}
while (messages && scope.length > 0) {
currentScope = scope.shift();
messages = messages[currentScope];
}
if (messages === undefined) {
messages = checkExtras(origScope, this.SEPARATOR, this.extras);
if (messages === undefined && this.extras && this.extras[locale]) {
messages = this.extras[locale];
scope = originalScope.split(this.SEPARATOR);
while (messages && scope.length > 0) {
currentScope = scope.shift();
messages = messages[currentScope];
}
}
if (messages === undefined) {

View File

@@ -40,21 +40,20 @@ class ExtraLocalesController < ApplicationController
translations = JsLocaleHelper.translations_for(locale_str)
for_key = {}
translations.values.each { |v| for_key.deep_merge!(v[bundle_str]) if v.has_key?(bundle_str) }
translations.keys.each do |l|
translations[l].keys.each do |k|
bundle_translations = translations[l].delete(k)
translations[l].deep_merge!(bundle_translations) if k == bundle_str
end
end
js = ""
if for_key.present?
if plugin_for_key = JsLocaleHelper.plugin_translations(locale_str)[bundle_str]
for_key.deep_merge!(plugin_for_key)
end
if translations.present?
js = <<~JS.squish
(function() {
if (window.I18n) {
window.I18n.extras = window.I18n.extras || [];
window.I18n.extras.push(#{for_key.to_json});
window.I18n.extras = #{translations.to_json};
}
})();
JS