Re: [mongoose] Multilingual Schema

1,165 views
Skip to first unread message

Martin Wawrusch

unread,
Sep 1, 2012, 10:30:26 AM9/1/12
to mongoo...@googlegroups.com
The way I have done this on other platforms (mongoid among them) is to create a new data type specific for translatable strings, which seems to be the prudent way to do in mongoose as well. In most systems not all strings are to be translated, and there is also the indexing problem.


On Sat, Sep 1, 2012 at 4:47 AM, hetsch <gerno...@gmail.com> wrote:
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
 
 


hetsch

unread,
Sep 2, 2012, 5:21:00 AM9/2/12
to mongoo...@googlegroups.com
Thank you for your help Martin!

This seems to be the way to go. But one of the problems with that kind of implementation is, that a SchemaType in Mongoose knows nothing (has no reference) about the Schema. In other words, if i create a custom SchemaType, i'm not able to attach virtual path's to the Schema. Like in my example above setting model.name or model.name.i18n which should point to model.name.language.

Thank's for your help again!

hetsch

unread,
Sep 15, 2012, 6:00:16 AM9/15/12
to mongoo...@googlegroups.com
Hi again,

sorry for bringing this topic up again, but I'm still working on that problem.
This time i'm trying to create a new custom SchemaType based on an Nested Object or Mixed one.
I've found out that if i extend the Mixed Schema Type, there's no way to validate the value that should be saved to database.
Think that's by design, so I need to go with a Schema Type that's similar to a Nested Object.
As far as I can tell, Nested Objects allow validation and Type casting. I would need something like this:

DummySchema = new mongoose.Schema({
    name
: {
        en
: {type:String, lowercase:true},
        de
: {type:String, lowercase:true},
        fr
: {type:String, lowercase:true},
     
}
})

Would it be possible to create a new type that mimics a Nested Object like the one in the schema above?
My problem is, that i can't find a way to mimic or extend a Nested Object to get all those features.

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
     
}
})

Hope this somehow illustrates my journey. Thanks again.
Gernot



Marco Pantaleoni

unread,
Sep 17, 2012, 4:29:11 AM9/17/12
to mongoo...@googlegroups.com
On Saturday, September 1, 2012 1:47:56 PM UTC+2, hetsch wrote:
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:

Hi Gernot,
could you please elaborate on the point of validation not working? I can't see why it should not, you are essentially creating sub-paths with properties from the master one, so I'd expect validation to work also for those (apart for the 'required' property, which you are explicitly, and correctly, stripping away).
The only issue I see here is that you are subclassing from Schema which could be an annoyance given the lack of multiple inheritance. Probably I would have used a middleware here (but I'm no mongoose expert).

Cheers,
Marco

Aaron Heckmann

unread,
Feb 2, 2013, 1:39:53 PM2/2/13
to mongoo...@googlegroups.com
might want to search plugins.mongoosejs.com


On Sun, Jan 6, 2013 at 9:34 AM, Roland Lambert <roland...@gmail.com> wrote:
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
 
 
Reply all
Reply to author
Forward
0 new messages