Support for other i18n bundles

This commit is contained in:
Robin Ward
2016-08-25 16:33:29 -04:00
parent 7acbd63d92
commit 6070939daa
6 changed files with 103 additions and 15 deletions

View File

@@ -92,6 +92,24 @@ 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);
scope.shift();
while (messages && scope.length > 0) {
currentScope = scope.shift();
messages = messages[currentScope];
}
if (messages) {
return messages;
}
}
}
I18n.lookup = function(scope, options) {
options = options || {};
var lookupInitialScope = scope,
@@ -110,13 +128,20 @@ I18n.lookup = function(scope, options) {
scope = options.scope.toString() + this.defaultSeparator + scope;
}
scope = scope.split(this.defaultSeparator);
var origScope = "" + scope;
scope = origScope.split(this.defaultSeparator);
while (messages && scope.length > 0) {
currentScope = scope.shift();
messages = messages[currentScope];
}
if (!messages) {
messages = checkExtras(origScope, this.defaultSeparator, this.extras);
}
if (!messages) {
if (I18n.fallbacks) {
var fallbacks = this.getFallbacks(locale);
@@ -192,7 +217,9 @@ I18n.interpolate = function(message, options) {
};
I18n.translate = function(scope, options) {
options = this.prepareOptions(options);
var translation = this.lookup(scope, options);
// Fallback to the default locale
if (!translation && this.currentLocale() !== this.defaultLocale && !this.noFallbacks) {

View File

@@ -0,0 +1,31 @@
class ExtraLocalesController < ApplicationController
layout :false
skip_before_filter :check_xhr, :preload_json
def show
locale_str = I18n.locale.to_s
translations = JsLocaleHelper.translations_for(locale_str)
bundle = params[:bundle]
raise Discourse::InvalidAccess.new unless bundle =~ /^[a-z]+$/
for_key = translations[locale_str]["#{bundle}_js"]
if for_key.present?
js = <<-JS
(function() {
if (window.I18n) {
window.I18n.extras = window.I18n.extras || [];
window.I18n.extras.push(#{for_key.to_json});
}
})();
JS
else
js = ""
end
render text: js, content_type: "application/javascript"
end
end

View File

@@ -30,6 +30,7 @@
<%= script "application" %>
<%- if staff? %>
<script src="/extra-locales/admin"></script>
<%= script "admin" %>
<%- end %>