|
|
|
@ -19,6 +19,13 @@
@@ -19,6 +19,13 @@
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
|
|
|
* IN THE SOFTWARE. |
|
|
|
|
*/ |
|
|
|
|
/* |
|
|
|
|
Additional modifications for PDF.js project: |
|
|
|
|
- Disables language initialization on page loading; |
|
|
|
|
- Removes console.warn and console.log and use console.log/warn directly. |
|
|
|
|
- Removes window._ assignment. |
|
|
|
|
- Remove compatibility code for OldIE. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
/*jshint browser: true, devel: true, es5: true, globalstrict: true */ |
|
|
|
|
'use strict'; |
|
|
|
@ -47,29 +54,6 @@ document.webL10n = (function(window, document, undefined) {
@@ -47,29 +54,6 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
var gAsyncResourceLoading = true; // read-only
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Debug helpers |
|
|
|
|
* |
|
|
|
|
* gDEBUG == 0: don't display any console message |
|
|
|
|
* gDEBUG == 1: display only warnings, not logs |
|
|
|
|
* gDEBUG == 2: display all console messages |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
var gDEBUG = 1; |
|
|
|
|
|
|
|
|
|
function consoleLog(message) { |
|
|
|
|
if (gDEBUG >= 2) { |
|
|
|
|
console.log('[l10n] ' + message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function consoleWarn(message) { |
|
|
|
|
if (gDEBUG) { |
|
|
|
|
console.warn('[l10n] ' + message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* DOM helpers for the so-called "HTML API". |
|
|
|
|
* |
|
|
|
@ -102,7 +86,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -102,7 +86,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
try { |
|
|
|
|
args = JSON.parse(l10nArgs); |
|
|
|
|
} catch (e) { |
|
|
|
|
consoleWarn('could not parse arguments for #' + l10nId); |
|
|
|
|
console.warn('could not parse arguments for #' + l10nId); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return { id: l10nId, args: args }; |
|
|
|
@ -118,7 +102,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -118,7 +102,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
function xhrLoadText(url, onSuccess, onFailure) { |
|
|
|
|
onSuccess = onSuccess || function _onSuccess(data) {}; |
|
|
|
|
onFailure = onFailure || function _onFailure() { |
|
|
|
|
consoleWarn(url + ' not found.'); |
|
|
|
|
console.warn(url + ' not found.'); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
@ -321,7 +305,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -321,7 +305,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
// we might have a pre-compiled dictionary instead
|
|
|
|
|
var dict = getL10nDictionary(); |
|
|
|
|
if (dict && dict.locales && dict.default_locale) { |
|
|
|
|
consoleLog('using the embedded JSON directory, early way out'); |
|
|
|
|
console.log('using the embedded JSON directory, early way out'); |
|
|
|
|
gL10nData = dict.locales[lang]; |
|
|
|
|
if (!gL10nData) { |
|
|
|
|
var defaultLocale = dict.default_locale.toLowerCase(); |
|
|
|
@ -337,7 +321,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -337,7 +321,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
} |
|
|
|
|
callback(); |
|
|
|
|
} else { |
|
|
|
|
consoleLog('no resource to load, early way out'); |
|
|
|
|
console.log('no resource to load, early way out'); |
|
|
|
|
} |
|
|
|
|
// early way out
|
|
|
|
|
fireL10nReadyEvent(lang); |
|
|
|
@ -364,9 +348,9 @@ document.webL10n = (function(window, document, undefined) {
@@ -364,9 +348,9 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
// are synchronously called.
|
|
|
|
|
this.load = function(lang, callback) { |
|
|
|
|
parseResource(href, lang, callback, function() { |
|
|
|
|
consoleWarn(href + ' not found.'); |
|
|
|
|
console.warn(href + ' not found.'); |
|
|
|
|
// lang not found, used default resource instead
|
|
|
|
|
consoleWarn('"' + lang + '" resource not found'); |
|
|
|
|
console.warn('"' + lang + '" resource not found'); |
|
|
|
|
gLanguage = ''; |
|
|
|
|
// Resource not loaded, but we still need to call the callback.
|
|
|
|
|
callback(); |
|
|
|
@ -795,7 +779,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -795,7 +779,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
// return a function that gives the plural form name for a given integer
|
|
|
|
|
var index = locales2rules[lang.replace(/-.*$/, '')]; |
|
|
|
|
if (!(index in pluralRules)) { |
|
|
|
|
consoleWarn('plural form unknown for [' + lang + ']'); |
|
|
|
|
console.warn('plural form unknown for [' + lang + ']'); |
|
|
|
|
return function() { return 'other'; }; |
|
|
|
|
} |
|
|
|
|
return pluralRules[index]; |
|
|
|
@ -842,7 +826,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -842,7 +826,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
function getL10nData(key, args, fallback) { |
|
|
|
|
var data = gL10nData[key]; |
|
|
|
|
if (!data) { |
|
|
|
|
consoleWarn('#' + key + ' is undefined.'); |
|
|
|
|
console.warn('#' + key + ' is undefined.'); |
|
|
|
|
if (!fallback) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
@ -900,7 +884,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -900,7 +884,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
if (arg in gL10nData) { |
|
|
|
|
return gL10nData[arg]; |
|
|
|
|
} |
|
|
|
|
consoleLog('argument {{' + arg + '}} for #' + key + ' is undefined.'); |
|
|
|
|
console.log('argument {{' + arg + '}} for #' + key + ' is undefined.'); |
|
|
|
|
return matched_text; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
@ -914,7 +898,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -914,7 +898,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
// get the related l10n object
|
|
|
|
|
var data = getL10nData(l10n.id, l10n.args); |
|
|
|
|
if (!data) { |
|
|
|
|
consoleWarn('#' + l10n.id + ' is undefined.'); |
|
|
|
|
console.warn('#' + l10n.id + ' is undefined.'); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -982,152 +966,6 @@ document.webL10n = (function(window, document, undefined) {
@@ -982,152 +966,6 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
translateElement(element); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Startup & Public API |
|
|
|
|
* |
|
|
|
|
* Warning: this part of the code contains browser-specific chunks -- |
|
|
|
|
* that's where obsolete browsers, namely IE8 and earlier, are handled. |
|
|
|
|
* |
|
|
|
|
* Unlike the rest of the lib, this section is not shared with FirefoxOS/Gaia. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
// load the default locale on startup
|
|
|
|
|
function l10nStartup() { |
|
|
|
|
gReadyState = 'interactive'; |
|
|
|
|
|
|
|
|
|
// most browsers expose the UI language as `navigator.language'
|
|
|
|
|
// but IE uses `navigator.userLanguage' instead
|
|
|
|
|
var userLocale = navigator.language || navigator.userLanguage; |
|
|
|
|
consoleLog('loading [' + userLocale + '] resources, ' + |
|
|
|
|
(gAsyncResourceLoading ? 'asynchronously.' : 'synchronously.')); |
|
|
|
|
|
|
|
|
|
// load the default locale and translate the document if required
|
|
|
|
|
if (document.documentElement.lang === userLocale) { |
|
|
|
|
loadLocale(userLocale); |
|
|
|
|
} else { |
|
|
|
|
loadLocale(userLocale, translateFragment); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// browser-specific startup
|
|
|
|
|
if (document.addEventListener) { // modern browsers and IE9+
|
|
|
|
|
if (document.readyState === 'loading') { |
|
|
|
|
// the document is not fully loaded yet: wait for DOMContentLoaded.
|
|
|
|
|
document.addEventListener('DOMContentLoaded', l10nStartup); |
|
|
|
|
} else { |
|
|
|
|
// l10n.js is being loaded with <script defer> or <script async>,
|
|
|
|
|
// the DOM is ready for parsing.
|
|
|
|
|
window.setTimeout(l10nStartup); |
|
|
|
|
} |
|
|
|
|
} else if (window.attachEvent) { // IE8 and before (= oldIE)
|
|
|
|
|
// TODO: check if jQuery is loaded (CSS selector + JSON + events)
|
|
|
|
|
|
|
|
|
|
// dummy `console.log' and `console.warn' functions
|
|
|
|
|
if (!window.console) { |
|
|
|
|
consoleLog = function(message) {}; // just ignore console.log calls
|
|
|
|
|
consoleWarn = function(message) { |
|
|
|
|
if (gDEBUG) { |
|
|
|
|
alert('[l10n] ' + message); // vintage debugging, baby!
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// XMLHttpRequest for IE6
|
|
|
|
|
if (!window.XMLHttpRequest) { |
|
|
|
|
xhrLoadText = function(url, onSuccess, onFailure) { |
|
|
|
|
onSuccess = onSuccess || function _onSuccess(data) {}; |
|
|
|
|
onFailure = onFailure || function _onFailure() { |
|
|
|
|
consoleWarn(url + ' not found.'); |
|
|
|
|
}; |
|
|
|
|
var xhr = new ActiveXObject('Microsoft.XMLHTTP'); |
|
|
|
|
xhr.open('GET', url, gAsyncResourceLoading); |
|
|
|
|
xhr.onreadystatechange = function() { |
|
|
|
|
if (xhr.readyState == 4) { |
|
|
|
|
if (xhr.status == 200) { |
|
|
|
|
onSuccess(xhr.responseText); |
|
|
|
|
} else { |
|
|
|
|
onFailure(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
xhr.send(null); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// worst hack ever for IE6 and IE7
|
|
|
|
|
if (!window.JSON) { |
|
|
|
|
getL10nAttributes = function(element) { |
|
|
|
|
if (!element) |
|
|
|
|
return {}; |
|
|
|
|
var l10nId = element.getAttribute('data-l10n-id'), |
|
|
|
|
l10nArgs = element.getAttribute('data-l10n-args'), |
|
|
|
|
args = {}; |
|
|
|
|
if (l10nArgs) try { |
|
|
|
|
args = eval(l10nArgs); // XXX yeah, I know...
|
|
|
|
|
} catch (e) { |
|
|
|
|
consoleWarn('could not parse arguments for #' + l10nId); |
|
|
|
|
} |
|
|
|
|
return { id: l10nId, args: args }; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// override `getTranslatableChildren' and `getL10nResourceLinks'
|
|
|
|
|
if (!document.querySelectorAll) { |
|
|
|
|
getTranslatableChildren = function(element) { |
|
|
|
|
if (!element) |
|
|
|
|
return []; |
|
|
|
|
var nodes = element.getElementsByTagName('*'), |
|
|
|
|
l10nElements = [], |
|
|
|
|
n = nodes.length; |
|
|
|
|
for (var i = 0; i < n; i++) { |
|
|
|
|
if (nodes[i].getAttribute('data-l10n-id')) |
|
|
|
|
l10nElements.push(nodes[i]); |
|
|
|
|
} |
|
|
|
|
return l10nElements; |
|
|
|
|
}; |
|
|
|
|
getL10nResourceLinks = function() { |
|
|
|
|
var links = document.getElementsByTagName('link'), |
|
|
|
|
l10nLinks = [], |
|
|
|
|
n = links.length; |
|
|
|
|
for (var i = 0; i < n; i++) { |
|
|
|
|
if (links[i].type == 'application/l10n') |
|
|
|
|
l10nLinks.push(links[i]); |
|
|
|
|
} |
|
|
|
|
return l10nLinks; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// override `getL10nDictionary'
|
|
|
|
|
if (!window.JSON || !document.querySelectorAll) { |
|
|
|
|
getL10nDictionary = function() { |
|
|
|
|
var scripts = document.getElementsByName('script'); |
|
|
|
|
for (var i = 0; i < scripts.length; i++) { |
|
|
|
|
if (scripts[i].type == 'application/l10n') { |
|
|
|
|
return eval(scripts[i].innerHTML); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// fire non-standard `localized' DOM events
|
|
|
|
|
if (document.createEventObject && !document.createEvent) { |
|
|
|
|
fireL10nReadyEvent = function(lang) { |
|
|
|
|
// hack to simulate a custom event in IE:
|
|
|
|
|
// to catch this event, add an event handler to `onpropertychange'
|
|
|
|
|
document.documentElement.localized = 1; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// startup for IE<9
|
|
|
|
|
window.attachEvent('onload', function() { |
|
|
|
|
gTextProp = document.textContent === null ? 'textContent' : 'innerText'; |
|
|
|
|
l10nStartup(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// cross-browser API (sorry, oldIE doesn't support getters & setters)
|
|
|
|
|
return { |
|
|
|
|
// get a localized string
|
|
|
|
|
get: function(key, args, fallbackString) { |
|
|
|
@ -1189,20 +1027,7 @@ document.webL10n = (function(window, document, undefined) {
@@ -1189,20 +1027,7 @@ document.webL10n = (function(window, document, undefined) {
|
|
|
|
|
document.removeEventListener('localized', once); |
|
|
|
|
callback(); |
|
|
|
|
}); |
|
|
|
|
} else if (document.attachEvent) { |
|
|
|
|
document.documentElement.attachEvent('onpropertychange', function once(e) { |
|
|
|
|
if (e.propertyName === 'localized') { |
|
|
|
|
document.documentElement.detachEvent('onpropertychange', once); |
|
|
|
|
callback(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
}) (window, document); |
|
|
|
|
|
|
|
|
|
// gettext-like shortcut for document.webL10n.get
|
|
|
|
|
if (window._ === undefined) { |
|
|
|
|
var _ = document.webL10n.get; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|