-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslate.js
72 lines (67 loc) · 2.56 KB
/
translate.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
/**
* Utility function that gets all elements that have a
* translate="" atttribute, uses it as stringbundle key,
* gets the corresponding string from the |stringbundle|,
* and sets the element content textnode to that string.
*
* Use it like this:
* JS: var gFooBundle = new StringBundle("email/foo.properties");
* translateElements(document, gFooBundle, {"brand": brandName });
* HTML: <a translate="resetpassword.label"
* translate-attr="title=resetpassword.tooltip" />
* takes the stringbundle value for "password.label", replaces placeholder
* %brand% with brandName, and inserts a text node as child of the <label>.
* Similarly, translate-attr would takes the stringbundle text for
* "resetpassword" and set it as value for attribute tooltip, e.g. ends up as
* <a title="Get a new password by email">Reset password</a>
*
* @param container {DOMElement} Iterators over this element
* @param stringbundle {StringBundle}
* @param brand {String} brand to use for substitution in strings
*/
function translateElements(container, stringbundle, placeholders) {
[].forEach.call(container.querySelectorAll("*[translate]"), function(el) {
var label = stringbundle.get(el.getAttribute("translate"));
for (var placeholder in placeholders) {
label = label.replace("%" + placeholder + "%", placeholders[placeholder]);
}
el.appendChild(document.createTextNode(label));
});
[].forEach.call(container.querySelectorAll("*[translate-attr]"), function(el) {
el.getAttribute("translate-attr").split(",").forEach(function(attrNameValue) {
var attrSplit = attrNameValue.split("=");
var attrName = attrSplit[0].trim();
var attrValue = attrSplit[1].trim();
var label = stringbundle.get(attrValue);
for (var placeholder in placeholders) {
label = label.replace("%" + placeholder + "%", placeholders[placeholder]);
}
el.setAttribute(attrName, label);
});
});
}
/**
* 3-way plural form for 0, 1 and >1. Picks the corresponding UI string.
* Also replaces %COUNT% with the number.
*
* @param count {Integer}
* @param str {String} a;b;c
* @return {String}
* if count = 0, use a
* if count = 1, use b
* if count > 1, use c
*/
function pluralform(count, str) {
var sp = str.split(";");
StringBundleUtils.assert(sp.length == 3, "pluralform: expected 3 parts in str: " + str);
var index;
if (count == 0)
index = 0;
else if (count == 1)
index = 1;
else
index = 2;
return sp[index].replace("%COUNT%", count);
}
exports.translateElements = translateElements;
exports.pluralform = pluralform;