Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handling storing nil values for some translations broke some things. …

…Should be fixed now.
  • Loading branch information...
commit a0105ec2e83301a84a5b99ad23ae7178695f564c 1 parent 6c2fd96
Shane Mingins authored
Showing with 17 additions and 2 deletions.
  1. +3 −2 lib/i18n_backend_database/database.rb
  2. +14 −0 spec/translate_spec.rb
5 lib/i18n_backend_database/database.rb
View
@@ -40,7 +40,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) || options[:scope].nil?
+ options[:scope] = [options[:scope]] unless options[:scope].is_a?(Array) || options[:scope].blank?
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,7 +50,6 @@ 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)
@@ -77,6 +76,8 @@ def translate(locale, key, options = {})
return translate(@locale.code, default, options.dup)
end
+ # we check the database before creating a translation as we can have translations with nil values
+ translation = @locale.translations.find_by_key_and_pluralization_index(Translation.hk(key), count)
# if we still have no blasted translation just go and create one for the current locale!
translation = @locale.create_translation(key, key, count) unless translation
14 spec/translate_spec.rb
View
@@ -46,6 +46,10 @@
@english_locale.translations.first.key.should == Translation.hk('date.order')
@english_locale.translations.first.raw_key.should == "date.order"
@english_locale.translations.first.value.should be_nil
+
+ # once cached
+ @backend.translate("en", :'date.order').should be_nil
+ @english_locale.reload.should have(1).translation
end
it "should find a cached record from a cache key if it exists in the cache" do
@@ -80,6 +84,16 @@
@backend.translate("en", :"models.blank", options).should == 'post'
end
+ it "should handle translating defaults used by active record attributes" do
+ options = {:scope=>[:activerecord, :attributes], :count=>1, :default=>["Content"]}
+ @backend.translate("en", :"post.content", options).should == 'Content'
+
+ # and when cached
+ options = {:scope=>[:activerecord, :attributes], :count=>1, :default=>["Content"]}
+ @backend.translate("en", :"post.content", options).should == 'Content'
+ @english_locale.should have(1).translation
+ 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}}!!')
Please sign in to comment.
Something went wrong with that request. Please try again.