From e89f29cca747efbcb057f0004e15e7771dd1ca2e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 29 Dec 2015 10:31:23 +0800 Subject: [PATCH] FIX: Pluralization error when overriding translations. --- lib/i18n/backend/discourse_i18n.rb | 20 ++++++++++++++++---- spec/components/discourse_i18n_spec.rb | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/i18n/backend/discourse_i18n.rb b/lib/i18n/backend/discourse_i18n.rb index c027f88b496..b82b084ac36 100644 --- a/lib/i18n/backend/discourse_i18n.rb +++ b/lib/i18n/backend/discourse_i18n.rb @@ -62,12 +62,24 @@ module I18n results end + # Support interpolation and pluralization of overrides by first looking up + # the original translations before applying our overrides. def lookup(locale, key, scope = [], options = {}) - # Support interpolation and pluralization of overrides - if options[:overrides] + existing_translations = super(locale, key, scope, options) + + if options[:overrides] && existing_translations if options[:count] + + existing_translations = + if existing_translations.is_a?(Hash) + Hash[existing_translations.map { |k, v| [k.to_s.prepend("#{key}."), v] }] + elsif existing_translations.is_a?(String) + Hash[[[key, existing_translations]]] + end + result = {} - options[:overrides].each do |k, v| + + existing_translations.merge(options[:overrides]).each do |k, v| result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s) end return result if result.size > 0 @@ -76,7 +88,7 @@ module I18n return options[:overrides][key] if options[:overrides][key] end - super(locale, key, scope, options) + existing_translations end end diff --git a/spec/components/discourse_i18n_spec.rb b/spec/components/discourse_i18n_spec.rb index c5a5611902d..daebd478092 100644 --- a/spec/components/discourse_i18n_spec.rb +++ b/spec/components/discourse_i18n_spec.rb @@ -101,21 +101,40 @@ describe I18n::Backend::DiscourseI18n do it 'supports interpolation' do TranslationOverride.upsert!('en', 'foo', 'hello %{world}') + I18n.backend.store_translations(:en, foo: 'bar') expect(I18n.translate('foo', world: 'foo')).to eq('hello foo') end it 'supports interpolation named count' do TranslationOverride.upsert!('en', 'wat', 'goodbye %{count}') + I18n.backend.store_translations(:en, wat: 'bar') expect(I18n.translate('wat', count: 123)).to eq('goodbye 123') end it 'supports one and other' do TranslationOverride.upsert!('en', 'items.one', 'one fish') TranslationOverride.upsert!('en', 'items.other', '%{count} fishies') + I18n.backend.store_translations(:en, items: { one: 'one item', other: "%{count} items" }) expect(I18n.translate('items', count: 13)).to eq('13 fishies') expect(I18n.translate('items', count: 1)).to eq('one fish') end + it 'supports one and other when only a single pluralization key is overridden' do + TranslationOverride.upsert!('en', 'keys.magic.other', "no magic keys") + I18n.backend.store_translations(:en, keys: { magic: { one: 'one magic key', other: "%{count} magic keys" } }) + expect(I18n.translate('keys.magic', count: 1)).to eq("one magic key") + expect(I18n.translate('keys.magic', count: 2)).to eq("no magic keys") + end + + it 'supports ActiveModel::Naming#human' do + Fish = Class.new(ActiveRecord::Base) + + TranslationOverride.upsert!('en', 'fish', "fake fish") + I18n.backend.store_translations(:en, fish: "original fish") + + expect(Fish.model_name.human).to eq('Fish') + end + describe "client json" do it "is empty by default" do expect(I18n.client_overrides_json('en')).to eq("{}")