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
10goog.provide('cvox.Msgs');
11
12/**
13 * @constructor
14 */
15cvox.Msgs = function() {
16  /**
17   * @type {Object.<string, string>}
18   * @private
19   */
20  this.localeNameDict_ = null;
21};
22
23
24/**
25 * The namespace for all Chromevox messages.
26 * @type {string}
27 * @const
28 * @private
29 */
30cvox.Msgs.NAMESPACE_ = 'chromevox_';
31
32
33/**
34 * Return the current locale.
35 * @return {string} The locale.
36 */
37cvox.Msgs.prototype.getLocale = function() {
38  return chrome.i18n.getMessage('locale');
39};
40
41
42/**
43 * Returns the message with the given message id from the ChromeVox namespace.
44 *
45 * If we can't find a message, throw an exception.  This allows us to catch
46 * typos early.
47 *
48 * @param {string} messageId The id.
49 * @param {Array.<string>=} opt_subs Substitution strings.
50 * @return {string} The message.
51 */
52cvox.Msgs.prototype.getMsg = function(messageId, opt_subs) {
53  var message = chrome.i18n.getMessage(
54      cvox.Msgs.NAMESPACE_ + messageId, opt_subs);
55  if (message == undefined || message == '') {
56    throw new Error('Invalid ChromeVox message id: ' + messageId);
57  }
58  return message;
59};
60
61
62/**
63 * Processes an HTML DOM the text of "i18n" elements with translated messages.
64 * This function expects HTML elements with a i18n clean and a msgid attribute.
65 *
66 * @param {Node} root The root node where the translation should be performed.
67 */
68cvox.Msgs.prototype.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/**
82 * Retuns a number formatted correctly.
83 *
84 * @param {number} num The number.
85 * @return {string} The number in the correct locale.
86 */
87cvox.Msgs.prototype.getNumber = function(num) {
88  return '' + num;
89};
90
91/**
92 * Gets a localized display name for a locale.
93 * NOTE: Only a subset of locale identifiers are supported.  See the
94 * |CHROMEVOX_LOCALE_DICT| message.
95 * @param {string} locale On the form |ll| or |ll_CC|, where |ll| is
96 *     the language code and |CC| the country code.
97 * @return {string} The display name.
98 */
99cvox.Msgs.prototype.getLocaleDisplayName = function(locale) {
100  if (!this.localeNameDict_) {
101    this.localeNameDict_ = /** @type {Object.<string, string>} */(
102        JSON.parse(this.getMsg('locale_dict')));
103  }
104  var name = this.localeNameDict_[locale];
105  if (!name) {
106    throw Error('Unsupported locale identifier: ' + locale);
107  }
108  return name;
109};
110