Skip to content

Commit

Permalink
Continued add better support for handling default locale translations…
Browse files Browse the repository at this point in the history
… that should have a nil value.

Translate calls with symbols will now create a record with a nil value if it is the first time used and now translation record exists.
Raw key values are now stored in translation table.
  • Loading branch information
Shane Mingins authored and Shane Mingins committed Mar 17, 2009
1 parent 0513de7 commit 6c2fd96
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
Expand Up @@ -8,11 +8,12 @@ def self.up

create_table :translations do |t|
t.string :key
t.string :raw_key
t.string :value
t.integer :pluralization_index, :default => 1
t.integer :locale_id
end
add_index :translations, [:locale_id, :key]
add_index :translations, [:locale_id, :key, :pluralization_index]

end

Expand Down
2 changes: 1 addition & 1 deletion lib/i18n_backend_database/database.rb
Expand Up @@ -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] = [options[:scope]] unless options[:scope].is_a?(Array) || options[:scope].nil?
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 Down
2 changes: 1 addition & 1 deletion lib/models/locale.rb
Expand Up @@ -20,7 +20,7 @@ def translation_from_key(key)
end

def create_translation(key, value, pluralization_index=1)
conditions = {:key => key, :pluralization_index => pluralization_index}
conditions = {:key => key, :raw_key => key.to_s, :pluralization_index => pluralization_index}

# 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))
Expand Down
2 changes: 1 addition & 1 deletion lib/views/translations/translations.html.erb
Expand Up @@ -23,7 +23,7 @@
<% for translation in @translations %>
<div id="<%= "translation_#{translation.class}_#{translation.id}" %>">
<% remote_form_for([@locale, translation]) do |f| %>
<p><%=h translation.default_locale_value %></p>
<p><%=h translation.default_locale_value || translation.raw_key %></p>
<p>
<%= f.text_field :value %>
<%= f.submit "Update" %>
Expand Down
10 changes: 6 additions & 4 deletions spec/translate_spec.rb
Expand Up @@ -24,6 +24,7 @@
@backend.translate("en", "String").should == "String"
@english_locale.should have(1).translation
@english_locale.translations.first.key.should == Translation.hk("String")
@english_locale.translations.first.raw_key.should == "String"
@english_locale.translations.first.value.should == "String"
end

Expand All @@ -34,15 +35,16 @@
end

it "should support having a record with a nil value" do
@english_locale.translations.create!(:key => '.date.order')
@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.key.should == Translation.hk('date.order')
@english_locale.translations.first.raw_key.should == "date.order"
@english_locale.translations.first.value.should be_nil
end

Expand Down Expand Up @@ -203,11 +205,11 @@
end

it "should support having a default locale record with a nil value" do
@english_locale.translations.create!(:key => '.date.order')
@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.key.should == Translation.hk('date.order')
@spanish_locale.translations.first.value.should be_nil
end

Expand Down
2 changes: 1 addition & 1 deletion tasks/i18n.rake
Expand Up @@ -2,7 +2,7 @@ def load_default_locales(path_to_file=nil)
path_to_file ||= File.join(File.dirname(__FILE__), "../data", "locales.yml")
data = YAML::load(IO.read(path_to_file))
data.each do |code, y|
Locale.create({:code => code, :name => y["name"]})
Locale.create({:code => code, :name => y["name"]}) unless Locale.exists?(:code => code)
end
end

Expand Down

0 comments on commit 6c2fd96

Please sign in to comment.