Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
michael@0 | 3 | /* globals FirefoxCom */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | // Small subset of the webL10n API by Fabien Cazenave for pdf.js extension. |
michael@0 | 8 | (function(window) { |
michael@0 | 9 | var gLanguage = ''; |
michael@0 | 10 | |
michael@0 | 11 | // fetch an l10n objects |
michael@0 | 12 | function getL10nData(key) { |
michael@0 | 13 | var response = FirefoxCom.requestSync('getStrings', key); |
michael@0 | 14 | var data = JSON.parse(response); |
michael@0 | 15 | if (!data) { |
michael@0 | 16 | console.warn('[l10n] #' + key + ' missing for [' + gLanguage + ']'); |
michael@0 | 17 | } |
michael@0 | 18 | return data; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | // replace {{arguments}} with their values |
michael@0 | 22 | function substArguments(text, args) { |
michael@0 | 23 | if (!args) { |
michael@0 | 24 | return text; |
michael@0 | 25 | } |
michael@0 | 26 | return text.replace(/\{\{\s*(\w+)\s*\}\}/g, function(all, name) { |
michael@0 | 27 | return (name in args ? args[name] : '{{' + name + '}}'); |
michael@0 | 28 | }); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | // translate a string |
michael@0 | 32 | function translateString(key, args, fallback) { |
michael@0 | 33 | var i = key.lastIndexOf('.'); |
michael@0 | 34 | var name, property; |
michael@0 | 35 | if (i >= 0) { |
michael@0 | 36 | name = key.substring(0, i); |
michael@0 | 37 | property = key.substring(i + 1); |
michael@0 | 38 | } else { |
michael@0 | 39 | name = key; |
michael@0 | 40 | property = 'textContent'; |
michael@0 | 41 | } |
michael@0 | 42 | var data = getL10nData(name); |
michael@0 | 43 | var value = (data && data[property]) || fallback; |
michael@0 | 44 | if (!value) { |
michael@0 | 45 | return '{{' + key + '}}'; |
michael@0 | 46 | } |
michael@0 | 47 | return substArguments(value, args); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | // translate an HTML element |
michael@0 | 51 | function translateElement(element) { |
michael@0 | 52 | if (!element || !element.dataset) { |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | // get the related l10n object |
michael@0 | 57 | var key = element.dataset.l10nId; |
michael@0 | 58 | var data = getL10nData(key); |
michael@0 | 59 | if (!data) { |
michael@0 | 60 | return; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | // get arguments (if any) |
michael@0 | 64 | // TODO: more flexible parser? |
michael@0 | 65 | var args; |
michael@0 | 66 | if (element.dataset.l10nArgs) { |
michael@0 | 67 | try { |
michael@0 | 68 | args = JSON.parse(element.dataset.l10nArgs); |
michael@0 | 69 | } catch (e) { |
michael@0 | 70 | console.warn('[l10n] could not parse arguments for #' + key + ''); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | // translate element |
michael@0 | 75 | // TODO: security check? |
michael@0 | 76 | for (var k in data) { |
michael@0 | 77 | element[k] = substArguments(data[k], args); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | // translate an HTML subtree |
michael@0 | 83 | function translateFragment(element) { |
michael@0 | 84 | element = element || document.querySelector('html'); |
michael@0 | 85 | |
michael@0 | 86 | // check all translatable children (= w/ a `data-l10n-id' attribute) |
michael@0 | 87 | var children = element.querySelectorAll('*[data-l10n-id]'); |
michael@0 | 88 | var elementCount = children.length; |
michael@0 | 89 | for (var i = 0; i < elementCount; i++) { |
michael@0 | 90 | translateElement(children[i]); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // translate element itself if necessary |
michael@0 | 94 | if (element.dataset.l10nId) { |
michael@0 | 95 | translateElement(element); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | window.addEventListener('DOMContentLoaded', function() { |
michael@0 | 100 | gLanguage = FirefoxCom.requestSync('getLocale', null); |
michael@0 | 101 | |
michael@0 | 102 | translateFragment(); |
michael@0 | 103 | |
michael@0 | 104 | // fire a 'localized' DOM event |
michael@0 | 105 | var evtObject = document.createEvent('Event'); |
michael@0 | 106 | evtObject.initEvent('localized', false, false); |
michael@0 | 107 | evtObject.language = gLanguage; |
michael@0 | 108 | window.dispatchEvent(evtObject); |
michael@0 | 109 | }); |
michael@0 | 110 | |
michael@0 | 111 | // Public API |
michael@0 | 112 | document.mozL10n = { |
michael@0 | 113 | // get a localized string |
michael@0 | 114 | get: translateString, |
michael@0 | 115 | |
michael@0 | 116 | // get the document language |
michael@0 | 117 | getLanguage: function() { |
michael@0 | 118 | return gLanguage; |
michael@0 | 119 | }, |
michael@0 | 120 | |
michael@0 | 121 | // get the direction (ltr|rtl) of the current language |
michael@0 | 122 | getDirection: function() { |
michael@0 | 123 | // http://www.w3.org/International/questions/qa-scripts |
michael@0 | 124 | // Arabic, Hebrew, Farsi, Pashto, Urdu |
michael@0 | 125 | var rtlList = ['ar', 'he', 'fa', 'ps', 'ur']; |
michael@0 | 126 | return (rtlList.indexOf(gLanguage) >= 0 ? 'rtl' : 'ltr'); |
michael@0 | 127 | }, |
michael@0 | 128 | |
michael@0 | 129 | // translate an element or document fragment |
michael@0 | 130 | translate: translateFragment |
michael@0 | 131 | }; |
michael@0 | 132 | })(this); |
michael@0 | 133 |