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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding of entry resolving entry resolving separate from defaults #622

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
3 changes: 2 additions & 1 deletion lib/i18n/backend/base.rb
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
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
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
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