Skip to content

Commit

Permalink
Added support for default locale translation records to be able to ha…
Browse files Browse the repository at this point in the history
…ve a nil value, and to create records with a nil value when the key is a symbol and not a string.
  • Loading branch information
Shane Mingins authored and Shane Mingins committed Mar 17, 2009
1 parent 6f4d776 commit 0513de7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/i18n_backend_database/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/models/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/models/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions spec/translate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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}}!!')
Expand Down

0 comments on commit 0513de7

Please sign in to comment.