OLD | NEW |
---|---|
(Empty) | |
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 Defines methods related to retrieving translated messages. | |
8 */ | |
9 | |
10 goog.provide('Msgs'); | |
11 | |
12 /** | |
13 * @constructor | |
14 */ | |
15 Msgs = function() {}; | |
16 | |
17 /** | |
18 * The namespace for all Chromevox messages. | |
19 * @type {string} | |
20 * @const | |
21 * @private | |
22 */ | |
23 Msgs.NAMESPACE_ = 'chromevox_'; | |
24 | |
25 /** | |
26 * Dictionary of locale names. | |
27 * @type {Object<string>} | |
28 * @private | |
29 */ | |
30 Msgs.localeNameDict_ = null; | |
31 | |
32 /** | |
33 * Return the current locale. | |
34 * @return {string} The locale. | |
35 */ | |
36 Msgs.getLocale = function() { | |
37 return chrome.i18n.getMessage('locale'); | |
38 }; | |
39 | |
40 /** | |
41 * Returns the message with the given message id from the ChromeVox namespace. | |
42 * | |
43 * If we can't find a message, throw an exception. This allows us to catch | |
44 * typos early. | |
45 * | |
46 * @param {string} messageId The id. | |
47 * @param {Array<string>=} opt_subs Substitution strings. | |
48 * @return {string} The localized message. | |
49 */ | |
50 Msgs.getMsg = function(messageId, opt_subs) { | |
51 var message = Msgs.Untranslated[messageId.toUpperCase()]; | |
52 if (message !== undefined) | |
53 return message; | |
54 message = chrome.i18n.getMessage( | |
55 Msgs.NAMESPACE_ + messageId, opt_subs); | |
56 if (message == undefined || message == '') { | |
57 throw new Error('Invalid ChromeVox message id: ' + messageId); | |
58 } | |
59 return message; | |
60 }; | |
61 | |
62 /** | |
63 * Processes an HTML DOM the text of "i18n" elements with translated messages. | |
dmazzoni
2015/09/25 16:23:35
Could you rewrite this doc? I know you didn't chan
| |
64 * This function expects HTML elements with a i18n clean and a msgid attribute. | |
dmazzoni
2015/09/25 16:23:35
I think the word "clean" should be "class" here
| |
65 * | |
66 * @param {Node} root The root node where the translation should be performed. | |
67 */ | |
68 Msgs.addTranslatedMessagesToDom = function(root) { | |
69 var elts = root.querySelectorAll('.i18n'); | |
70 for (var i = 0; i < elts.length; i++) { | |
71 var msgid = elts[i].getAttribute('msgid'); | |
72 if (!msgid) { | |
73 throw new Error('Element has no msgid attribute: ' + elts[i]); | |
74 } | |
75 elts[i].textContent = this.getMsg(msgid); | |
76 elts[i].classList.add('i18n-processed'); | |
77 } | |
78 }; | |
79 | |
80 /** | |
81 * Retuns a number formatted correctly. | |
82 * | |
83 * @param {number} num The number. | |
84 * @return {string} The number in the correct locale. | |
85 */ | |
86 Msgs.getNumber = function(num) { | |
dmazzoni
2015/09/25 16:23:35
Get we delete this while we're at it?
| |
87 return '' + num; | |
88 }; | |
89 | |
90 /** | |
91 * Gets a localized display name for a locale. | |
92 * NOTE: Only a subset of locale identifiers are supported. See the | |
93 * |CHROMEVOX_LOCALE_DICT| message. | |
94 * @param {string} locale On the form |ll| or |ll_CC|, where |ll| is | |
95 * the language code and |CC| the country code. | |
96 * @return {string} The display name. | |
97 */ | |
98 Msgs.getLocaleDisplayName = function(locale) { | |
99 if (!Msgs.localeNameDict_) { | |
100 Msgs.localeNameDict_ = /** @type {!Object<string>} */( | |
101 JSON.parse(this.getMsg('locale_dict'))); | |
102 } | |
103 var name = Msgs.localeNameDict_[locale]; | |
104 if (!name) { | |
105 throw Error('Unsupported locale identifier: ' + locale); | |
106 } | |
107 return name; | |
108 }; | |
109 | |
110 /** | |
111 * Strings that are displayed in the user interface but don't need | |
112 * be translated. | |
113 * @type {Object<string>} | |
114 */ | |
115 Msgs.Untranslated = { | |
116 /** The unchecked state for a checkbox in braille. */ | |
117 CHECKBOX_UNCHECKED_STATE_BRL: '( )', | |
118 /** The checked state for a checkbox in braille. */ | |
119 CHECKBOX_CHECKED_STATE_BRL: '(x)', | |
120 /** The unselected state for a radio button in braille. */ | |
121 RADIO_UNSELECTED_STATE_BRL: '( )', | |
122 /** The selected state for a radio button in braille. */ | |
123 RADIO_SELECTED_STATE_BRL: '(x)', | |
124 /** Brailled after a menu if the menu has a submenu. */ | |
125 ARIA_HAS_SUBMENU_BRL: '->', | |
126 /** Describes an element with the ARIA role option. */ | |
127 ARIA_ROLE_OPTION: ' ', | |
128 /** Braille of element with the ARIA role option. */ | |
129 ARIA_ROLE_OPTION_BRL: ' ', | |
130 /** Braille of element with the ARIA attribute aria-checked=true. */ | |
131 ARIA_CHECKED_TRUE_BRL: '(x)', | |
132 /** Braille of element with the ARIA attribute aria-checked=false. */ | |
133 ARIA_CHECKED_FALSE_BRL: '( )', | |
134 /** Braille of element with the ARIA attribute aria-checked=mixed. */ | |
135 ARIA_CHECKED_MIXED_BRL: '(-)', | |
136 /** Braille of element with the ARIA attribute aria-disabled=true. */ | |
137 ARIA_DISABLED_TRUE_BRL: 'xx', | |
138 /** Braille of element with the ARIA attribute aria-expanded=true. */ | |
139 ARIA_EXPANDED_TRUE_BRL: '-', | |
140 /** Braille of element with the ARIA attribute aria-expanded=false. */ | |
141 ARIA_EXPANDED_FALSE_BRL: '+', | |
142 /** Braille of element with the ARIA attribute aria-invalid=true. */ | |
143 ARIA_INVALID_TRUE_BRL: '!', | |
144 /** Braille of element with the ARIA attribute aria-pressed=true. */ | |
145 ARIA_PRESSED_TRUE_BRL: '=', | |
146 /** Braille of element with the ARIA attribute aria-pressed=false. */ | |
147 ARIA_PRESSED_FALSE_BRL: ' ', | |
148 /** Braille of element with the ARIA attribute aria-pressed=mixed. */ | |
149 ARIA_PRESSED_MIXED_BRL: '-', | |
150 /** Braille of element with the ARIA attribute aria-selected=true. */ | |
151 ARIA_SELECTED_TRUE_BRL: '(x)', | |
152 /** Braille of element with the ARIA attribute aria-selected=false. */ | |
153 ARIA_SELECTED_FALSE_BRL: '( )', | |
154 /** Brailled after a menu if it has a submenu. */ | |
155 HAS_SUBMENU_BRL: '->' | |
156 }; | |
OLD | NEW |