Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / remoting / webapp / l10n.js
blob2df3e50525593428cd57719f0a5c4c9361ed512b
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
6 var l10n = l10n || {};
8 /**
9 * Localize an element by setting its innerText according to the specified tag
10 * and an optional set of substitutions.
11 * @param {Element} element The element to localize.
12 * @param {string} tag The localization tag.
13 * @param {(string|Array)=} opt_substitutions An optional set of substitution
14 * strings corresponding to the "placeholders" attributes in messages.json.
15 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
16 * This parameter should be used with caution.
17 * @return {boolean} True if the localization was successful; false otherwise.
19 l10n.localizeElementFromTag = function(element, tag, opt_substitutions,
20 opt_asHtml) {
21 var translation = chrome.i18n.getMessage(tag, opt_substitutions);
22 if (!translation) {
23 console.error('Missing translation for "' + tag + '":', element);
24 translation = tag; // Make errors more obvious
26 if (opt_asHtml) {
27 element.innerHTML = translation;
28 } else {
29 element.innerText = translation;
31 return translation != null;
34 /**
35 * Localize an element by setting its innerText according to its i18n-content
36 * attribute, and an optional set of substitutions.
37 * @param {Element} element The element to localize.
38 * @param {(string|Array)=} opt_substitutions An optional set of substitution
39 * strings corresponding to the "placeholders" attributes in messages.json.
40 * @param {boolean=} opt_asHtml If true, set innerHTML instead of innerText.
41 * This parameter should be used with caution.
42 * @return {boolean} True if the localization was successful; false otherwise.
44 l10n.localizeElement = function(element, opt_substitutions, opt_asHtml) {
45 var tag = element.getAttribute('i18n-content');
46 return l10n.localizeElementFromTag(element, tag, opt_substitutions,
47 opt_asHtml);
50 /**
51 * Localize all tags with the i18n-content attribute, using i18n-data-n
52 * attributes to specify any placeholder substitutions.
54 * Because we use i18n-value attributes to implement translations of rich
55 * content (including paragraphs with hyperlinks), we localize these as
56 * HTML iff there are any substitutions.
58 l10n.localize = function() {
59 var elements = document.querySelectorAll('[i18n-content]');
60 for (var i = 0; i < elements.length; ++i) {
61 /** @type {Element} */ var element = elements[i];
62 var substitutions = [];
63 for (var j = 1; j < 9; ++j) {
64 var value = 'i18n-value-' + j;
65 var valueName = 'i18n-value-name-' + j;
66 if (element.hasAttribute(value)) {
67 substitutions.push(element.getAttribute(value));
68 } else if (element.hasAttribute(valueName)) {
69 var name = element.getAttribute(valueName);
70 var translation = chrome.i18n.getMessage(name);
71 if (translation) {
72 substitutions.push(translation);
73 } else {
74 console.error('Missing translation for substitution: ' + name);
75 substitutions.push(name);
77 } else {
78 break;
81 l10n.localizeElement(element, substitutions, substitutions.length != 0);
82 // Localize tool-tips
83 // TODO(jamiewalch): Move this logic to the html document.
84 var editButton = document.getElementById('this-host-rename');
85 editButton.title = chrome.i18n.getMessage(/*i18n-content*/'TOOLTIP_RENAME');