Skip to content

Commit 789d12e

Browse files
authored
Merge pull request #622 from movermeyer/movermeyer/resolve_defaults_with_current_locale
Allow overriding of entry resolving entry resolving separate from defaults
2 parents 0a9e47a + 19f190d commit 789d12e

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

Diff for: lib/i18n/backend/base.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def translate(locale, key, options = EMPTY_HASH)
3535
if entry.nil? && options.key?(:default)
3636
entry = default(locale, key, options[:default], options)
3737
else
38-
entry = resolve(locale, key, entry, options)
38+
entry = resolve_entry(locale, key, entry, options)
3939
end
4040

4141
count = options[:count]
@@ -154,6 +154,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH)
154154
end
155155
result unless result.is_a?(MissingTranslation)
156156
end
157+
alias_method :resolve_entry, :resolve
157158

158159
# Picks a translation from a pluralized mnemonic subkey according to English
159160
# pluralization rules :

Diff for: lib/i18n/backend/fallbacks.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ def translate(locale, key, options = EMPTY_HASH)
6464
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
6565
end
6666

67-
def resolve(locale, object, subject, options = EMPTY_HASH)
67+
def resolve_entry(locale, object, subject, options = EMPTY_HASH)
6868
return subject if options[:resolve] == false
69-
return super unless subject.is_a?(Symbol)
70-
7169
result = catch(:exception) do
72-
options.delete(:fallback_in_progress)
73-
I18n.translate(subject, **options.merge(locale: options[:fallback_original_locale], throw: true))
70+
options.delete(:fallback_in_progress) if options.key?(:fallback_in_progress)
71+
72+
case subject
73+
when Symbol
74+
I18n.translate(subject, **options.merge(:locale => options[:fallback_original_locale], :throw => true))
75+
when Proc
76+
date_or_time = options.delete(:object) || object
77+
resolve_entry(options[:fallback_original_locale], object, subject.call(date_or_time, **options))
78+
else
79+
subject
80+
end
7481
end
7582
result unless result.is_a?(MissingTranslation)
7683
end

Diff for: lib/i18n/backend/simple.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def lookup(locale, key, scope = [], options = EMPTY_HASH)
9494
return nil unless result.has_key?(_key)
9595
end
9696
result = result[_key]
97-
result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
97+
result = resolve_entry(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
9898
result
9999
end
100100
end

Diff for: test/backend/fallbacks_test.rb

+22
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,28 @@ def setup
239239
end
240240
end
241241

242+
# See Issue #617
243+
class RegressionTestFor617 < I18n::TestCase
244+
class Backend < I18n::Backend::Simple
245+
include I18n::Backend::Fallbacks
246+
end
247+
248+
def setup
249+
super
250+
I18n.backend = Backend.new
251+
I18n.enforce_available_locales = false
252+
I18n.fallbacks = {:en=>[:en], :"en-US"=>[:"en-US", :en]}
253+
I18n.locale = :'en-US'
254+
store_translations(:"en-US", {})
255+
store_translations(:en, :activerecord=>{:models=>{:product=>{:one=>"Product", :other=>"Products"}, :"product/ticket"=>{:one=>"Ticket", :other=>"Tickets"}}})
256+
end
257+
258+
test 'model scope resolution' do
259+
defaults = [:product, "Ticket"]
260+
options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults}
261+
assert_equal("Ticket", I18n.t(:"product/ticket", **options))
262+
end
263+
end
242264

243265
class I18nBackendFallbacksLocalizeTest < I18n::TestCase
244266
class Backend < I18n::Backend::Simple

0 commit comments

Comments
 (0)