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.
7 * @fileoverview A cvox.AbstractMsgs implementation for Chrome.
10 goog.provide('cvox.ChromeMsgs');
12 goog.require('cvox.AbstractMsgs');
13 goog.require('cvox.HostFactory');
19 * @extends {cvox.AbstractMsgs}
21 cvox.ChromeMsgs = function() {
22 cvox.AbstractMsgs.call(this);
24 goog.inherits(cvox.ChromeMsgs, cvox.AbstractMsgs);
28 * The namespace for all Chromevox messages.
33 cvox.ChromeMsgs.NAMESPACE_ = 'chromevox_';
37 * Return the current locale.
38 * @return {string} The locale.
40 cvox.ChromeMsgs.prototype.getLocale = function() {
41 return chrome.i18n.getMessage('locale');
46 * Returns the message with the given message id from the ChromeVox namespace.
48 * If we can't find a message, throw an exception. This allows us to catch
51 * @param {string} message_id The id.
52 * @param {Array.<string>=} opt_subs Substitution strings.
53 * @return {string} The message.
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);
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.
69 * @param {Node} root The root node where the translation should be performed.
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');
76 throw new Error('Element has no msgid attribute: ' + elts[i]);
78 elts[i].textContent = this.getMsg(msgid);
79 elts[i].classList.add('i18n-processed');
85 * Retuns a number formatted correctly.
87 * @param {number} num The number.
88 * @return {string} The number in the correct locale.
90 cvox.ChromeMsgs.prototype.getNumber = function(num) {
94 cvox.HostFactory.msgsConstructor = cvox.ChromeMsgs;