1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var AbstractMethodError = require("../common/errors").AbstractMethodError;
|
11 | var AbstractClassError = require("../common/errors").AbstractClassError;
|
12 | var LangUtils = require('@themost/common/utils').LangUtils;
|
13 | var Args = require('@themost/common/utils').Args;
|
14 | var TraceUtils = require('@themost/common/utils').TraceUtils;
|
15 | var path = require('path');
|
16 | var HttpApplicationService = require('./types').HttpApplicationService;
|
17 | var _ = require('lodash');
|
18 | var Symbol = require('symbol');
|
19 | var sprintf = require('sprintf').sprintf;
|
20 | var i18n = require('i18n');
|
21 | var optionsProperty = Symbol('options');
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function LocalizationStrategy(app) {
|
30 | LocalizationStrategy.super_.bind(this)(app);
|
31 | if (this.constructor === LocalizationStrategy.prototype.constructor) {
|
32 | throw new AbstractClassError();
|
33 | }
|
34 | }
|
35 | LangUtils.inherits(LocalizationStrategy, HttpApplicationService);
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | LocalizationStrategy.prototype.getCultures = function() {
|
44 | throw new AbstractMethodError();
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | LocalizationStrategy.prototype.getDefaultCulture = function() {
|
54 | throw new AbstractMethodError();
|
55 | };
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | LocalizationStrategy.prototype.hasCulture = function(culture) {
|
63 | Args.notString(culture,'Culture');
|
64 | return typeof _.find(this.getCultures(), function(x) {
|
65 | return x===culture;
|
66 | }) === 'string';
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | LocalizationStrategy.prototype.getLocaleString = function(locale, str) {
|
78 | throw new AbstractMethodError();
|
79 | };
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | LocalizationStrategy.prototype.setLocaleString = function(locale, data, shouldMerge) {
|
89 | throw new AbstractMethodError();
|
90 | };
|
91 |
|
92 | var culturesProperty = Symbol('cultures');
|
93 | var defaultCultureProperty = Symbol('defaultCulture');
|
94 | var librariesProperty = Symbol('libraries');
|
95 | var defaultLibProperty = "global";
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function DefaultLocalizationStrategy(app) {
|
104 | DefaultLocalizationStrategy.super_.bind(this)(app);
|
105 | var configuration = this.getApplication().getConfiguration();
|
106 | this[culturesProperty] = ['en-us'];
|
107 | this[defaultCultureProperty] = 'en-us';
|
108 | this[librariesProperty] = {};
|
109 | if (configuration.settings) {
|
110 | if (configuration.settings && configuration.settings.hasOwnProperty('localization')) {
|
111 | |
112 |
|
113 |
|
114 | var localization = configuration.settings['localization'];
|
115 | if (_.isArray(localization.cultures))
|
116 | this[culturesProperty] = localization.cultures;
|
117 | if (_.isString(localization.default))
|
118 | this[defaultCultureProperty] = localization.default;
|
119 | }
|
120 | }
|
121 |
|
122 | }
|
123 | LangUtils.inherits(DefaultLocalizationStrategy, LocalizationStrategy);
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | DefaultLocalizationStrategy.prototype.getCultures = function() {
|
131 | return this[culturesProperty];
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | DefaultLocalizationStrategy.prototype.getDefaultCulture = function() {
|
141 | return this[defaultCultureProperty];
|
142 | };
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | DefaultLocalizationStrategy.prototype.resolveLocalePath = function(locale) {
|
148 | return path.resolve(this.getApplication().getExecutionPath(),_.template('locales/global.${locale}.json')({ locale:locale }));
|
149 | };
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | DefaultLocalizationStrategy.prototype.getLocaleString = function(locale, str) {
|
160 | var lib = 'global';
|
161 | var libraries = this[librariesProperty];
|
162 | var locLibrary;
|
163 | var locText;
|
164 | var args;
|
165 | if (libraries.hasOwnProperty(lib)) {
|
166 | locLibrary = libraries[lib];
|
167 | if (locLibrary.hasOwnProperty(locale)) {
|
168 |
|
169 | locText = locLibrary[locale][str] || str;
|
170 |
|
171 | args = Array.prototype.slice.call(arguments, 1);
|
172 |
|
173 | args[0] = locText;
|
174 |
|
175 | return sprintf.apply(sprintf, args);
|
176 | }
|
177 | }
|
178 | var libraryFile;
|
179 | try {
|
180 | libraryFile = this.resolveLocalePath(locale);
|
181 | }
|
182 | catch(err) {
|
183 | if (err.code === 'ENOENT' || err.code === 'MODULE_NOT_FOUND') {
|
184 | TraceUtils.debug('Cannot find localization module' + libraryFile + '.');
|
185 | return str;
|
186 | }
|
187 | throw err;
|
188 | }
|
189 | try {
|
190 |
|
191 | if (libraries.hasOwnProperty(lib))
|
192 | locLibrary = libraries[lib];
|
193 | else
|
194 | locLibrary = libraries[lib] = { };
|
195 | locLibrary[locale] = require(libraryFile);
|
196 |
|
197 | locText = locLibrary[locale][str] || str;
|
198 |
|
199 | args = Array.prototype.slice.call(arguments, 1);
|
200 |
|
201 | args[0] = locText;
|
202 |
|
203 | return sprintf.apply(sprintf, args);
|
204 | }
|
205 | catch (err) {
|
206 | if (err.code === 'ENOENT' || err.code === 'MODULE_NOT_FOUND') {
|
207 | TraceUtils.debug('Cannot find localization module' + libraryFile + '.');
|
208 | return str;
|
209 | }
|
210 | throw err;
|
211 | }
|
212 | };
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | DefaultLocalizationStrategy.prototype.setLocaleString = function(locale, data, shouldMerge) {
|
222 | var libraries = this[librariesProperty];
|
223 |
|
224 | if (this.getCultures().indexOf(locale)<0) {
|
225 | throw new Error('Invalid locale. The specified locale does not exist in application locales.');
|
226 | }
|
227 |
|
228 | libraries[defaultLibProperty] = libraries[defaultLibProperty] || {};
|
229 | libraries[defaultLibProperty][locale] = libraries[defaultLibProperty][locale] || {};
|
230 | if (typeof shouldMerge === 'undefined' || shouldMerge) {
|
231 | _.assign(libraries[defaultLibProperty][locale], data);
|
232 | }
|
233 | else {
|
234 | libraries[defaultLibProperty][locale] = data;
|
235 | }
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | function I18nLocalizationStrategy(app) {
|
245 | I18nLocalizationStrategy.super_.bind(this)(app);
|
246 |
|
247 | var configuration = this.getApplication().getConfiguration();
|
248 |
|
249 | var options = _.assign({
|
250 | "locales": [ "en" ],
|
251 | "defaultLocale": "en",
|
252 | "directory": path.resolve(this.getApplication().getExecutionPath(),'i18n'),
|
253 | "autoReload": false
|
254 | }, configuration.getSourceAt('settings/i18n'));
|
255 |
|
256 | this[culturesProperty] = options.locales;
|
257 | this[defaultCultureProperty] = options.defaultLocale;
|
258 | i18n.configure(options);
|
259 | }
|
260 | LangUtils.inherits(I18nLocalizationStrategy, DefaultLocalizationStrategy);
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | I18nLocalizationStrategy.prototype.getLocaleString = function(locale, str) {
|
271 | return i18n.__.apply({
|
272 | locale: locale
|
273 | }, Array.prototype.slice.call(arguments, 1));
|
274 | };
|
275 |
|
276 | I18nLocalizationStrategy.prototype.resolveLocalePath = function(locale) {
|
277 | if (typeof locale !== 'string') {
|
278 | throw new Error('Invalid locale parameter. Expected string.');
|
279 | }
|
280 | return path.resolve(this.getApplication().getExecutionPath(),_.template('i18n/${locale}.json')({ locale:locale.substr(0,2) }));
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | I18nLocalizationStrategy.prototype.getCultures = function() {
|
287 | return this[culturesProperty];
|
288 | };
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | I18nLocalizationStrategy.prototype.getDefaultCulture = function() {
|
297 | return this[defaultCultureProperty];
|
298 | };
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | I18nLocalizationStrategy.prototype.setLocaleString = function(locale, data, shouldMerge) {
|
308 |
|
309 | var catalog = i18n.getCatalog(locale);
|
310 |
|
311 | if (typeof shouldMerge === 'undefined' || shouldMerge) {
|
312 | _.assign(catalog, data);
|
313 | }
|
314 | else {
|
315 | _.assign(catalog, data);
|
316 | }
|
317 | };
|
318 |
|
319 | if (typeof exports !== 'undefined')
|
320 | {
|
321 | module.exports.LocalizationStrategy = LocalizationStrategy;
|
322 | module.exports.DefaultLocalizationStrategy = DefaultLocalizationStrategy;
|
323 | module.exports.I18nLocalizationStrategy = I18nLocalizationStrategy;
|
324 | }
|