blob: fc4fc80ae52506cfd19af5b6fd64ca64eea23425 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Defines methods related to retrieving translated messages.
*/
goog.provide('cvox.Msgs');
/**
* @constructor
*/
cvox.Msgs = function() {
/**
* @type {Object<string, string>}
* @private
*/
this.localeNameDict_ = null;
};
/**
* The namespace for all Chromevox messages.
* @type {string}
* @const
* @private
*/
cvox.Msgs.NAMESPACE_ = 'chromevox_';
/**
* Return the current locale.
* @return {string} The locale.
*/
cvox.Msgs.prototype.getLocale = function() {
return chrome.i18n.getMessage('locale');
};
/**
* Returns the message with the given message id from the ChromeVox namespace.
*
* If we can't find a message, throw an exception. This allows us to catch
* typos early.
*
* @param {string} messageId The id.
* @param {Array<string>=} opt_subs Substitution strings.
* @return {string} The message.
*/
cvox.Msgs.prototype.getMsg = function(messageId, opt_subs) {
var message = chrome.i18n.getMessage(
cvox.Msgs.NAMESPACE_ + messageId, opt_subs);
if (message == undefined || message == '') {
throw new Error('Invalid ChromeVox message id: ' + messageId);
}
return message;
};
/**
* Processes an HTML DOM the text of "i18n" elements with translated messages.
* This function expects HTML elements with a i18n clean and a msgid attribute.
*
* @param {Node} root The root node where the translation should be performed.
*/
cvox.Msgs.prototype.addTranslatedMessagesToDom = function(root) {
var elts = root.querySelectorAll('.i18n');
for (var i = 0; i < elts.length; i++) {
var msgid = elts[i].getAttribute('msgid');
if (!msgid) {
throw new Error('Element has no msgid attribute: ' + elts[i]);
}
elts[i].textContent = this.getMsg(msgid);
elts[i].classList.add('i18n-processed');
}
};
/**
* Retuns a number formatted correctly.
*
* @param {number} num The number.
* @return {string} The number in the correct locale.
*/
cvox.Msgs.prototype.getNumber = function(num) {
return '' + num;
};
/**
* Gets a localized display name for a locale.
* NOTE: Only a subset of locale identifiers are supported. See the
* |CHROMEVOX_LOCALE_DICT| message.
* @param {string} locale On the form |ll| or |ll_CC|, where |ll| is
* the language code and |CC| the country code.
* @return {string} The display name.
*/
cvox.Msgs.prototype.getLocaleDisplayName = function(locale) {
if (!this.localeNameDict_) {
this.localeNameDict_ = /** @type {Object<string, string>} */(
JSON.parse(this.getMsg('locale_dict')));
}
var name = this.localeNameDict_[locale];
if (!name) {
throw Error('Unsupported locale identifier: ' + locale);
}
return name;
};
|