OLD | NEW |
1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 /** | 6 /** |
7 * Localize the document by replacing innerText of nodes with an i18n-content | 7 * Localize the document by replacing innerText of nodes with an i18n-content |
8 * attribute with the corresponding localized string. | 8 * attribute with the corresponding localized string. |
9 */ | 9 */ |
10 | 10 |
11 var l10n = l10n || {}; | 11 var l10n = l10n || {}; |
12 | 12 |
| 13 /** |
| 14 * Localize an element by setting its innerText according to its i18n-content |
| 15 * attribute, and an optional set of substitutions. |
| 16 * @param {Element} element The element to localize. |
| 17 * @param {string=} opt_substitutions An optional set of substitution strings |
| 18 * corresponding to the "placeholders"attributes in messages.json. |
| 19 * @return {boolean} True if the localization was successful; false otherwise. |
| 20 */ |
| 21 l10n.localizeElement = function(element, opt_substitutions) { |
| 22 var tag = element.getAttribute('i18n-content'); |
| 23 var translation = chrome.i18n.getMessage(tag, opt_substitutions); |
| 24 if (translation) { |
| 25 element.innerText = translation; |
| 26 } else { |
| 27 console.error('Missing translation for "' + tag + '":', element); |
| 28 } |
| 29 return translation != null; |
| 30 }; |
| 31 |
| 32 /** |
| 33 * Localize all tags with the i18n-content attribute. |
| 34 */ |
13 l10n.localize = function() { | 35 l10n.localize = function() { |
14 var elements = document.querySelectorAll('[i18n-content]'); | 36 var elements = document.querySelectorAll('[i18n-content]'); |
15 for (var i = 0; i < elements.length; ++i) { | 37 for (var i = 0; i < elements.length; ++i) { |
16 var element = elements[i]; | 38 var element = elements[i]; |
17 var tag = element.getAttribute('i18n-content'); | 39 l10n.localizeElement(element); |
18 var translation = chrome.i18n.getMessage(tag); | |
19 if (translation) { | |
20 element.innerText = translation; | |
21 } else { | |
22 console.error('Missing translation for "' + tag +'":', element); | |
23 } | |
24 } | 40 } |
25 } | 41 }; |
OLD | NEW |