0

我正在使用 Globalize2。我想用 db:seed 为不同的语言环境填充数据库:en、de 和 ru。那么有没有更好的解决方案:

categories = [{ :en => 'Health & Beauty', :ru => 'Красота и здоровье', :de => 'Beauty & Gesundheit'},
              { :en => 'Baby', :ru => 'Детские товары', :de => 'Baby' }]

categories.each_index do |i|
  I18n.locale = 'en'
  Category.create(:name => categories[i][:en])
  I18n.locale = 'ru'
  cat = Category.find(:last)
  cat.name = categories[i][:ru]
  cat.save
  I18n.locale = 'de'
  cat.name = categories[i][:de] 
  cat.save
end

?

4

1 回答 1

2

随着 Globalize2 0.2.0 版本的发布,我们可以使用更简单的代码:

categories = [{ :en => 'Health & Beauty', :ru => 'Красота и здоровье', :de => 'Beauty & Gesundheit'},
              { :en => 'Baby', :ru => 'Детские товары', :de => 'Baby' }]

categories.each_index do |i|
  I18n.locale = 'en'
  cat = Category.new :name => categories[i][:en]
  I18n.locale = 'ru'
  cat.name = categories[i][:ru]
  I18n.locale = 'de'
  cat.name = categories[i][:de]
  cat.save
end
于 2009-12-31T09:05:56.280 回答