FIX: Pluralization error when overriding translations.

This commit is contained in:
Guo Xiang Tan 2015-12-29 10:31:23 +08:00
parent 0613399d7a
commit e89f29cca7
2 changed files with 35 additions and 4 deletions

View file

@ -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

View file

@ -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("{}")