-
Notifications
You must be signed in to change notification settings - Fork 1
/
i18n.js
42 lines (34 loc) · 1.14 KB
/
i18n.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
'use strict';
var I18n = require('./lib/i18n');
var magico = require('magico');
// Overwrite the original interpolate function
// This function interpolates the all variables in the given message.
// Enhanced with magico support
I18n.interpolate = function(message, options) {
options = this.prepareOptions(options);
var matches = message.match(this.placeholder)
, placeholder
, value
, name
, regex;
if (!matches) {
return message;
}
while (matches.length) {
placeholder = matches.shift();
name = placeholder.replace(this.placeholder, '$1');
// get value by name: support dot path like -> `person.gender`
value = magico.get(options, name);
if (this.isSet(value)) {
value = value.toString().replace(/\$/gm, '_#$#_');
} else if (name in options) {
value = this.nullPlaceholder(placeholder, message, options);
} else {
value = this.missingPlaceholder(placeholder, message, options);
}
regex = new RegExp(placeholder.replace(/\{/gm, '\\{').replace(/\}/gm, '\\}'));
message = message.replace(regex, value);
}
return message.replace(/_#\$#_/g, '$');
};
module.exports = I18n;