Skip to content

Commit e3bd2ff

Browse files
author
Sergey Dolin
committed
v 1.0.4 adopt
1 parent 3c4a9c8 commit e3bd2ff

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ mongoose-localize
33

44
A nodejs module to convert mongoose model property to set of localized properties.
55

6-
As soon as mongoose_localize has been required...
6+
As soon as mongoose_localize has been required with CommonJS ...
77

88
```javascript
9+
var
10+
mongoose = require('mongoose'),
11+
localize = require('mongoose-localize');
12+
```
13+
14+
or imported and initialized with ES6 syntax
15+
```javascript
16+
import mongoose from 'mongoose';
917
import mongoose_localize from 'mongoose-localize';
10-
mongoose_localize.setLocales(['locale1','locale2',...,'localeN']);
18+
mongoose_localize.localize(mongoose,[currentLocale[,locales]]);
1119
```
1220

1321
...every attribute of mongoose Scheme containing "localize" attribute set to true...

index.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
'use strict';
22

3-
var mongoose = require('mongoose');
3+
//var mongoose = require('mongoose');
44

55
var locales=['en','ru'];
66
var locale=locales[0];
77

8+
function setLocales(sLocales){
9+
locales=sLocales;
10+
}
11+
812
function currentLocale(){
913
return locale?locale:((locales.length>0)?locales[0]:undefined);
1014
}
1115

12-
var prototype_mongoose=function(){
13-
var ma = mongoose.Schema.prototype.add;
14-
if (mongoose.Schema.prototype.localized) return;
15-
mongoose.Schema.prototype.localized=()=>true;
16+
function setCurrentLocale(sLocale){
17+
locale=sLocale;
18+
}
19+
20+
function isLocalized(){
21+
return true;
22+
}
23+
24+
var prototype_mongoose=function(mongoose_instance,_currentLocale,_locales){
25+
26+
function localizedAdd(obj, prefix) {
27+
//console.log({in:obj});
28+
var oobj=addI18n(this,obj,prefix);
29+
//console.log({out:oobj});
30+
ma.call(this,oobj,prefix);
31+
};
32+
33+
34+
if (_currentLocale) setCurrentLocale(_currentLocale);
35+
if (_locales) setLocales(_locales);
36+
if (!mongoose_instance)
37+
mongoose_instance=require('mongoose');
38+
39+
//if (mongoose_instance.Schema.prototype.add === localizedAdd){
40+
if (mongoose_instance.Schema.prototype.localized === isLocalized){
41+
//console.log('WARN: mongoose already has been localized');
42+
return;
43+
}else{
44+
//console.log('INFO: mongoose will be localized');
45+
};
46+
47+
var ma = mongoose_instance.Schema.prototype.add;
48+
if (mongoose_instance.Schema.prototype.localized) return;
1649

1750
//traverse over all fields and modify the objects having "localize" attribute
1851
var addI18n=function(schema,obj,prefix){
@@ -50,21 +83,21 @@ var prototype_mongoose=function(){
5083
if ((locales instanceof Array) && locales.length>0){
5184
var nfield={};
5285
for (var li=0;li<locales.length;li++){
53-
//TODO: remove ES6
86+
//TODO: remove ES6
5487
nfield[locales[li]]=Object.assign({},field);
5588
delete(nfield[locales[li]].localize);
5689
}
5790
ret[key]=nfield;
5891
var vpath=(prefix?prefix:'')+key;
5992
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-
});
93+
.get(function(){
94+
var l=currentLocale();
95+
if (l) return this.get(vpath+'.'+l);
96+
return undefined;
97+
}).set(function(value,virtual){
98+
if (currentLocale(value))
99+
this.set((prefix?prefix:'')+key+'.'+l,value);
100+
});
68101
}else{
69102
ret[key]=Object.assign({},field);
70103
delete(ret[key].localize);
@@ -76,30 +109,23 @@ var prototype_mongoose=function(){
76109
return ret;
77110
}
78111

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-
};
112+
mongoose_instance.Schema.prototype.add = localizedAdd;
113+
mongoose_instance.Schema.prototype.localized=isLocalized;
85114
};
86115

87-
prototype_mongoose();
116+
prototype_mongoose()
88117

89118

90119
module.exports = {
91120
currentLocale:currentLocale,
92-
setCurrentLocale:function(sLocale){
93-
locale=sLocale;
94-
},
121+
setCurrentLocale:setCurrentLocale,
95122
locales:function(){
96123
return locales;
97124
},
98-
setLocales:function(sLocales){
99-
locales=sLocales;
100-
},
101-
activate:prototype_mongoose,
102-
active:()=>!!mongoose.Schema.prototype.localized
125+
setLocales:setLocales,
126+
localize:prototype_mongoose,
127+
localized:function(){mongoose.Schema.prototype.localized==isLocalized}
103128
}
104129

105130

131+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
],
66
"name": "mongoose-localize",
77
"description": "A module to conver mongoose model property to set of localized properties.",
8-
"version": "1.0.3",
8+
"version": "1.0.4",
99
"keywords": [
1010
"mongoose",
1111
"localize",

0 commit comments

Comments
 (0)