1
1
'use strict' ;
2
2
3
- var mongoose = require ( 'mongoose' ) ;
3
+ // var mongoose = require('mongoose');
4
4
5
5
var locales = [ 'en' , 'ru' ] ;
6
6
var locale = locales [ 0 ] ;
7
7
8
+ function setLocales ( sLocales ) {
9
+ locales = sLocales ;
10
+ }
11
+
8
12
function currentLocale ( ) {
9
13
return locale ?locale :( ( locales . length > 0 ) ?locales [ 0 ] :undefined ) ;
10
14
}
11
15
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 ;
16
49
17
50
//traverse over all fields and modify the objects having "localize" attribute
18
51
var addI18n = function ( schema , obj , prefix ) {
@@ -50,21 +83,21 @@ var prototype_mongoose=function(){
50
83
if ( ( locales instanceof Array ) && locales . length > 0 ) {
51
84
var nfield = { } ;
52
85
for ( var li = 0 ; li < locales . length ; li ++ ) {
53
- //TODO: remove ES6
86
+ //TODO: remove ES6
54
87
nfield [ locales [ li ] ] = Object . assign ( { } , field ) ;
55
88
delete ( nfield [ locales [ li ] ] . localize ) ;
56
89
}
57
90
ret [ key ] = nfield ;
58
91
var vpath = ( prefix ?prefix :'' ) + key ;
59
92
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
+ } ) ;
68
101
} else {
69
102
ret [ key ] = Object . assign ( { } , field ) ;
70
103
delete ( ret [ key ] . localize ) ;
@@ -76,30 +109,23 @@ var prototype_mongoose=function(){
76
109
return ret ;
77
110
}
78
111
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 ;
85
114
} ;
86
115
87
- prototype_mongoose ( ) ;
116
+ prototype_mongoose ( )
88
117
89
118
90
119
module . exports = {
91
120
currentLocale :currentLocale ,
92
- setCurrentLocale :function ( sLocale ) {
93
- locale = sLocale ;
94
- } ,
121
+ setCurrentLocale :setCurrentLocale ,
95
122
locales :function ( ) {
96
123
return locales ;
97
124
} ,
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 }
103
128
}
104
129
105
130
131
+
0 commit comments