index.js
7.76 KB
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
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: strong-globalize
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
// Single-instance strong-globalize
// module.exports = require('./lib/globalize');
// Multi-instance strong-globalize
var globalize = require('./lib/globalize');
var helper = require('./lib/helper');
var path = require('path');
var translate = require('./lib/translate');
var xtend = require('xtend');
exports = module.exports = StrongGlobalize;
exports.SetRootDir = SetRootDir;
exports.SetDefaultLanguage = globalize.setDefaultLanguage;
exports.SetPersistentLogging = globalize.setPersistentLogging;
function StrongGlobalize(options) {
if (!(this instanceof StrongGlobalize)) {
return new StrongGlobalize(options);
}
if (typeof options === 'string') {
exports.SetRootDir(options);
options = undefined;
}
if (!global.STRONGLOOP_GLB) globalize.setDefaultLanguage();
var defaults = {
language: global.STRONGLOOP_GLB.DEFAULT_LANG,
};
this._options = options ? xtend(defaults, options) : defaults;
}
function SetRootDir(rootDir, options) {
var defaults = {
autonomousMsgLoading: helper.AML_DEFAULT,
};
options = options ? xtend(defaults, options) : defaults;
options.autonomousMsgLoading =
helper.validateAmlValue(options.autonomousMsgLoading);
if (!options.autonomousMsgLoading) {
options.autonomousMsgLoading = defaults.autonomousMsgLoading;
}
globalize.setRootDir(rootDir);
if (!global.STRONGLOOP_GLB) {
globalize.setDefaultLanguage();
global.STRONGLOOP_GLB.AUTO_MSG_LOADING = options.autonomousMsgLoading;
}
if (path.resolve(rootDir) !==
path.resolve(global.STRONGLOOP_GLB.MASTER_ROOT_DIR) &&
helper.isLoadMessages(rootDir)) {
var langs = Object.keys(global.STRONGLOOP_GLB.bundles);
langs.forEach(function(lang) {
translate.loadMsgFromFile(lang, rootDir);
});
}
}
StrongGlobalize.prototype.setLanguage = function(lang) {
lang = helper.isSupportedLanguage(lang) ?
lang : global.STRONGLOOP_GLB.DEFAULT_LANG;
this._options.language = lang;
};
StrongGlobalize.prototype.getLanguage = function() {
return this._options.language;
};
StrongGlobalize.prototype.c = function(value, currencySymbol, options) {
globalize.loadGlobalize(this._options.language);
return globalize.formatCurrency(value, currencySymbol, options,
this._options.language);
};
StrongGlobalize.prototype.formatCurrency = StrongGlobalize.prototype.c;
StrongGlobalize.prototype.d = function(value, options) {
globalize.loadGlobalize(this._options.language);
return globalize.formatDate(value, options, this._options.language);
};
StrongGlobalize.prototype.formatDate = StrongGlobalize.prototype.d;
StrongGlobalize.prototype.n = function(value, options) {
globalize.loadGlobalize(this._options.language);
return globalize.formatNumber(value, options, this._options.language);
};
StrongGlobalize.prototype.formatNumber = StrongGlobalize.prototype.n;
StrongGlobalize.prototype.m = function(path, variables) {
globalize.loadGlobalize(this._options.language);
return globalize.formatMessage(path, variables, this._options.language);
};
StrongGlobalize.prototype.formatMessage = StrongGlobalize.prototype.m;
StrongGlobalize.prototype.t = StrongGlobalize.prototype.m;
StrongGlobalize.prototype.Error = function() {
globalize.loadGlobalize(this._options.language);
var msg = globalize.packMessage(arguments, null, true,
this._options.language);
globalize.logPersistent('error', msg);
return Error(msg.message);
};
StrongGlobalize.prototype.f = function() {
globalize.loadGlobalize(this._options.language);
return globalize.packMessage(arguments, null,
false, this._options.language);
};
StrongGlobalize.prototype.format = StrongGlobalize.prototype.f;
StrongGlobalize.prototype.ewrite = function() {
globalize.loadGlobalize(this._options.language);
return globalize.packMessage(arguments, function(msg) {
globalize.logPersistent(msg, 'error');
if (globalize.consoleEnabled()) process.stderr.write(msg.message);
}, true, this._options.language);
};
StrongGlobalize.prototype.owrite = function() {
globalize.loadGlobalize(this._options.language);
return globalize.packMessage(arguments, function(msg) {
globalize.logPersistent(msg, 'error');
if (globalize.consoleEnabled()) process.stdout.write(msg.message);
}, true, this._options.language);
};
StrongGlobalize.prototype.write = StrongGlobalize.prototype.owrite;
// RFC 5424 Syslog Message Severities
StrongGlobalize.prototype.emergency = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('emergency', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.alert = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('alert', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.critical = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('critical', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.error = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('error', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.warning = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('warning', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.notice = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('notice', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.informational = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('informational', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.debug = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('debug', arguments, console.log,
this._options.language);
};
// Node.js console
StrongGlobalize.prototype.warn = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('warn', arguments, console.error,
this._options.language);
};
StrongGlobalize.prototype.info = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('info', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.log = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('log', arguments, console.log,
this._options.language);
};
// Misc Logging Levels
StrongGlobalize.prototype.help = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('help', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.data = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('data', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.prompt = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('prompt', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.verbose = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('verbose', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.input = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('input', arguments, console.log,
this._options.language);
};
StrongGlobalize.prototype.silly = function() {
globalize.loadGlobalize(this._options.language);
return globalize.rfc5424('silly', arguments, console.log,
this._options.language);
};