我使用db / seeds.rb来播种一些段落;但我需要为多种语言播种(I18n)。
这就是我所拥有的:
school = Building.create(
:body => 'My school'
)
这是我假装的:
school = Building.create(
:body => 'My school'
)
# here I should change locale
school.create(
:body => 'Mi escuela'
)
如何编写代码以便在同一个种子文件中为多个区域设置播种?
答案 0 :(得分:3)
我会使用Globalize Gem:https://github.com/globalize/globalize。请务必查看安装和型号设置说明。
以下是一个例子:
class Post < ActiveRecord::Base
translates :title, :name
end
然后在种子文件中:
I18n.locale = :en
post.title # => 'Globalize rocks!'
post.name # => 'Globalize'
I18n.locale = :nl
post.title # => ''
post.name # => 'Globalize'