Changeset 109

Show
Ignore:
Timestamp:
04/25/07 20:33:41 (5 years ago)
Author:
saimon
Message:

--Allowing for the possibility of fallbacks for the base locale in internal engine.

e.g. If value of attribute is nil when Locale.base? and fallback option

is active, then fallbacks are also checked for the first non-nil value.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/saimon/trunk_with_rfc4646_support/lib/globalize/localization/db_translate.rb

    r107 r109  
    470470                else 
    471471                  value = read_attribute(:#{facet}) 
     472                  #If the base locale value is nil and fallback is active... 
     473                  unless value 
     474                    if @@facet_options[:#{facet}][:fallback] 
     475                      #If fallbacks are active then go through each fallback locale 
     476                      #and look for a translation 
     477                      Locale.active.possible_languages.each do |fallback| 
     478                        unless Locale.base_language.code == fallback.code 
     479                          localized_method = "#{facet}_\#{fallback.code}" 
     480                          value = read_attribute(localized_method.to_sym) 
     481                          break if value 
     482                        end 
     483                      end 
     484                    end 
     485                  end 
    472486                end 
    473487                value.nil? ? nil : add_bidi(value, :#{facet}) 
     
    500514                else 
    501515                  value = read_attribute_before_type_cast('#{facet}') 
     516                  #If the base locale value is nil and fallback is active... 
     517                  unless value 
     518                    if @@facet_options[:#{facet}][:fallback] 
     519                      #If fallbacks are active then go through each fallback locale 
     520                      #and look for a translation 
     521                      Locale.active.possible_languages.each do |fallback| 
     522                        unless Locale.base_language.code == fallback.code 
     523                          localized_method = "#{facet}_\#{fallback.code}_before_type_cast" 
     524                          value = send(localized_method.to_sym) 
     525                          break if value 
     526                        end 
     527                      end 
     528                    end 
     529                  end 
    502530                end 
    503531                value.nil? ? nil : add_bidi(value, :#{facet}) 
  • branches/saimon/trunk_with_rfc4646_support/test/db_localizes_translates_test.rb

    r108 r109  
    4545 
    4646  def setup 
    47     Globalize::Locale.set_base_language('en'
     47    Globalize::Locale.set_base_language(Language.pick('en')
    4848    Globalize::Locale.set('en','US') 
    4949  end 
     
    551551    assert_nil prod.description_before_type_cast 
    552552  end 
     553 
     554  def test_fallbacks_for_base_locale 
     555 
     556    Globalize::Locale.set_base_language(Language.pick('es-MX')) 
     557 
     558    Globalize::Locale.set('he','IL') 
     559    prod = Product.create!(:code => 'test-fallback 2', 
     560                           :name => 'hebrew name fallbacks 2', 
     561                           :description => 'hebrew desc fallbacks 2') 
     562    assert_equal 'hebrew name fallbacks 2', prod.name 
     563    assert_equal 'hebrew desc fallbacks 2', prod.description 
     564    assert_equal 'hebrew name fallbacks 2', prod.name_before_type_cast 
     565    assert_equal 'hebrew desc fallbacks 2', prod.description_before_type_cast 
     566 
     567    Globalize::Locale.set('es-MX','MX') 
     568    assert_nil prod.name 
     569    assert_nil prod.name_before_type_cast 
     570    assert_nil prod.description 
     571    assert_nil prod.description_before_type_cast 
     572 
     573    Globalize::Locale.set('es','ES') 
     574    prod.name = 'spanish name fallbacks 2' 
     575    prod.description = 'spanish desc fallbacks 2' 
     576    prod.save! 
     577    assert_equal 'spanish name fallbacks 2', prod.name 
     578    assert_equal 'spanish desc fallbacks 2', prod.description 
     579    assert_equal 'spanish name fallbacks 2', prod.name_before_type_cast 
     580    assert_equal 'spanish desc fallbacks 2', prod.description_before_type_cast 
     581 
     582    Globalize::Locale.set('es-MX','MX') 
     583    assert_equal 'spanish name fallbacks 2', prod.name 
     584    assert_equal 'spanish name fallbacks 2', prod.name_before_type_cast 
     585    assert_nil prod.description 
     586    assert_nil prod.description_before_type_cast 
     587 
     588    Globalize::Locale.set('es-MX','ES', [['he','IL']]) 
     589    assert_equal 'hebrew name fallbacks 2', prod.name 
     590    assert_equal 'hebrew name fallbacks 2', prod.name_before_type_cast 
     591    assert_nil prod.description 
     592    assert_nil prod.description_before_type_cast 
     593 
     594    Globalize::Locale.set_base_language(Language.pick('en')) 
     595  end 
     596 
    553597end