Skip to content

Commit

Permalink
Merge pull request #591 from movermeyer/movermeyer/resolve_using_orig…
Browse files Browse the repository at this point in the history
…inal_fallback_locale

Resolve `Symbol`s using the original fallback locale
  • Loading branch information
radar committed Jan 25, 2022
2 parents ade765a + fe72c0f commit be5a8e0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/i18n.rb
Expand Up @@ -23,6 +23,7 @@ module I18n
exception_handler
fallback
fallback_in_progress
fallback_original_locale
format
object
raise
Expand Down
13 changes: 12 additions & 1 deletion lib/i18n/backend/fallbacks.rb
Expand Up @@ -43,7 +43,7 @@ def translate(locale, key, options = EMPTY_HASH)
return super if options[:fallback_in_progress]
default = extract_non_symbol_default!(options) if options[:default]

fallback_options = options.merge(:fallback_in_progress => true)
fallback_options = options.merge(:fallback_in_progress => true, fallback_original_locale: locale)
I18n.fallbacks[locale].each do |fallback|
begin
catch(:exception) do
Expand All @@ -64,6 +64,17 @@ def translate(locale, key, options = EMPTY_HASH)
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

def resolve(locale, object, subject, options = EMPTY_HASH)
return subject if options[:resolve] == false
return super unless subject.is_a?(Symbol)

result = catch(:exception) do
options.delete(:fallback_in_progress)
I18n.translate(subject, **options.merge(locale: options[:fallback_original_locale], throw: true))
end
result unless result.is_a?(MissingTranslation)
end

def extract_non_symbol_default!(options)
defaults = [options[:default]].flatten
first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/tests/localization/procs.rb
Expand Up @@ -74,6 +74,7 @@ def self.inspect_args(args, kwargs)
arg.strftime('%a, %d %b %Y')
when Hash
arg.delete(:fallback_in_progress)
arg.delete(:fallback_original_locale)
arg.inspect
else
arg.inspect
Expand Down
8 changes: 7 additions & 1 deletion lib/i18n/tests/procs.rb
Expand Up @@ -53,7 +53,13 @@ module Procs


def self.filter_args(*args)
args.map {|arg| arg.delete(:fallback_in_progress) if arg.is_a?(Hash) ; arg }.inspect
args.map do |arg|
if arg.is_a?(Hash)
arg.delete(:fallback_in_progress)
arg.delete(:fallback_original_locale)
end
arg
end.inspect
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions test/backend/fallbacks_test.rb
Expand Up @@ -191,6 +191,54 @@ def setup
end
end

# See Issue #590
class I18nBackendFallbacksSymbolReolveRestartsLookupAtOriginalLocale < I18n::TestCase
class Backend < I18n::Backend::Simple
include I18n::Backend::Fallbacks
end

def setup
super
I18n.backend = Backend.new
I18n.enforce_available_locales = false
I18n.fallbacks = [:root]
store_translations(:ak,
'calendars' => {
'gregorian' => {
'months' => {
'format' => {
'abbreviated' => {
1 => 'S-Ɔ'
# Other months omitted for brevity
}
}
}
}
})
store_translations(:root,
'calendars' => {
'gregorian' => {
'months' => {
'format' => {
'abbreviated' => :"calendars.gregorian.months.format.wide",
'wide' => {
1 => 'M01'
# Other months omitted for brevity
}
},
'stand-alone' => {
'abbreviated' => :"calendars.gregorian.months.format.abbreviated"
}
}
}
})
end

test 'falls back to original locale when symbol resolved at fallback locale' do
assert_equal({ 1 => 'S-Ɔ' }, I18n.t('calendars.gregorian.months.stand-alone.abbreviated', locale: :"ak-GH"))
end
end


class I18nBackendFallbacksLocalizeTest < I18n::TestCase
class Backend < I18n::Backend::Simple
Expand Down

0 comments on commit be5a8e0

Please sign in to comment.