diff --git a/lib/globalize/model/active_record/adapter.rb b/lib/globalize/model/active_record/adapter.rb index d52e940..fadedf2 100644 --- a/lib/globalize/model/active_record/adapter.rb +++ b/lib/globalize/model/active_record/adapter.rb @@ -9,8 +9,14 @@ module Globalize def write(locale, attr_name, value) locale = locale.to_sym - self[locale] ||= {} - self[locale][attr_name] = value + if value.is_a?(String) + self[locale] ||= {} + self[locale][attr_name] = value + elsif value.is_a?(Hash) + value.each do |locale, value| + self.write(locale, attr_name, value) + end + end end end diff --git a/test/model/active_record/translated_test.rb b/test/model/active_record/translated_test.rb index 1c0e69b..d54b0b9 100644 --- a/test/model/active_record/translated_test.rb +++ b/test/model/active_record/translated_test.rb @@ -215,6 +215,23 @@ class TranslatedTest < ActiveSupport::TestCase post = Post.find_by_subject('foo') assert_equal foo, post end + + test "create with multiple translation at once" do + foo = Post.create :subject => {:'en-US' => 'foo', :'de-DE' => 'bar'} + I18n.locale = 'en-US' + assert_equal 'foo', foo.subject + I18n.locale = 'de-DE' + assert_equal 'bar', foo.subject + end + + test "should be able update with multiple translation" do + foo = Post.create :subject => {:'en-US' => 'foo', :'de-DE' => 'bar'} + foo.subject = {:'en-US' => 'bar', :'de-DE' => 'foo'} + I18n.locale = 'en-US' + assert_equal 'bar', foo.subject + I18n.locale = 'de-DE' + assert_equal 'foo', foo.subject + end end # TODO error checking for fields that exist in main table, don't exist in