forked from rusty1s/mongoose-i18n-localize
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
168 lines (151 loc) · 4.91 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* jshint node: true */
'use strict';
function forIn(obj, callb) {
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
var e = obj[k];
callb(e, k);
}
}
}
function ArrNoDupe(a) {
var temp = {},
r = [],
i = 0,
k;
for (i; i < a.length; i++) {
temp[a[i]] = true;
}
forIn(temp, function (v, k) {
r.push(k);
});
return r;
}
function addLocales(prePath, pathname, schema, options_locales) {
var instance = schema.paths[pathname].instance,
config = {}
;
forIn(schema.paths[pathname].options, function (el, key) {
config[key] = el;
})
if (config.i18n && typeof instance === 'string' && instance.match(/^(String|Number|Boolean|Date)$/i)) {
delete(config.i18n);
config._i18n = true;
schema.remove(pathname);
options_locales.forEach(function (locale) {
schema.path(pathname + '.' + locale, config);
});
}
}
function recursiveIteration(prePath, schema, options_locales) {
forIn(schema.paths, function (schemaField, schemaPath) {
if (schemaField.schema) {
recursiveIteration(prePath + (prePath && '.') + schemaPath, schemaField.schema, options_locales);
} else {
addLocales(prePath, schemaField.path, schema, options_locales);
}
});
}
function getI18nCapsulePaths(prePath, schema) {
var i18nPathCapsules = [],
i18nCapsulePathMask = /^(.*)\.[^\.]*$/
;
forIn(schema.paths, function (schemaField, schemaPath) {
if (schema.childSchemas.find(function (modSch) {
return modSch.schema === schemaField.schema;
})) {
i18nPathCapsules = i18nPathCapsules.concat(getI18nCapsulePaths(prePath + (prePath && '.') + schemaPath, schemaField.schema));
} else if (schemaField.options._i18n) {
var i18nCapsulePath = (prePath + (prePath && '.') + schemaPath).replace(i18nCapsulePathMask, '$1');
if (!~i18nPathCapsules.indexOf(i18nCapsulePath)) {
i18nPathCapsules.push(i18nCapsulePath);
}
}
});
return i18nPathCapsules;
}
function localyzeRecursive(obj, i18nCapsulePathArr, o) {
var thisSubObjPath = i18nCapsulePathArr[0],
thisSubObj = obj && obj[thisSubObjPath],
val, defVal
;
if (obj && i18nCapsulePathArr.length === 1) {
if (obj[thisSubObjPath]) {
if (o.locale) { val = obj[thisSubObjPath][o.locale]; }
defVal = obj[thisSubObjPath][o.defaultLocale];
val = (typeof val !== 'undefined') ? val : defVal;
if (o.only) {
obj[thisSubObjPath] = val;
} else {
obj[thisSubObjPath].localized = val;
}
}
} else if (thisSubObj && i18nCapsulePathArr.length > 1) {
if (thisSubObj instanceof Array) {
thisSubObj.map(function (i) {
localyzeRecursive(i, i18nCapsulePathArr.slice(1), o);
});
} else {
localyzeRecursive(thisSubObj, i18nCapsulePathArr.slice(1), o);
}
}
}
function addLocalized(o) {
var _obj = o.toJSON ? o.obj.toJSON(o.params) : o.obj.toObject(o.params),
val, defVal, _i18n_paths = [];
_i18n_paths = getI18nCapsulePaths('', o.obj.schema);
_i18n_paths.forEach(function (i18nCapsulePath) {
var i18nCapsulePathArr = i18nCapsulePath.split('.');
localyzeRecursive(_obj, i18nCapsulePathArr, o);
});
return _obj;
}
function guessMorphAndApply(_this, args, extra) {
var o = {}, target, options_defaultLocale = extra[3];
if (typeof args[0] === 'string') {
o.locale = args[0];
} else if (args[0] && args[0].hasOwnProperty('isNew')) {
target = args[0];
}
if (!o.locale && typeof args[1] === 'string') {
o.locale = args[1];
} else if (o.locale && typeof args[1] === 'string') {
o.defaultLocale = args[1];
}
if (args.length && typeof args[args.length-1] === 'object' && !args[args.length-1].hasOwnProperty('isNew')) {
o.params = args[args.length-1];
}
if (target && typeof args[2] === 'string') { o.defaultLocale = args[2]; }
if (!target && _this.hasOwnProperty('isNew')) { target = _this; }
if (!o.defaultLocale) { o.defaultLocale = options_defaultLocale; }
if (!o.locale) { o.locale = options_defaultLocale; }
o.toJSON = extra[0];
o.only = extra[1];
o.obj = target;
return addLocalized(o);
}
function mongooseI18nLocalyse(schema, options) {
options = options || {};
var options_locales = ArrNoDupe(options.locales || []),
options_defaultLocale = options.defaultLocale
;
if (options_locales.length > 0) {
recursiveIteration('', schema, options_locales);
if (!~options_locales.indexOf(options_defaultLocale)) {
options_defaultLocale = options_locales[0];
}
}
schema.methods.toJSONLocalized = function () {
return guessMorphAndApply(this, arguments, [true, false, 'toJSONLocalized', options_defaultLocale]);
};
schema.methods.toObjectLocalized = function () {
return guessMorphAndApply(this, arguments, [false, false, 'toObjectLocalized', options_defaultLocale]);
};
schema.methods.toJSONLocalizedOnly = function () {
return guessMorphAndApply(this, arguments, [true, true, 'toJSONLocalizedOnly', options_defaultLocale]);
};
schema.methods.toObjectLocalizedOnly = function () {
return guessMorphAndApply(this, arguments, [false, true, 'toObjectLocalizedOnly', options_defaultLocale]);
};
}
module.exports = mongooseI18nLocalyse;