Translatable と呼ばれる RoR 2.3.5 でカスタム 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) になる文字列です。
助けていただければ幸いです。