Merge pull request #4948 from tgxworld/fix_translation_fallback_not_using_overrides

FIX: Translation fallback was not using fallback's translation override.
This commit is contained in:
Guo Xiang Tan 2017-07-04 09:08:22 +09:00 committed by GitHub
commit a7a8ccb100
3 changed files with 32 additions and 7 deletions

View File

@ -137,17 +137,25 @@ module I18n
load_locale(locale) unless @loaded_locales.include?(locale)
if @overrides_enabled
if by_locale = overrides_by_locale(locale)
overrides = {}
backend.fallbacks(locale).each do |l|
overrides[l] = overrides_by_locale(l)
end
if overrides.present?
if options.present?
options[:overrides] = by_locale
options[:overrides] = overrides
# I18n likes to use throw...
catch(:exception) do
return backend.translate(locale, key, options)
end
else
if result = by_locale[key]
return result
overrides.each do |_k, v|
if result = v[key]
return result
end
end
end
end

View File

@ -72,8 +72,9 @@ module I18n
# the original translations before applying our overrides.
def lookup(locale, key, scope = [], options = {})
existing_translations = super(locale, key, scope, options)
overrides = options.dig(:overrides, locale)
if options[:overrides] && existing_translations
if overrides && existing_translations
if options[:count]
remapped_translations =
@ -85,13 +86,13 @@ module I18n
result = {}
remapped_translations.merge(options[:overrides]).each do |k, v|
remapped_translations.merge(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
end
return options[:overrides][key] if options[:overrides][key]
return overrides[key] if overrides[key]
end
existing_translations

View File

@ -147,6 +147,22 @@ describe I18n::Backend::DiscourseI18n do
expect(I18n.translate('keys.magic', count: 2)).to eq("no magic keys")
end
it "returns the overriden text when falling back" do
TranslationOverride.upsert!('en', 'got', "summer")
I18n.backend.store_translations(:en, got: 'winter')
expect(I18n.translate('got')).to eq('summer')
expect(I18n.with_locale(:zh_TW) { I18n.translate('got') }).to eq('summer')
TranslationOverride.upsert!('en', 'throne', "%{title} is the new queen")
I18n.backend.store_translations(:en, throne: "%{title} is the new king")
expect(I18n.t('throne', title: 'snow')).to eq('snow is the new queen')
expect(I18n.with_locale(:en) { I18n.t('throne', title: 'snow') })
.to eq('snow is the new queen')
end
it 'supports ActiveModel::Naming#human' do
Fish = Class.new(ActiveRecord::Base)