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, replacing text content with translated text messages |
| 64 * on elements marked up for translation. Elements whose class attributes |
| 65 * contain the 'i18n' class name are expected to also have an msgid |
| 66 * attribute. The value of the msgid attributes are looked up as message |
| 67 * IDs and the resulting text is used as the text content of the elements. |
| 68 * |
| 69 * @param {Node} root The root node where the translation should be performed. |
| 70 */ |
| 71 Msgs.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 * Retuns a number formatted correctly. |
| 85 * |
| 86 * @param {number} num The number. |
| 87 * @return {string} The number in the correct locale. |
| 88 */ |
| 89 Msgs.getNumber = function(num) { |
| 90 return '' + num; |
| 91 }; |
| 92 |
| 93 /** |
| 94 * Gets a localized display name for a locale. |
| 95 * NOTE: Only a subset of locale identifiers are supported. See the |
| 96 * |CHROMEVOX_LOCALE_DICT| message. |
| 97 * @param {string} locale On the form |ll| or |ll_CC|, where |ll| is |
| 98 * the language code and |CC| the country code. |
| 99 * @return {string} The display name. |
| 100 */ |
| 101 Msgs.getLocaleDisplayName = function(locale) { |
| 102 if (!Msgs.localeNameDict_) { |
| 103 Msgs.localeNameDict_ = /** @type {!Object<string>} */( |
| 104 JSON.parse(this.getMsg('locale_dict'))); |
| 105 } |
| 106 var name = Msgs.localeNameDict_[locale]; |
| 107 if (!name) { |
| 108 throw Error('Unsupported locale identifier: ' + locale); |
| 109 } |
| 110 return name; |
| 111 }; |
| 112 |
| 113 /** |
| 114 * Strings that are displayed in the user interface but don't need |
| 115 * be translated. |
| 116 * @type {Object<string>} |
| 117 */ |
| 118 Msgs.Untranslated = { |
| 119 /** The unchecked state for a checkbox in braille. */ |
| 120 CHECKBOX_UNCHECKED_STATE_BRL: '( )', |
| 121 /** The checked state for a checkbox in braille. */ |
| 122 CHECKBOX_CHECKED_STATE_BRL: '(x)', |
| 123 /** The unselected state for a radio button in braille. */ |
| 124 RADIO_UNSELECTED_STATE_BRL: '( )', |
| 125 /** The selected state for a radio button in braille. */ |
| 126 RADIO_SELECTED_STATE_BRL: '(x)', |
| 127 /** Brailled after a menu if the menu has a submenu. */ |
| 128 ARIA_HAS_SUBMENU_BRL: '->', |
| 129 /** Describes an element with the ARIA role option. */ |
| 130 ROLE_OPTION: ' ', |
| 131 /** Braille of element with the ARIA role option. */ |
| 132 ROLE_OPTION_BRL: ' ', |
| 133 /** Braille of element with the ARIA attribute aria-checked=true. */ |
| 134 ARIA_CHECKED_TRUE_BRL: '(x)', |
| 135 /** Braille of element with the ARIA attribute aria-checked=false. */ |
| 136 ARIA_CHECKED_FALSE_BRL: '( )', |
| 137 /** Braille of element with the ARIA attribute aria-checked=mixed. */ |
| 138 ARIA_CHECKED_MIXED_BRL: '(-)', |
| 139 /** Braille of element with the ARIA attribute aria-disabled=true. */ |
| 140 ARIA_DISABLED_TRUE_BRL: 'xx', |
| 141 /** Braille of element with the ARIA attribute aria-expanded=true. */ |
| 142 ARIA_EXPANDED_TRUE_BRL: '-', |
| 143 /** Braille of element with the ARIA attribute aria-expanded=false. */ |
| 144 ARIA_EXPANDED_FALSE_BRL: '+', |
| 145 /** Braille of element with the ARIA attribute aria-invalid=true. */ |
| 146 ARIA_INVALID_TRUE_BRL: '!', |
| 147 /** Braille of element with the ARIA attribute aria-pressed=true. */ |
| 148 ARIA_PRESSED_TRUE_BRL: '=', |
| 149 /** Braille of element with the ARIA attribute aria-pressed=false. */ |
| 150 ARIA_PRESSED_FALSE_BRL: ' ', |
| 151 /** Braille of element with the ARIA attribute aria-pressed=mixed. */ |
| 152 ARIA_PRESSED_MIXED_BRL: '-', |
| 153 /** Braille of element with the ARIA attribute aria-selected=true. */ |
| 154 ARIA_SELECTED_TRUE_BRL: '(x)', |
| 155 /** Braille of element with the ARIA attribute aria-selected=false. */ |
| 156 ARIA_SELECTED_FALSE_BRL: '( )', |
| 157 /** Brailled after a menu if it has a submenu. */ |
| 158 HAS_SUBMENU_BRL: '->', |
| 159 /** Brailled to describe a <time> tag. */ |
| 160 TAG_TIME_BRL: ' ', |
| 161 }; |
OLD | NEW |