我正在尝试在 RoR 2.3.5 中创建一个名为 Translatable 的自定义 MongoMapper 数据类型:
class Translatable < String
def initialize(translation, culture="en")
end
def languages
end
def has_translation(culture)?
end
def self.to_mongo(value)
end
def self.from_mongo(value)
end
end
我希望能够像这样使用它:
class Page
include MongoMapper::Document
key :title, Translatable, :required => true
key :content, String
end
然后像这样实现:
p = Page.new
p.title = "Hello"
p.title(:fr) = "Bonjour"
p.title(:es) = "Hola"
p.content = "Some content here"
p.save
p = Page.first
p.languages
=> [:en, :fr, :es]
p.has_translation(:fr)
=> true
en = p.title
=> "Hello"
en = p.title(:en)
=> "Hello"
fr = p.title(:fr)
=> "Bonjour"
es = p.title(:es)
=> "Hola"
在 mongoDB 中,我想这些信息会像这样存储:
{ "_id" : ObjectId("4b98cd7803bca46ca6000002"), "title" : { "en" :
"Hello", "fr" : "Bonjour", "es" : "Hola" }, "content" : "Some content
here" }
所以 Page.title 是一个字符串,当没有指定文化时默认为英语 (:en)。
我真的很感激任何帮助。