mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Support for other i18n bundles
This commit is contained in:
@@ -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) {
|
||||
|
||||
31
app/controllers/extra_locales_controller.rb
Normal file
31
app/controllers/extra_locales_controller.rb
Normal 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
|
||||
@@ -30,6 +30,7 @@
|
||||
<%= script "application" %>
|
||||
|
||||
<%- if staff? %>
|
||||
<script src="/extra-locales/admin"></script>
|
||||
<%= script "admin" %>
|
||||
<%- end %>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user