-
Notifications
You must be signed in to change notification settings - Fork 8
/
puttext.js
111 lines (93 loc) · 3.47 KB
/
puttext.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
/*
(c) 2013 Instant Communication Ltd.
under terms of ISC License
*/
(function(define) {
return define(function() {
var pluralRe = /^Plural-Forms:\s*nplurals\s*=\s*(\d+);\s*plural\s*=\s*([^a-zA-Z0-9\$]*([a-zA-Z0-9\$]+).+)$/im;
function format(s, ctx) {
return s.replace(/(^|.)?\{([^\}]+)\}/g, function(match, prev, k) {
if (prev === '\\') {
return '{' + k + '}';
}
prev = prev || ""; // should be empty string if something like `undefined`
return prev + ctx[k.split('#')[0].trim()];
});
}
function parsePlural(header) {
var rv = {
pluralNum: 2,
isPlural: function(n) {
return n !== 1;
}
};
if (!header) {
return rv;
}
var match = header.match(pluralRe);
if (!match) {
return rv;
}
rv.pluralNum = parseInt(match[1], 10);
if (rv.pluralNum == 1) {
rv.isPlural = function () {return 0;};
return rv;
}
var expr = match[2];
var varName = match[3];
var code = "(function (" + varName + ") { return " + expr + " })";
try {
rv.isPlural = eval(code);
} catch (e) {
console.log("Could not evaluate: " + code);
}
return rv;
}
function gettrans(messages, isPlural, msg1, msg2, num) {
if (!messages || !messages[msg1]) {
return num !== undefined && isPlural(num) ? msg2 : msg1;
}
var trans = messages[msg1];
if (msg2 === undefined && num === undefined) {
return typeof trans === 'string' ? trans : trans[0];
}
if (num !== undefined && typeof trans === 'string') {
throw format('Plural number ({num}) provided for "{msg1}", ' +
'but only singular translation exists: {trans}',
{num: num, msg1: msg1, trans: trans});
}
return trans[+isPlural(num)];
}
return function(messages) {
function puttext(msg1, msg2, num, ctx) {
// in case of `puttext(msg, ctx);`
if (typeof msg2 === 'object' && num === undefined &&
ctx === undefined) {
ctx = msg2;
msg2 = undefined;
}
var text = gettrans(puttext.messages, puttext.plural,
msg1, msg2, num);
if (ctx) {
return format(text, ctx);
}
return text;
}
puttext.format = format;
puttext.setMessages = function(messages) {
puttext.messages = messages;
var parsed = parsePlural(messages && messages[""]);
puttext.pluralNum = parsed.pluralNum;
puttext.plural = parsed.isPlural;
};
puttext.setMessages(messages);
return puttext;
};
});
})(typeof define !== 'undefined' ? define : function(factory) {
if (typeof module !== 'undefined' && typeof exports !== 'undefined') {
return module.exports = factory();
} else {
return window.puttext = factory();
}
});