Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve Symbols using the original fallback locale #591

Merged
merged 1 commit into from Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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