14b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// Copyright 2014 The Chromium Authors. All rights reserved. 24b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// Use of this source code is governed by a BSD-style license that can be 34b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis// found in the LICENSE file. 44b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis 54b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis 64b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis/** 74b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * @fileoverview Defines methods related to retrieving translated messages. 84b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis */ 94b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis 104b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidisgoog.provide('cvox.Msgs'); 114b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis 124b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis/** 134b562cf889bc59e1914dd2c5d9fbd7e7bfa1ad77Argyrios Kyrtzidis * @constructor 140853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis */ 150853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidiscvox.Msgs = function() { 160853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis /** 17eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor * @type {Object.<string, string>} 1805a07605322dfef2b017781042043a261c5a89cdSebastian Redl * @private 19914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor */ 200a2c5e256abb4dc031c21fe4dc92c4f3afe9947cJohn McCall this.localeNameDict_ = null; 214ae8f298b1ea51b4c2234f9148e2e4349c9bdd23Douglas Gregor}; 2231b87d8006d4863dd9b17e515ac720941efc38e3Daniel Dunbar 23eb8837b88c18631c69ac75f64ab1853762063180Douglas Gregor 24389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis/** 2587c08a5d6b9e1e44ae6f554df40139d3a6f60b33Douglas Gregor * The namespace for all Chromevox messages. 2628019772db70d4547be05a042eb950bc910f134fDouglas Gregor * @type {string} 270853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis * @const 28a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor * @private 29cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor */ 3003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencercvox.Msgs.NAMESPACE_ = 'chromevox_'; 31788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor 320853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis 33f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar/** 34f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar * Return the current locale. 354db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor * @return {string} The locale. 36cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor */ 374db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregorcvox.Msgs.prototype.getLocale = function() { 384db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor return chrome.i18n.getMessage('locale'); 394db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor}; 404db64a461cb3442934afe43c83ed3f17f7c11c1dDouglas Gregor 410853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis 420853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis/** 43521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * Returns the message with the given message id from the ChromeVox namespace. 441abc6bca3d7fb0e7b1e40fbcad6cfb5e10594548Douglas Gregor * 45521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * If we can't find a message, throw an exception. This allows us to catch 46521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * typos early. 47521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * 48521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * @param {string} messageId The id. 49521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * @param {Array.<string>=} opt_subs Substitution strings. 50521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar * @return {string} The message. 51521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar */ 52521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbarcvox.Msgs.prototype.getMsg = function(messageId, opt_subs) { 53521bf9c529e653ab28896d027352d3e16e2672d5Daniel Dunbar var message = chrome.i18n.getMessage( 540853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis cvox.Msgs.NAMESPACE_ + messageId, opt_subs); 55f96b524306ccfa623235d375deee79637bd38f29Steve Naroff if (message == undefined || message == '') { 5644c181aec37789f25f6c15543c164416f72e562aDouglas Gregor throw new Error('Invalid ChromeVox message id: ' + messageId); 573c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl } 580853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis return message; 590853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis}; 60788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor 61788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor 62788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor/** 63bdbb004f38978da0c4a75af3294d1c7b5ff84af1Douglas Gregor * Processes an HTML DOM the text of "i18n" elements with translated messages. 64788f5a1242c04762f91eaa7565c07b9865846d88Douglas Gregor * This function expects HTML elements with a i18n clean and a msgid attribute. 6528019772db70d4547be05a042eb950bc910f134fDouglas Gregor * 66405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor * @param {Node} root The root node where the translation should be performed. 67405634b215f19eec7183bd8005e34aa5a02f64a1Douglas Gregor */ 680853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidiscvox.Msgs.prototype.addTranslatedMessagesToDom = function(root) { 690853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis var elts = root.querySelectorAll('.i18n'); 700853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis for (var i = 0; i < elts.length; i++) { 710853a02c3b04d96a3c432b883e403175c954cd81Argyrios Kyrtzidis var msgid = elts[i].getAttribute('msgid'); 72389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis if (!msgid) { 73389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis throw new Error('Element has no msgid attribute: ' + elts[i]); 74389db16c63eec6ecfa9b235155252d8da766e94eArgyrios Kyrtzidis } 75914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor elts[i].textContent = this.getMsg(msgid); 76914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor elts[i].classList.add('i18n-processed'); 77914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor } 78914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor}; 79914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor 80914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor 81914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor/** 82914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor * Retuns a number formatted correctly. 83807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar * 84807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar * @param {number} num The number. 85807b06157a1a5c050520fc194d32f16d22d423a8Daniel Dunbar * @return {string} The number in the correct locale. 86914ed9d30e9abf829a62aa996b083b1e47c19ff6Douglas Gregor */ 877d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregorcvox.Msgs.prototype.getNumber = function(num) { 887d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor return '' + num; 897d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor}; 907d1d49d2971b20a97b3c2a301470b9eaaa130137Douglas Gregor 91c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar/** 92abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor * Gets a localized display name for a locale. 93abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor * NOTE: Only a subset of locale identifiers are supported. See the 94e47be3e9682e82da15059006f43c7f3c021e4fffDouglas Gregor * |CHROMEVOX_LOCALE_DICT| message. 95df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor * @param {string} locale On the form |ll| or |ll_CC|, where |ll| is 96c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar * the language code and |CC| the country code. 97c7822dbf3c01a2a5f837cff82ba7889ea755dacaDaniel Dunbar * @return {string} The display name. 98df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor */ 99df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregorcvox.Msgs.prototype.getLocaleDisplayName = function(locale) { 100df95a13ec73d2cdaea79555cb412d767f4963120Douglas Gregor if (!this.localeNameDict_) { 101213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor this.localeNameDict_ = /** @type {Object.<string, string>} */( 102213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor JSON.parse(this.getMsg('locale_dict'))); 103213f18b3d654de7d1c7cf4a329ea9d3db1c50b6aDouglas Gregor } 104f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar var name = this.localeNameDict_[locale]; 105f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar if (!name) { 106f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar throw Error('Unsupported locale identifier: ' + locale); 107f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar } 108f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar return name; 109f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar}; 110f772d1e2a5688572d07f42896a50ac57a4a41fe8Daniel Dunbar