Hi,
i am trying to find out an acceptable way of defining a multilingual schema. The code at the bottom is my current try to solve that specific problem. I'm currently not happy to that implementation (validation not working and sure some other things that I completely missed). Would it be easier to create a new SchemaType or Plugin? Has someone ever tried to achieve the same? Think I let my code speak:
languages = ['de', 'en', 'es']
defaultLanguage = 'de'
getLanguage = () ->
return 'de'
class MultilingualSchema extends Schema
multilingualValues: []
constructor: (obj, options) ->
#console.log(obj, options)
for key, value of obj
# check if we have a multilingual path
if 'i18n' of value and value['i18n'] is true
@multilingualValues.push key
struct = {}
for lang in languages
o = _.clone(value)
# only the default language path has to be required
if lang isnt defaultLanguage and o['required']
delete o['required']
struct[lang] = o
# saving the new multilingual structures
obj[key] = struct
super(obj, options)
for key in @multilingualValues
# setting virtual methods for shorthand getters and setters
v = "#{key}.i18n"
@virtual(v).get () ->
return @[key][getLanguage()]
@virtual(v).set (value) ->
return @[key][getLanguage()] = value
DummySchema = new Schema( #new MultilingualSchema(
name:
i18n: true
type: String
trim: true
required: true
)
Dummy = db.model("Dummy", DummySchema)
d = new Dummy()
d.name.de = "aaa"
# it would be nice if 'd.name' would also point to the current set language
d.name.en = "bbb"
d.name.es = "ccc"
d.save (err) ->
if err
return console.log err
d.name.i18n = 'ddd'
d.save (err) ->
if err
return console.log err
console.log d.name.i18n # same as d.name.de - it would be optimal if d.name would point to the actual language
console.log d.name.en
console.log d.name.es
Thank you for any input on this.
Regards, Gernot
--
--
http://mongoosejs.com - docs
http://plugins.mongoosejs.com - plugins search
http://github.com/learnboost/mongoose - source code
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
DummySchema = new mongoose.Schema({
name: {
en: {type:String, lowercase:true},
de: {type:String, lowercase:true},
fr: {type:String, lowercase:true},
}
})var languages = ("en", "de", "fr")
DummySchema = new mongoose.Schema({
// delivers the value for the current active language and is also capable of validating the value
// db structure should be an object like {name: {de:"a", en:"b", fr:"c"}}
name: {
type: Multilingual,
cast: String,
lowercase: true
}
})Hi,
i am trying to find out an acceptable way of defining a multilingual schema. The code at the bottom is my current try to solve that specific problem. I'm currently not happy to that implementation (validation not working and sure some other things that I completely missed). Would it be easier to create a new SchemaType or Plugin? Has someone ever tried to achieve the same? Think I let my code speak:
Hello,Is there a preferred way to achieve multilingual fields in mongoose? Is there a plugin already exist that achieves this?Thanks
--
--
http://mongoosejs.com - docs
http://plugins.mongoosejs.com - plugins search
http://github.com/learnboost/mongoose - source code
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en