GeezerVison is showing github.com — Merge pull request #622 from movermeyer/movermeyer/resolve_defaults_w… · ruby-i18n/i18n@789d12e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/i18n/backend/base.rb +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,7 @@ def translate(locale, key, options = EMPTY_HASH) | 35 | 35 | if entry.nil? && options.key?(:default) | 36 | 36 | entry = default(locale, key, options[:default], options) | 37 | 37 | else | 38 | | - entry = resolve(locale, key, entry, options) | | 38 | + entry = resolve_entry(locale, key, entry, options) | 39 | 39 | end | 40 | 40 |
| 41 | 41 | count = options[:count] | @@ -154,6 +154,7 @@ def resolve(locale, object, subject, options = EMPTY_HASH) | 154 | 154 | end | 155 | 155 | result unless result.is_a?(MissingTranslation) | 156 | 156 | end | | 157 | + alias_method :resolve_entry, :resolve | 157 | 158 |
| 158 | 159 | # Picks a translation from a pluralized mnemonic subkey according to English | 159 | 160 | # pluralization rules : | |
Collapse file Copy file name to clipboardExpand all lines: lib/i18n/backend/fallbacks.rb +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,13 +64,20 @@ def translate(locale, key, options = EMPTY_HASH) | 64 | 64 | throw(:exception, I18n::MissingTranslation.new(locale, key, options)) | 65 | 65 | end | 66 | 66 |
| 67 | | - def resolve(locale, object, subject, options = EMPTY_HASH) | | 67 | + def resolve_entry(locale, object, subject, options = EMPTY_HASH) | 68 | 68 | return subject if options[:resolve] == false | 69 | | - return super unless subject.is_a?(Symbol) | 70 | | - | 71 | 69 | 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 | 74 | 81 | end | 75 | 82 | result unless result.is_a?(MissingTranslation) | 76 | 83 | end | |
Collapse file Copy file name to clipboardExpand all lines: lib/i18n/backend/simple.rb +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,7 +94,7 @@ def lookup(locale, key, scope = [], options = EMPTY_HASH) | 94 | 94 | return nil unless result.has_key?(_key) | 95 | 95 | end | 96 | 96 | 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) | 98 | 98 | result | 99 | 99 | end | 100 | 100 | end | |
Collapse file Copy file name to clipboardExpand all lines: test/backend/fallbacks_test.rb +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -239,6 +239,28 @@ def setup | 239 | 239 | end | 240 | 240 | end | 241 | 241 |
| | 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 | 242 | 264 |
| 243 | 265 | class I18nBackendFallbacksLocalizeTest < I18n::TestCase | 244 | 266 | class Backend < I18n::Backend::Simple | |
|