Chromium Code Reviews| Index: chrome/browser/resources/chromeos/chromevox/common/msgs.js |
| diff --git a/chrome/browser/resources/chromeos/chromevox/common/msgs.js b/chrome/browser/resources/chromeos/chromevox/common/msgs.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..61e5877ecf214501587681304ee341a44ce2b394 |
| --- /dev/null |
| +++ b/chrome/browser/resources/chromeos/chromevox/common/msgs.js |
| @@ -0,0 +1,156 @@ |
| +// 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('Msgs'); |
| + |
| +/** |
| + * @constructor |
| + */ |
| +Msgs = function() {}; |
| + |
| +/** |
| + * The namespace for all Chromevox messages. |
| + * @type {string} |
| + * @const |
| + * @private |
| + */ |
| +Msgs.NAMESPACE_ = 'chromevox_'; |
| + |
| +/** |
| + * Dictionary of locale names. |
| + * @type {Object<string>} |
| + * @private |
| + */ |
| +Msgs.localeNameDict_ = null; |
| + |
| +/** |
| + * Return the current locale. |
| + * @return {string} The locale. |
| + */ |
| +Msgs.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 localized message. |
| + */ |
| +Msgs.getMsg = function(messageId, opt_subs) { |
| + var message = Msgs.Untranslated[messageId.toUpperCase()]; |
| + if (message !== undefined) |
| + return message; |
| + message = chrome.i18n.getMessage( |
| + 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. |
|
dmazzoni
2015/09/25 16:23:35
Could you rewrite this doc? I know you didn't chan
|
| + * 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
|
| + * |
| + * @param {Node} root The root node where the translation should be performed. |
| + */ |
| +Msgs.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. |
| + */ |
| +Msgs.getNumber = function(num) { |
|
dmazzoni
2015/09/25 16:23:35
Get we delete this while we're at it?
|
| + 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. |
| + */ |
| +Msgs.getLocaleDisplayName = function(locale) { |
| + if (!Msgs.localeNameDict_) { |
| + Msgs.localeNameDict_ = /** @type {!Object<string>} */( |
| + JSON.parse(this.getMsg('locale_dict'))); |
| + } |
| + var name = Msgs.localeNameDict_[locale]; |
| + if (!name) { |
| + throw Error('Unsupported locale identifier: ' + locale); |
| + } |
| + return name; |
| +}; |
| + |
| +/** |
| + * Strings that are displayed in the user interface but don't need |
| + * be translated. |
| + * @type {Object<string>} |
| + */ |
| +Msgs.Untranslated = { |
| + /** The unchecked state for a checkbox in braille. */ |
| + CHECKBOX_UNCHECKED_STATE_BRL: '( )', |
| + /** The checked state for a checkbox in braille. */ |
| + CHECKBOX_CHECKED_STATE_BRL: '(x)', |
| + /** The unselected state for a radio button in braille. */ |
| + RADIO_UNSELECTED_STATE_BRL: '( )', |
| + /** The selected state for a radio button in braille. */ |
| + RADIO_SELECTED_STATE_BRL: '(x)', |
| + /** Brailled after a menu if the menu has a submenu. */ |
| + ARIA_HAS_SUBMENU_BRL: '->', |
| + /** Describes an element with the ARIA role option. */ |
| + ARIA_ROLE_OPTION: ' ', |
| + /** Braille of element with the ARIA role option. */ |
| + ARIA_ROLE_OPTION_BRL: ' ', |
| + /** Braille of element with the ARIA attribute aria-checked=true. */ |
| + ARIA_CHECKED_TRUE_BRL: '(x)', |
| + /** Braille of element with the ARIA attribute aria-checked=false. */ |
| + ARIA_CHECKED_FALSE_BRL: '( )', |
| + /** Braille of element with the ARIA attribute aria-checked=mixed. */ |
| + ARIA_CHECKED_MIXED_BRL: '(-)', |
| + /** Braille of element with the ARIA attribute aria-disabled=true. */ |
| + ARIA_DISABLED_TRUE_BRL: 'xx', |
| + /** Braille of element with the ARIA attribute aria-expanded=true. */ |
| + ARIA_EXPANDED_TRUE_BRL: '-', |
| + /** Braille of element with the ARIA attribute aria-expanded=false. */ |
| + ARIA_EXPANDED_FALSE_BRL: '+', |
| + /** Braille of element with the ARIA attribute aria-invalid=true. */ |
| + ARIA_INVALID_TRUE_BRL: '!', |
| + /** Braille of element with the ARIA attribute aria-pressed=true. */ |
| + ARIA_PRESSED_TRUE_BRL: '=', |
| + /** Braille of element with the ARIA attribute aria-pressed=false. */ |
| + ARIA_PRESSED_FALSE_BRL: ' ', |
| + /** Braille of element with the ARIA attribute aria-pressed=mixed. */ |
| + ARIA_PRESSED_MIXED_BRL: '-', |
| + /** Braille of element with the ARIA attribute aria-selected=true. */ |
| + ARIA_SELECTED_TRUE_BRL: '(x)', |
| + /** Braille of element with the ARIA attribute aria-selected=false. */ |
| + ARIA_SELECTED_FALSE_BRL: '( )', |
| + /** Brailled after a menu if it has a submenu. */ |
| + HAS_SUBMENU_BRL: '->' |
| +}; |