From 1506eba28dbc883b51b4f99e251dcafafb37181c Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 20 Nov 2015 17:13:37 -0500 Subject: [PATCH] Support for overriding client side translation keys --- .../initializers/localization.js.es6 | 28 ++++++++++++ .../initializers/verbose-localization.js.es6 | 11 ----- app/controllers/application_controller.rb | 1 + lib/freedom_patches/translate_accelerator.rb | 43 ++++++++++++------- spec/components/discourse_i18n_spec.rb | 19 ++++++++ 5 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 app/assets/javascripts/discourse/initializers/localization.js.es6 delete mode 100644 app/assets/javascripts/discourse/initializers/verbose-localization.js.es6 diff --git a/app/assets/javascripts/discourse/initializers/localization.js.es6 b/app/assets/javascripts/discourse/initializers/localization.js.es6 new file mode 100644 index 000000000..5c9f3292c --- /dev/null +++ b/app/assets/javascripts/discourse/initializers/localization.js.es6 @@ -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 0 && args[0].is_a?(Hash) args[0][:overrides] = by_locale diff --git a/spec/components/discourse_i18n_spec.rb b/spec/components/discourse_i18n_spec.rb index 551c05aa3..dc6cd2293 100644 --- a/spec/components/discourse_i18n_spec.rb +++ b/spec/components/discourse_i18n_spec.rb @@ -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