mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
FIX: Pluralization error when overriding translations.
This commit is contained in:
parent
0613399d7a
commit
e89f29cca7
2 changed files with 35 additions and 4 deletions
|
@ -62,12 +62,24 @@ module I18n
|
||||||
results
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Support interpolation and pluralization of overrides by first looking up
|
||||||
|
# the original translations before applying our overrides.
|
||||||
def lookup(locale, key, scope = [], options = {})
|
def lookup(locale, key, scope = [], options = {})
|
||||||
# Support interpolation and pluralization of overrides
|
existing_translations = super(locale, key, scope, options)
|
||||||
if options[:overrides]
|
|
||||||
|
if options[:overrides] && existing_translations
|
||||||
if options[:count]
|
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 = {}
|
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)
|
result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s)
|
||||||
end
|
end
|
||||||
return result if result.size > 0
|
return result if result.size > 0
|
||||||
|
@ -76,7 +88,7 @@ module I18n
|
||||||
return options[:overrides][key] if options[:overrides][key]
|
return options[:overrides][key] if options[:overrides][key]
|
||||||
end
|
end
|
||||||
|
|
||||||
super(locale, key, scope, options)
|
existing_translations
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -101,21 +101,40 @@ describe I18n::Backend::DiscourseI18n do
|
||||||
|
|
||||||
it 'supports interpolation' do
|
it 'supports interpolation' do
|
||||||
TranslationOverride.upsert!('en', 'foo', 'hello %{world}')
|
TranslationOverride.upsert!('en', 'foo', 'hello %{world}')
|
||||||
|
I18n.backend.store_translations(:en, foo: 'bar')
|
||||||
expect(I18n.translate('foo', world: 'foo')).to eq('hello foo')
|
expect(I18n.translate('foo', world: 'foo')).to eq('hello foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'supports interpolation named count' do
|
it 'supports interpolation named count' do
|
||||||
TranslationOverride.upsert!('en', 'wat', 'goodbye %{count}')
|
TranslationOverride.upsert!('en', 'wat', 'goodbye %{count}')
|
||||||
|
I18n.backend.store_translations(:en, wat: 'bar')
|
||||||
expect(I18n.translate('wat', count: 123)).to eq('goodbye 123')
|
expect(I18n.translate('wat', count: 123)).to eq('goodbye 123')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'supports one and other' do
|
it 'supports one and other' do
|
||||||
TranslationOverride.upsert!('en', 'items.one', 'one fish')
|
TranslationOverride.upsert!('en', 'items.one', 'one fish')
|
||||||
TranslationOverride.upsert!('en', 'items.other', '%{count} fishies')
|
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: 13)).to eq('13 fishies')
|
||||||
expect(I18n.translate('items', count: 1)).to eq('one fish')
|
expect(I18n.translate('items', count: 1)).to eq('one fish')
|
||||||
end
|
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
|
describe "client json" do
|
||||||
it "is empty by default" do
|
it "is empty by default" do
|
||||||
expect(I18n.client_overrides_json('en')).to eq("{}")
|
expect(I18n.client_overrides_json('en')).to eq("{}")
|
||||||
|
|
Loading…
Reference in a new issue