-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (120 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
//var mongoose = require('mongoose');
var locales=['en','ru'];
var locale=locales[0];
function setLocales(sLocales){
locales=sLocales;
}
function currentLocale(){
return locale?locale:((locales.length>0)?locales[0]:undefined);
}
function setCurrentLocale(sLocale){
if (locales.indexOf(sLocale)>=0)
locale=sLocale;
else{
let i=sLocale.indexOf('-');
if (i>0){
return setCurrentLocale(sLocale.substring(0,i));
}else{
i=sLocale.indexOf('_');
if (i>0){
return setCurrentLocale(sLocale.substring(0,i));
}else{
if (locales.length>0){
setCurrentLocale(locales[0]);
}
}
}
}
}
function isLocalized(){
return true;
}
var prototype_mongoose=function(mongoose_instance,_currentLocale,_locales){
function localizedAdd(obj, prefix) {
//console.log({in:obj});
var oobj=addI18n(this,obj,prefix);
//console.log({out:oobj});
ma.call(this,oobj,prefix);
};
if (_currentLocale) setCurrentLocale(_currentLocale);
if (_locales) setLocales(_locales);
if (!mongoose_instance)
mongoose_instance=require('mongoose');
//if (mongoose_instance.Schema.prototype.add === localizedAdd){
if (mongoose_instance.Schema.prototype.localized === isLocalized){
//console.log('WARN: mongoose already has been localized');
return;
}else{
//console.log('INFO: mongoose will be localized');
};
var ma = mongoose_instance.Schema.prototype.add;
if (mongoose_instance.Schema.prototype.localized) return;
//traverse over all fields and modify the objects having "localize" attribute
var addI18n=function(schema,obj,prefix){
var keys = Object.keys(obj);
if (keys.length==1 && keys=='_id') return obj;
var ret={};
for (var i = 0; i < keys.length; i++) {
//keys of fields
var key = keys[i];
var field = obj[key];
if (key==='type'){
ret[key]=field;
continue;
}
if (typeof field != "object") {
ret[key]=field;
continue;
};
if (field instanceof Array) {
//TODO: Should traverse? Than a problem with virtual name path
ret[key]=field.slice();
continue;
};
//now we are sure "field" is an object
//field=addI18n(schema,field,path+(path==''?'':'.')+key);
if (field.localize){
if ((locales instanceof Array) && locales.length>0){
var nfield={};
for (var li=0;li<locales.length;li++){
//TODO: remove ES6
nfield[locales[li]]=Object.assign({},field);
delete(nfield[locales[li]].localize);
}
ret[key]=nfield;
var vpath=(prefix?prefix:'')+key;
schema.virtual(vpath+'._')
.get(function(){
var l=currentLocale();
if (l) return this.get(vpath+'.'+l);
return undefined;
}).set(function(value,virtual){
var l=currentLocale();
if (l)
this.set(vpath+'.'+l,value);
});
}else{
ret[key]=Object.assign({},field);
delete(ret[key].localize);
}
}else{
ret[key]=field;
}
};
return ret;
}
mongoose_instance.Schema.prototype.add = localizedAdd;
mongoose_instance.Schema.prototype.localized=isLocalized;
};
prototype_mongoose()
module.exports = {
currentLocale:currentLocale,
setCurrentLocale:setCurrentLocale,
locales:function(){
return locales;
},
setLocales:setLocales,
localize:prototype_mongoose,
localized:function(){mongoose.Schema.prototype.localized==isLocalized}
}