mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Support for overriding client side translation keys
This commit is contained in:
parent
523138f1fd
commit
1506eba28d
5 changed files with 75 additions and 27 deletions
|
@ -0,0 +1,28 @@
|
|||
export default {
|
||||
name: 'localization',
|
||||
after: 'inject-objects',
|
||||
|
||||
initialize: function(container) {
|
||||
const siteSettings = container.lookup('site-settings:main');
|
||||
if (siteSettings.verbose_localization) {
|
||||
I18n.enable_verbose_localization();
|
||||
}
|
||||
|
||||
// Merge any overrides into our object
|
||||
const overrides = PreloadStore.get('translationOverrides') || {};
|
||||
Object.keys(overrides).forEach(k => {
|
||||
const v = overrides[k];
|
||||
|
||||
const segs = k.split('.');
|
||||
let node = I18n.translations[I18n.locale];
|
||||
let i = 0;
|
||||
for (; node && i<segs.length-1; i++) {
|
||||
node = node[segs[i]];
|
||||
}
|
||||
|
||||
if (node && i == segs.length-1) {
|
||||
node[segs[segs.length-1]] = v;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
|
@ -1,11 +0,0 @@
|
|||
export default {
|
||||
name: 'verbose-localization',
|
||||
after: 'inject-objects',
|
||||
|
||||
initialize: function(container) {
|
||||
var siteSettings = container.lookup('site-settings:main');
|
||||
if (siteSettings.verbose_localization) {
|
||||
I18n.enable_verbose_localization();
|
||||
}
|
||||
}
|
||||
};
|
|
@ -288,6 +288,7 @@ class ApplicationController < ActionController::Base
|
|||
store_preloaded("customHTML", custom_html_json)
|
||||
store_preloaded("banner", banner_json)
|
||||
store_preloaded("customEmoji", custom_emoji)
|
||||
store_preloaded("translationOverrides", I18n.client_overrides_json)
|
||||
end
|
||||
|
||||
def preload_current_user_data
|
||||
|
|
|
@ -72,26 +72,37 @@ module I18n
|
|||
end
|
||||
end
|
||||
|
||||
def overrides_by_locale
|
||||
return unless @overrides_enabled
|
||||
|
||||
site = RailsMultisite::ConnectionManagement.current_db
|
||||
|
||||
by_site = @overrides_by_site[site]
|
||||
|
||||
by_locale = nil
|
||||
unless by_site
|
||||
by_site = @overrides_by_site[site] = {}
|
||||
|
||||
# Load overrides
|
||||
TranslationOverride.where(locale: locale).pluck(:translation_key, :value).each do |tuple|
|
||||
by_locale = by_site[locale] ||= {}
|
||||
by_locale[tuple[0]] = tuple[1]
|
||||
end
|
||||
end
|
||||
|
||||
by_site[config.locale]
|
||||
end
|
||||
|
||||
def client_overrides_json
|
||||
client_json = (overrides_by_locale || {}).select {|k, _| k.starts_with?('js.')}
|
||||
MultiJson.dump(client_json)
|
||||
end
|
||||
|
||||
def translate(key, *args)
|
||||
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
||||
|
||||
if @overrides_enabled
|
||||
site = RailsMultisite::ConnectionManagement.current_db
|
||||
|
||||
by_site = @overrides_by_site[site]
|
||||
|
||||
by_locale = nil
|
||||
unless by_site
|
||||
by_site = @overrides_by_site[site] = {}
|
||||
|
||||
# Load overrides
|
||||
TranslationOverride.where(locale: locale).pluck(:translation_key, :value).each do |tuple|
|
||||
by_locale = by_site[locale] ||= {}
|
||||
by_locale[tuple[0]] = tuple[1]
|
||||
end
|
||||
end
|
||||
|
||||
by_locale = by_site[config.locale]
|
||||
by_locale = overrides_by_locale
|
||||
if by_locale
|
||||
if args.size > 0 && args[0].is_a?(Hash)
|
||||
args[0][:overrides] = by_locale
|
||||
|
|
|
@ -91,6 +91,25 @@ describe I18n::Backend::DiscourseI18n do
|
|||
expect(I18n.translate('items', count: 13)).to eq('13 fishies')
|
||||
expect(I18n.translate('items', count: 1)).to eq('one fish')
|
||||
end
|
||||
|
||||
describe "client json" do
|
||||
it "is empty by default" do
|
||||
expect(I18n.client_overrides_json).to eq("{}")
|
||||
end
|
||||
|
||||
it "doesn't return server overrides" do
|
||||
TranslationOverride.upsert!('en', 'foo', 'bar')
|
||||
expect(I18n.client_overrides_json).to eq("{}")
|
||||
end
|
||||
|
||||
it "returns client overrides" do
|
||||
TranslationOverride.upsert!('en', 'js.foo', 'bar')
|
||||
json = ::JSON.parse(I18n.client_overrides_json)
|
||||
|
||||
expect(json).to be_present
|
||||
expect(json['js.foo']).to eq('bar')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue