Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / chromeos / chromevox / host / chrome / msgs.js
1 // Copyright 2014 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
5
6 /**
7  * @fileoverview A cvox.AbstractMsgs implementation for Chrome.
8  */
9
10 goog.provide('cvox.ChromeMsgs');
11
12 goog.require('cvox.AbstractMsgs');
13 goog.require('cvox.HostFactory');
14
15
16
17 /**
18  * @constructor
19  * @extends {cvox.AbstractMsgs}
20  */
21 cvox.ChromeMsgs = function() {
22   cvox.AbstractMsgs.call(this);
23 };
24 goog.inherits(cvox.ChromeMsgs, cvox.AbstractMsgs);
25
26
27 /**
28  * The namespace for all Chromevox messages.
29  * @type {string}
30  * @const
31  * @private
32  */
33 cvox.ChromeMsgs.NAMESPACE_ = 'chromevox_';
34
35
36 /**
37  * Return the current locale.
38  * @return {string} The locale.
39  */
40 cvox.ChromeMsgs.prototype.getLocale = function() {
41   return chrome.i18n.getMessage('locale');
42 };
43
44
45 /**
46  * Returns the message with the given message id from the ChromeVox namespace.
47  *
48  * If we can't find a message, throw an exception.  This allows us to catch
49  * typos early.
50  *
51  * @param {string} message_id The id.
52  * @param {Array.<string>=} opt_subs Substitution strings.
53  * @return {string} The message.
54  */
55 cvox.ChromeMsgs.prototype.getMsg = function(message_id, opt_subs) {
56   var message = chrome.i18n.getMessage(
57       cvox.ChromeMsgs.NAMESPACE_ + message_id, opt_subs);
58   if (message == undefined || message == '') {
59     throw new Error('Invalid ChromeVox message id: ' + message_id);
60   }
61   return message;
62 };
63
64
65 /**
66  * Processes an HTML DOM the text of "i18n" elements with translated messages.
67  * This function expects HTML elements with a i18n clean and a msgid attribute.
68  *
69  * @param {Node} root The root node where the translation should be performed.
70  */
71 cvox.ChromeMsgs.prototype.addTranslatedMessagesToDom = function(root) {
72   var elts = root.querySelectorAll('.i18n');
73   for (var i = 0; i < elts.length; i++) {
74     var msgid = elts[i].getAttribute('msgid');
75     if (!msgid) {
76       throw new Error('Element has no msgid attribute: ' + elts[i]);
77     }
78     elts[i].textContent = this.getMsg(msgid);
79     elts[i].classList.add('i18n-processed');
80   }
81 };
82
83
84 /**
85  * Retuns a number formatted correctly.
86  *
87  * @param {number} num The number.
88  * @return {string} The number in the correct locale.
89  */
90 cvox.ChromeMsgs.prototype.getNumber = function(num) {
91   return '' + num;
92 };
93
94 cvox.HostFactory.msgsConstructor = cvox.ChromeMsgs;