Skip to content

Commit

Permalink
Merge pull request #622 from movermeyer/movermeyer/resolve_defaults_w…
Browse files Browse the repository at this point in the history
…ith_current_locale

Allow overriding of entry resolving entry resolving separate from defaults
  • Loading branch information
radar authored Feb 14, 2022
2 parents 0a9e47a + 19f190d commit 789d12e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def translate(locale, key, options = EMPTY_HASH)
if entry.nil? && options.key?(:default)
entry = default(locale, key, options[:default], options)
else
entry = resolve(locale, key, entry, options)
entry = resolve_entry(locale, key, entry, options)
end

count = options[:count]
Expand Down Expand Up @@ -154,6 +154,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
end
result unless result.is_a?(MissingTranslation)
end
alias_method :resolve_entry, :resolve

# Picks a translation from a pluralized mnemonic subkey according to English
# pluralization rules :
Expand Down
17 changes: 12 additions & 5 deletions lib/i18n/backend/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,20 @@ def translate(locale, key, options = EMPTY_HASH)
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

def resolve(locale, object, subject, options = EMPTY_HASH)
def resolve_entry(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))
options.delete(:fallback_in_progress) if options.key?(:fallback_in_progress)

case subject
when Symbol
I18n.translate(subject, **options.merge(:locale => options[:fallback_original_locale], :throw => true))
when Proc
date_or_time = options.delete(:object) || object
resolve_entry(options[:fallback_original_locale], object, subject.call(date_or_time, **options))
else
subject
end
end
result unless result.is_a?(MissingTranslation)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/backend/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def lookup(locale, key, scope = [], options = EMPTY_HASH)
return nil unless result.has_key?(_key)
end
result = result[_key]
result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
result = resolve_entry(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
result
end
end
Expand Down
22 changes: 22 additions & 0 deletions test/backend/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ def setup
end
end

# See Issue #617
class RegressionTestFor617 < 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 = {:en=>[:en], :"en-US"=>[:"en-US", :en]}
I18n.locale = :'en-US'
store_translations(:"en-US", {})
store_translations(:en, :activerecord=>{:models=>{:product=>{:one=>"Product", :other=>"Products"}, :"product/ticket"=>{:one=>"Ticket", :other=>"Tickets"}}})
end

test 'model scope resolution' do
defaults = [:product, "Ticket"]
options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults}
assert_equal("Ticket", I18n.t(:"product/ticket", **options))
end
end

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

0 comments on commit 789d12e

Please sign in to comment.