-
Notifications
You must be signed in to change notification settings - Fork 8
/
l10n.js
105 lines (100 loc) · 3.92 KB
/
l10n.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
/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
var l10n = l10n || {};
/**
* Localize a tag, returning the tag itself and logging an error if no
* translation exists.
*
* @param {string} tag The localization tag.
* @param {(string|Array)=} opt_substitutions An optional set of substitution
* strings corresponding to the "placeholders" attributes in messages.json.
* @return {string} The translated tag.
*/
l10n.getTranslationOrError = function(tag, opt_substitutions) {
var translation = chrome.i18n.getMessage(tag, opt_substitutions);
if (translation) {
return translation;
}
console.error('Missing translation for "' + tag + '"');
return tag;
};
/**
* Localize an element by setting its innerText according to the specified tag
* and an optional set of substitutions.
*
* @param {Element} element The element to localize.
* @param {string} tag The localization tag.
* @param {(string|Array)=} opt_substitutions An optional set of substitution
* strings corresponding to the "placeholders" attributes in messages.json.
* @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
* This parameter should be used with caution.
* @return {boolean} True if the localization was successful; false otherwise.
*/
l10n.localizeElementFromTag = function(element, tag, opt_substitutions,
opt_asHtml) {
var translation = l10n.getTranslationOrError(tag, opt_substitutions);
if (opt_asHtml) {
element.innerHTML = translation;
} else {
element.innerText = translation;
}
return translation != null;
};
/**
* Localize an element by setting its innerText according to its i18n-content
* attribute, and an optional set of substitutions.
*
* @param {Element} element The element to localize.
* @param {(string|Array)=} opt_substitutions An optional set of substitution
* strings corresponding to the "placeholders" attributes in messages.json.
* @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
* This parameter should be used with caution.
* @return {boolean} True if the localization was successful; false otherwise.
*/
l10n.localizeElement = function(element, opt_substitutions, opt_asHtml) {
var tag = element.getAttribute('i18n-content');
return l10n.localizeElementFromTag(element, tag, opt_substitutions,
opt_asHtml);
};
/**
* Localize all tags with the i18n-content attribute, using i18n-data-n
* attributes to specify any placeholder substitutions.
*
* Because we use i18n-value attributes to implement translations of rich
* content (including paragraphs with hyperlinks), we localize these as
* HTML iff there are any substitutions.
*/
l10n.localize = function() {
var elements = document.querySelectorAll('[i18n-content],[i18n-title]');
for (var i = 0; i < elements.length; ++i) {
/** @type {Element} */ var element = elements[i];
var substitutions = [];
for (var j = 1; j < 9; ++j) {
var value = 'i18n-value-' + j;
var valueName = 'i18n-value-name-' + j;
if (element.hasAttribute(value)) {
substitutions.push(element.getAttribute(value));
} else if (element.hasAttribute(valueName)) {
var name = element.getAttribute(valueName);
var translation = chrome.i18n.getMessage(name);
if (translation) {
substitutions.push(translation);
} else {
console.error('Missing translation for substitution: ' + name);
substitutions.push(name);
}
} else {
break;
}
}
var titleTag = element.getAttribute('i18n-title');
if (titleTag) {
element.title = l10n.getTranslationOrError(titleTag, substitutions);
} else {
l10n.localizeElement(element, substitutions,
substitutions.length != 0);
}
}
};