-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
269 lines (179 loc) · 5.82 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var _ = require('lodash');
var async = require('async');
var util = require("util");
var u = require("./lib/utils");
module.exports = {
improve: 'apostrophe-docs',
afterConstruct:function(self){
self.apos.app.use(self.localizedHelper);
self.apos.app.use(self.localizedSessionToLocale);
self.apos.app.use(self.localizedGet);
},
construct:function(self,options){
self.defaultLocale = options.default || "en";
self.locales = options.locales;
self.localized = [ 'title' ].concat(options.localized || []);
self.localizedHelper=function(req, res, next) {
self.addHelpers({
localePicker:function(args){
var locales = [];
var availableLanguages = _.keys(locales);
var urls = require('url');
var parsed = urls.parse(req.url, true);
delete parsed.search;
delete parsed.query.apos_refresh;
var currentUrl = urls.format(parsed);
if (args && args.localized && args._edit == undefined) {
availableLanguages = _.keys(args.localized);
}
_.each(options.locales, function (label, locale) {
var newUrl = '/' + locale + currentUrl;
/**
* We don't want to include a locale
* slug for defaultLocale
*/
if(locale == self.defaultLocale){
newUrl = currentUrl;
}
var localeObject = {
key: locale,
label: label,
url: newUrl,
translated: (_.indexOf(availableLanguages, locale) >=0) ,
active: (req.locale === locale)
};
locales.push(localeObject);
});
return self.partial('localePicker', {locales: locales, args: args});
}
});
return next();
};
self.localizedSessionToLocale=function(req, res, next) {
if(req.session.locale){
req.locale = req.session.locale;
}else{
req.locale = options.default;
}
req.data.activeLocale = req.locale;
return next();
};
self.localizedGet=function(req, res, next) {
if (req.method !== 'GET') {
return next();
}
var matches = req.url.match(/^\/(\w+)(\/.*|\?.*|)$/);
if (!matches) {
//do not keep the session locale here
req.locale = self.defaultLocale;
req.session.locale = req.locale;
return next();
}
if (!_.has(options.locales, matches[1])) {
req.locale = self.defaultLocale;
req.session.locale = req.locale;
return next();
}
req.locale = matches[1];
req.session.locale = req.locale;
req.url = matches[2];
if (!req.url.length) {
req.url = "/"
}
return next();
};
self.docBeforeSave = function(req, doc, options) {
//TODO check why req.locale is always en
// using req.session.locale as a fallback
var locale = req.locale;
if(req.session && req.session.locale){
locale = req.session.locale;
}
if(!locale){
locale = self.defaultLocale;
}
if(!locale){
return;
}
u.ensureProperties(doc,{
"defaultLocale": self.defaultLocale,
"locale": locale,
"locales":options.locales
});
var before = JSON.stringify(doc.localized[locale] || {});
_.each(doc, function(value, key) {
if (!u.isArea(value)) {
return;
}
if (u.isUniversal(doc, key,self.options)) {
return;
}
doc.localized[locale][key] = value;
// Revert .body to the default culture's body, unless
// that doesn't exist yet, in which case we let it spawn from
// the current culture
if (_.has(doc.localized[self.defaultLocale], key)) {
doc[key] = doc.localized[self.defaultLocale][key];
} else {
doc.localized[self.defaultLocale][key] = doc[key];
}
});
_.each(self.localized,function(name){
name = u.localizeForPage(doc,name);
if(!name){
return;
}
if(u.isArea(doc[name])){
return;
}
doc.localized[locale][name] = doc[name];
if (_.has(doc.localized[self.defaultLocale], name)) {
doc[name] = doc.localized[self.defaultLocale][name];
} else {
doc.localized[self.defaultLocale][name] = doc[name];
}
});
/**
* TODO This sometimes causes circular reference problems
* we need to check how else to do it
*/
var after = JSON.stringify(doc.localized[locale] || {});
if (before !== after) {
doc.localizedAt[locale] = new Date();
if (locale === self.default) {
doc.localizedStale = _.without(_.keys(self.locales), self.defaultLocale);
} else {
// modifies in place
_.pull(doc.localizedStale, locale);
}
}
};
/**
* Go over all schemas and set the _localized value so that
* we can augment the apostrophe-schema:macros in order
* to display a localization icon
*
*/
_.each(self.apos.modules,function(module){
var moduleName =
module.options.alias ?
module.options.alias : module.__meta.name;
if(module.schema){
_.each(module.schema,function(field){
if(
field.type == "area" ||
self.localized.indexOf(field.name) >= 0 ||
self.localized.indexOf(moduleName+":"+field.name) >=0 ){
field._localized = true;
}
})
}
});
// merge new methods with all apostrophe-cursors
self.apos.define('apostrophe-cursor', require('./lib/cursor.js')({
defaultLocale : self.defaultLocale,
locales : self.locales,
localized : self.localized
}));
}
};