diff --git a/lib/i18n_backend_database/database.rb b/lib/i18n_backend_database/database.rb index 44bdd9b..fe93446 100644 --- a/lib/i18n_backend_database/database.rb +++ b/lib/i18n_backend_database/database.rb @@ -41,7 +41,7 @@ def translate(locale, key, options = {}) # create a composite key if scope provided original_key = key options[:scope] = [options[:scope]] unless options[:scope].is_a?(Array) - key = "#{options[:scope].join('.')}.#{key}" if options[:scope] && key.is_a?(Symbol) + key = :"#{options[:scope].join('.')}.#{key}" if options[:scope] && key.is_a?(Symbol) count = (options[:count].nil? || options[:count] == 1) ? 1 : 0 cache_key = Translation.ck(@locale, key, count) @@ -50,6 +50,7 @@ def translate(locale, key, options = {}) if @cache_store.exist?(cache_key) translation = @cache_store.read(cache_key) + return translation if translation.nil? && @locale.default_locale? return interpolate(@locale.code, translation, values) if translation else translation = @locale.translations.find_by_key_and_pluralization_index(Translation.hk(key), count) diff --git a/lib/models/locale.rb b/lib/models/locale.rb index c9bf852..52c4f28 100644 --- a/lib/models/locale.rb +++ b/lib/models/locale.rb @@ -22,8 +22,8 @@ def translation_from_key(key) def create_translation(key, value, pluralization_index=1) conditions = {:key => key, :pluralization_index => pluralization_index} - # set the key as the value if we're using the default locale - conditions.merge!({:value => value}) if (self.code == I18n.default_locale.to_s) + # set the key as the value if we're using the default locale and the key is a string + conditions.merge!({:value => value}) if (self.code == I18n.default_locale.to_s && key.is_a?(String)) translation = self.translations.create(conditions) # hackity hack. bug #922 maybe? diff --git a/lib/models/translation.rb b/lib/models/translation.rb index 41d6a8a..7da1113 100644 --- a/lib/models/translation.rb +++ b/lib/models/translation.rb @@ -23,7 +23,7 @@ def value_or_default(key) # create hash key def self.hk(key) - Base64.encode64(Digest::MD5.hexdigest(key)) + Base64.encode64(Digest::MD5.hexdigest(key.to_s)) end # create cache key diff --git a/spec/translate_spec.rb b/spec/translate_spec.rb index 14406f9..8c32266 100644 --- a/spec/translate_spec.rb +++ b/spec/translate_spec.rb @@ -33,18 +33,45 @@ @english_locale.should have(1).translation end + it "should support having a record with a nil value" do + @english_locale.translations.create!(:key => '.date.order') + @backend.translate("en", :'date.order').should be_nil + @english_locale.should have(1).translation + end + + it "should create a record with a nil value when key is a symbol" do + @backend.translate("en", :'date.order').should be_nil + @english_locale.should have(1).translation + @english_locale.translations.first.key.should == Translation.hk('.date.order') + @english_locale.translations.first.value.should be_nil + end + it "should find a cached record from a cache key if it exists in the cache" do hash_key = Translation.hk("blah") @backend.cache_store.write("en:#{hash_key}:1", 'woot') @backend.translate("en", "blah").should == "woot" end + it "should find a cached record with a nil value from a cache key if it exists in the cache" do + hash_key = Translation.hk(".date.order") + @backend.cache_store.write("en:#{hash_key}:1", nil) + @backend.translate("en", :'date.order').should be_nil + end + it "should write a cache record to the cache for a newly created translation record" do hash_key = Translation.hk("blah") @backend.translate("en", "blah") @backend.cache_store.read("en:#{hash_key}:1").should == "blah" end + it "should write a cache record to the cache for translation record with nil value" do + @english_locale.translations.create!(:key => '.date.order') + @backend.translate("en", :'date.order').should be_nil + + hash_key = Translation.hk(".date.order") + @backend.cache_store.read("en:#{hash_key}:1").should be_nil + end + it "should handle active record helper defaults, where default is the object name" do options = {:count=>1, :scope=>[:activerecord, :models], :default=>"post"} @english_locale.translations.create!(:key => 'activerecord.errors.models.blank', :value => 'post') @@ -175,6 +202,15 @@ @spanish_locale.should have(1).translation end + it "should support having a default locale record with a nil value" do + @english_locale.translations.create!(:key => '.date.order') + @backend.translate("es", :'date.order').should be_nil + + @spanish_locale.should have(1).translation + @spanish_locale.translations.first.key.should == Translation.hk('.date.order') + @spanish_locale.translations.first.value.should be_nil + end + it "should be able to handle interpolated values" do options = {:some_value => 'INTERPOLATED'} @english_locale.translations.create!(:key => 'Fred', :value => 'Fred has been {{some_value}}!!')