FIX: Translation fallback was not using fallback's translation override.

https://meta.discourse.org/t/discobot-falling-back-to-site-locale-is-not-using-customized-copies/65140
This commit is contained in:
Guo Xiang Tan 2017-07-03 14:52:27 +09:00
parent d3ee5752b8
commit 52e654b3ac
3 changed files with 32 additions and 7 deletions

View File

@ -137,21 +137,29 @@ 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]
overrides.each do |_k, v|
if result = v[key]
return result
end
end
end
end
end
translate_no_override(key, options)
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)