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