Skip to content

Commit d02b488

Browse files
author
Sergey Dolin
committed
v 1.0.1 fully redone
1 parent aaf3a04 commit d02b488

File tree

4 files changed

+228
-144
lines changed

4 files changed

+228
-144
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,26 @@ approverSchema = new mongoose.Schema({
3131
localeN: {type:String},
3232
}
3333
});
34-
approverSchema.virtual('localized.name').get(function () {
34+
approverSchema.virtual('name._').get(function () {
3535
// return name in the current locale
3636
...
37-
});
37+
}).set(function (v) {
38+
// set name of the current locale to v
39+
...
40+
})
41+
;
3842
```
39-
The "localize" attribute will be removed from source Scheme. It's nor a bug neither a feature, it is "by design".
4043

4144
While the module must be required and setLocales must be called before the first Schema added the current locale may be set and changed in any moment.
4245

4346
```javascript
44-
mongoose_localize.setLocale('locale2');
47+
mongoose_localize.setCurrenLocale('locale2');
4548
```
49+
50+
Locales and current locales may be retrieved with
51+
52+
```javascript
53+
mongoose_localize.locales();
54+
mongoose_localize.currentLocale();
55+
```
56+

index.js

Lines changed: 92 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,103 @@
33
var mongoose = require('mongoose');
44

55
var locales=['en','ru'];
6-
var locale='en';
6+
var locale=locales[0];
7+
8+
function currentLocale(){
9+
return locale?locale:((locales.length>0)?locales[0]:undefined);
10+
}
711

812
var prototype_mongoose=function(){
9-
var ma = mongoose.Schema.prototype.add;
10-
var addI18n=function(schema,obj){
11-
var keys = Object.keys(obj);
12-
13-
if (keys.length==1 && keys=='_id') return obj;
14-
15-
var ret={};
16-
17-
for (var i = 0; i < keys.length; ++i) {
18-
var key = keys[i];
19-
var val = obj[key];
20-
21-
if (key==='type'){
22-
ret[key]=val;
23-
continue;
24-
}
25-
26-
if (typeof val != "object") {
27-
ret[key]=val;
28-
continue;
29-
};
30-
31-
if (val instanceof Array) {
32-
ret[key]=val;
33-
continue;
34-
};
35-
36-
var kkeys=Object.keys(val);
37-
var localize=false;
38-
for (var ii=0; ii<kkeys.length;++ii){
39-
var kkey=kkeys[ii];
40-
var vval=val[kkey];
41-
if (typeof vval==="object"){
42-
val[kkey]=addI18n(schema,vval)
43-
}else{
44-
if (vval && kkey=="localize"){
45-
localize=true;
46-
}
47-
}
48-
}
49-
if (localize){
50-
delete(val.localize);
51-
var nval={};
52-
for (var j=0;j<locales.length;j++){
53-
nval[locales[j]]=val;// Note: must live without copy JSON.parse(JSON.stringify(va)) because of [Function];
54-
//nval[locales[j]]=JSON.parse(JSON.stringify(val));
55-
}
56-
//ret['_localized_'+key]=nval;
57-
ret[key]=nval;
58-
schema.virtual('localized.'+key)
59-
.get(function(value,virtual){
60-
var n=virtual.path.substring(10);
61-
return this[n][locale];
62-
})/*
63-
.set(function(value,virtual){
64-
var n=virtual.path.substring(10);
65-
this[n]=value;
66-
});*/
67-
}else{
68-
ret[key]=val;
69-
}
70-
};
71-
return ret;
72-
}
73-
74-
mongoose.Schema.prototype.add = function add (obj, prefix) {
75-
var oobj=addI18n(this,obj);
76-
ma.call(this,oobj,prefix);
77-
};
13+
var ma = mongoose.Schema.prototype.add;
14+
if (mongoose.Schema.protection.localized) return;
15+
mongoose.Schema.protection.localized=()=>true;
16+
17+
//traverse over all fields and modify the objects having "localize" attribute
18+
var addI18n=function(schema,obj,prefix){
19+
var keys = Object.keys(obj);
20+
21+
if (keys.length==1 && keys=='_id') return obj;
22+
23+
var ret={};
24+
25+
for (var i = 0; i < keys.length; i++) {
26+
//keys of fields
27+
var key = keys[i];
28+
var field = obj[key];
29+
30+
if (key==='type'){
31+
ret[key]=field;
32+
continue;
33+
}
34+
35+
if (typeof field != "object") {
36+
ret[key]=field;
37+
continue;
38+
};
39+
40+
if (field instanceof Array) {
41+
//TODO: Should traverse? Than a problem with virtual name path
42+
ret[key]=field.slice();
43+
continue;
44+
};
45+
46+
//now we are sure "field" is an object
47+
//field=addI18n(schema,field,path+(path==''?'':'.')+key);
48+
49+
if (field.localize){
50+
if ((locales instanceof Array) && locales.length>0){
51+
var nfield={};
52+
for (var li=0;li<locales.length;li++){
53+
//TODO: remove ES6
54+
nfield[locales[li]]=Object.assign({},field);
55+
delete(nfield[locales[li]].localize);
56+
}
57+
ret[key]=nfield;
58+
var vpath=(prefix?prefix:'')+key;
59+
schema.virtual(vpath+'._')
60+
.get(function(){
61+
var l=currentLocale();
62+
if (l) return this.get(vpath+'.'+l);
63+
return undefined;
64+
}).set(function(value,virtual){
65+
if (currentLocale(value))
66+
this.set((prefix?prefix:'')+key+'.'+l,value);
67+
});
68+
}else{
69+
ret[key]=Object.assign({},field);
70+
delete(ret[key].localize);
71+
}
72+
}else{
73+
ret[key]=field;
74+
}
75+
};
76+
return ret;
77+
}
78+
79+
mongoose.Schema.prototype.add = function add (obj, prefix) {
80+
console.log({in:obj});
81+
var oobj=addI18n(this,obj,prefix);
82+
console.log({out:oobj});
83+
ma.call(this,oobj,prefix);
84+
};
7885
};
7986

8087
prototype_mongoose();
8188

89+
8290
module.exports = {
83-
locale:function(){
84-
return locale;
85-
},
86-
setLocale:function(sLocale){
87-
locale=sLocale;
88-
},
89-
locales:function(){
90-
return locales;
91-
},
92-
setLocales:function(sLocales){
93-
locales=sLocales;
94-
}
91+
currentLocale:currentLocale,
92+
setCurrentLocale:function(sLocale){
93+
locale=sLocale;
94+
},
95+
locales:function(){
96+
return locales;
97+
},
98+
setLocales:function(sLocales){
99+
locales=sLocales;
100+
},
101+
activate:prototype_mongoose;
102+
active:()=>!!mongoose.Schema.protection.localized
95103
}
104+
105+

package.json

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
22
"author": "S4Y Solution <e@s4y.solutions>",
3-
"contributors": ["Sergey Dolin <e@sergey.email>"],
3+
"contributors": [
4+
"Sergey Dolin <e@sergey.email>"
5+
],
46
"name": "mongoose-localize",
57
"description": "A module to conver mongoose model property to set of localized properties.",
6-
"version": "0.1.0",
7-
"keywords": ["mongoose","localize","l10n","i19n"],
8+
"version": "1.0.1",
9+
"keywords": [
10+
"mongoose",
11+
"localize",
12+
"translation",
13+
"l10n",
14+
"i19n"
15+
],
816
"license": "ISC",
917
"repository": {
1018
"url": "git://github.com/dsame/mongoose-localize.git"
@@ -14,6 +22,13 @@
1422
"node": "*"
1523
},
1624
"scripts": {
17-
"test": "mocha test"
18-
}
25+
"test": "mocha test"
26+
},
27+
"devDependencies": {
28+
"chai": "3.5.0",
29+
"mocha": "3.1.2"
30+
},
31+
"dependencies": {
32+
"mongoose": "4.6.5"
33+
}
1934
}

0 commit comments

Comments
 (0)