2017-01-24 07:41:56 +09:00
|
|
|
"use strict";
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// Small subset of the webL10n API by Fabien Cazenave for pdf.js extension.
|
|
|
|
(function(window) {
|
2017-01-24 07:41:56 +09:00
|
|
|
var gLanguage = "";
|
2016-04-14 06:21:05 +09:00
|
|
|
var gExternalLocalizerServices = null;
|
2017-01-24 07:41:56 +09:00
|
|
|
var gReadyState = "loading";
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// fetch an l10n objects
|
|
|
|
function getL10nData(key) {
|
2016-04-14 06:21:05 +09:00
|
|
|
var response = gExternalLocalizerServices.getStrings(key);
|
2012-05-12 05:14:50 +09:00
|
|
|
var data = JSON.parse(response);
|
2014-03-23 05:45:08 +09:00
|
|
|
if (!data) {
|
2017-01-24 07:41:56 +09:00
|
|
|
console.warn("[l10n] #" + key + " missing for [" + gLanguage + "]");
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace {{arguments}} with their values
|
|
|
|
function substArguments(text, args) {
|
2014-03-23 05:45:08 +09:00
|
|
|
if (!args) {
|
2012-05-12 05:14:50 +09:00
|
|
|
return text;
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
return text.replace(/\{\{\s*(\w+)\s*\}\}/g, function(all, name) {
|
2017-01-24 07:41:56 +09:00
|
|
|
return (name in args ? args[name] : "{{" + name + "}}");
|
2012-05-12 05:14:50 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate a string
|
|
|
|
function translateString(key, args, fallback) {
|
2017-01-24 07:41:56 +09:00
|
|
|
var i = key.lastIndexOf(".");
|
2014-03-14 07:37:10 +09:00
|
|
|
var name, property;
|
|
|
|
if (i >= 0) {
|
|
|
|
name = key.substring(0, i);
|
|
|
|
property = key.substring(i + 1);
|
|
|
|
} else {
|
|
|
|
name = key;
|
2017-01-24 07:41:56 +09:00
|
|
|
property = "textContent";
|
2014-03-14 07:37:10 +09:00
|
|
|
}
|
|
|
|
var data = getL10nData(name);
|
|
|
|
var value = (data && data[property]) || fallback;
|
|
|
|
if (!value) {
|
2017-01-24 07:41:56 +09:00
|
|
|
return "{{" + key + "}}";
|
2014-03-14 07:37:10 +09:00
|
|
|
}
|
|
|
|
return substArguments(value, args);
|
2012-05-12 05:14:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// translate an HTML element
|
|
|
|
function translateElement(element) {
|
2014-03-23 05:45:08 +09:00
|
|
|
if (!element || !element.dataset) {
|
2012-05-12 05:14:50 +09:00
|
|
|
return;
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// get the related l10n object
|
2013-02-05 03:41:46 +09:00
|
|
|
var key = element.dataset.l10nId;
|
|
|
|
var data = getL10nData(key);
|
2014-03-23 05:45:08 +09:00
|
|
|
if (!data) {
|
2012-05-12 05:14:50 +09:00
|
|
|
return;
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// get arguments (if any)
|
|
|
|
// TODO: more flexible parser?
|
|
|
|
var args;
|
2014-03-23 05:45:08 +09:00
|
|
|
if (element.dataset.l10nArgs) {
|
|
|
|
try {
|
|
|
|
args = JSON.parse(element.dataset.l10nArgs);
|
|
|
|
} catch (e) {
|
2017-01-24 07:41:56 +09:00
|
|
|
console.warn("[l10n] could not parse arguments for #" + key + "");
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// translate element
|
|
|
|
// TODO: security check?
|
2014-03-23 05:45:08 +09:00
|
|
|
for (var k in data) {
|
2012-05-12 05:14:50 +09:00
|
|
|
element[k] = substArguments(data[k], args);
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// translate an HTML subtree
|
|
|
|
function translateFragment(element) {
|
2017-01-24 07:41:56 +09:00
|
|
|
element = element || document.querySelector("html");
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// check all translatable children (= w/ a `data-l10n-id' attribute)
|
2017-01-24 07:41:56 +09:00
|
|
|
var children = element.querySelectorAll("*[data-l10n-id]");
|
2012-05-12 05:14:50 +09:00
|
|
|
var elementCount = children.length;
|
2014-03-23 05:45:08 +09:00
|
|
|
for (var i = 0; i < elementCount; i++) {
|
2012-05-12 05:14:50 +09:00
|
|
|
translateElement(children[i]);
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// translate element itself if necessary
|
2014-03-23 05:45:08 +09:00
|
|
|
if (element.dataset.l10nId) {
|
2012-05-12 05:14:50 +09:00
|
|
|
translateElement(element);
|
2014-03-23 05:45:08 +09:00
|
|
|
}
|
2012-05-12 05:14:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Public API
|
|
|
|
document.mozL10n = {
|
|
|
|
// get a localized string
|
|
|
|
get: translateString,
|
|
|
|
|
2013-01-25 08:35:19 +09:00
|
|
|
// get the document language
|
[Firefox addon] Convert the code to be ES6 friendly, in order to better agree with mozilla-central coding conventions (issue 7957)
*Please note: ignoring whitespace changes is most likely necessary for the diff to be readable.*
This patch addresses all the current, in `mozilla-central`, linting failures in the addon. It should thus be possible to change the `.eslintignore` entry for PDF.js in `mozilla-central` from `browser/extensions/pdfjs/**` to `browser/extensions/pdfjs/build/**` and `browser/extensions/pdfjs/web/**` instead.
Note that we cannot, for backwards compatibility reason of the general PDF.js library, at this time make similar changes for files residing in the `build` and `web` directories in `mozilla-central`.
The main changes in this patch are that we now use [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) instead of our previous "class-like" functions, and also use the more compact [object shorthand notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015).
A couple of functions were also converted to [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), to reduced usages of `bind(this)` and `var self = this`.
One caveat with ES6 classes is that it's not (yet) possible to define private constants/helper functions within them, which is why the `NetworkManagerClosure` was kept to not change the visibility of those constant/functions.
Besides testing in Firefox Nightly 53, this patch has also been tested in Firefox ESR 45 and SeaMonkey 2.46.
However, I'd gladly welcome help with testing the patch more, to ensure that nothing has gone wrong during the refactoring.
Fixes the first bullet point of issue 7957.
2017-01-23 02:07:53 +09:00
|
|
|
getLanguage() {
|
2014-03-23 05:45:08 +09:00
|
|
|
return gLanguage;
|
|
|
|
},
|
2013-01-25 08:35:19 +09:00
|
|
|
|
|
|
|
// get the direction (ltr|rtl) of the current language
|
[Firefox addon] Convert the code to be ES6 friendly, in order to better agree with mozilla-central coding conventions (issue 7957)
*Please note: ignoring whitespace changes is most likely necessary for the diff to be readable.*
This patch addresses all the current, in `mozilla-central`, linting failures in the addon. It should thus be possible to change the `.eslintignore` entry for PDF.js in `mozilla-central` from `browser/extensions/pdfjs/**` to `browser/extensions/pdfjs/build/**` and `browser/extensions/pdfjs/web/**` instead.
Note that we cannot, for backwards compatibility reason of the general PDF.js library, at this time make similar changes for files residing in the `build` and `web` directories in `mozilla-central`.
The main changes in this patch are that we now use [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) instead of our previous "class-like" functions, and also use the more compact [object shorthand notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015).
A couple of functions were also converted to [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), to reduced usages of `bind(this)` and `var self = this`.
One caveat with ES6 classes is that it's not (yet) possible to define private constants/helper functions within them, which is why the `NetworkManagerClosure` was kept to not change the visibility of those constant/functions.
Besides testing in Firefox Nightly 53, this patch has also been tested in Firefox ESR 45 and SeaMonkey 2.46.
However, I'd gladly welcome help with testing the patch more, to ensure that nothing has gone wrong during the refactoring.
Fixes the first bullet point of issue 7957.
2017-01-23 02:07:53 +09:00
|
|
|
getDirection() {
|
2013-01-25 08:35:19 +09:00
|
|
|
// http://www.w3.org/International/questions/qa-scripts
|
|
|
|
// Arabic, Hebrew, Farsi, Pashto, Urdu
|
2017-01-24 07:41:56 +09:00
|
|
|
var rtlList = ["ar", "he", "fa", "ps", "ur"];
|
2014-10-27 06:15:31 +09:00
|
|
|
|
2015-02-03 00:12:52 +09:00
|
|
|
// use the short language code for "full" codes like 'ar-sa' (issue 5440)
|
2017-01-24 07:41:56 +09:00
|
|
|
var shortCode = gLanguage.split("-")[0];
|
2014-10-27 06:15:31 +09:00
|
|
|
|
2018-02-07 18:34:50 +09:00
|
|
|
return (rtlList.includes(shortCode) ? "rtl" : "ltr");
|
2013-03-21 17:04:44 +09:00
|
|
|
},
|
|
|
|
|
[Firefox addon] Convert the code to be ES6 friendly, in order to better agree with mozilla-central coding conventions (issue 7957)
*Please note: ignoring whitespace changes is most likely necessary for the diff to be readable.*
This patch addresses all the current, in `mozilla-central`, linting failures in the addon. It should thus be possible to change the `.eslintignore` entry for PDF.js in `mozilla-central` from `browser/extensions/pdfjs/**` to `browser/extensions/pdfjs/build/**` and `browser/extensions/pdfjs/web/**` instead.
Note that we cannot, for backwards compatibility reason of the general PDF.js library, at this time make similar changes for files residing in the `build` and `web` directories in `mozilla-central`.
The main changes in this patch are that we now use [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) instead of our previous "class-like" functions, and also use the more compact [object shorthand notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015).
A couple of functions were also converted to [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), to reduced usages of `bind(this)` and `var self = this`.
One caveat with ES6 classes is that it's not (yet) possible to define private constants/helper functions within them, which is why the `NetworkManagerClosure` was kept to not change the visibility of those constant/functions.
Besides testing in Firefox Nightly 53, this patch has also been tested in Firefox ESR 45 and SeaMonkey 2.46.
However, I'd gladly welcome help with testing the patch more, to ensure that nothing has gone wrong during the refactoring.
Fixes the first bullet point of issue 7957.
2017-01-23 02:07:53 +09:00
|
|
|
getReadyState() {
|
|
|
|
return gReadyState;
|
|
|
|
},
|
2016-11-19 03:50:29 +09:00
|
|
|
|
[Firefox addon] Convert the code to be ES6 friendly, in order to better agree with mozilla-central coding conventions (issue 7957)
*Please note: ignoring whitespace changes is most likely necessary for the diff to be readable.*
This patch addresses all the current, in `mozilla-central`, linting failures in the addon. It should thus be possible to change the `.eslintignore` entry for PDF.js in `mozilla-central` from `browser/extensions/pdfjs/**` to `browser/extensions/pdfjs/build/**` and `browser/extensions/pdfjs/web/**` instead.
Note that we cannot, for backwards compatibility reason of the general PDF.js library, at this time make similar changes for files residing in the `build` and `web` directories in `mozilla-central`.
The main changes in this patch are that we now use [classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) instead of our previous "class-like" functions, and also use the more compact [object shorthand notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015).
A couple of functions were also converted to [arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions), to reduced usages of `bind(this)` and `var self = this`.
One caveat with ES6 classes is that it's not (yet) possible to define private constants/helper functions within them, which is why the `NetworkManagerClosure` was kept to not change the visibility of those constant/functions.
Besides testing in Firefox Nightly 53, this patch has also been tested in Firefox ESR 45 and SeaMonkey 2.46.
However, I'd gladly welcome help with testing the patch more, to ensure that nothing has gone wrong during the refactoring.
Fixes the first bullet point of issue 7957.
2017-01-23 02:07:53 +09:00
|
|
|
setExternalLocalizerServices(externalLocalizerServices) {
|
2016-04-14 06:21:05 +09:00
|
|
|
gExternalLocalizerServices = externalLocalizerServices;
|
2017-05-04 10:05:53 +09:00
|
|
|
gLanguage = gExternalLocalizerServices.getLocale();
|
|
|
|
gReadyState = "complete";
|
2016-04-14 06:21:05 +09:00
|
|
|
},
|
|
|
|
|
2013-03-21 17:04:44 +09:00
|
|
|
// translate an element or document fragment
|
Fix inconsistent spacing and trailing commas in objects in `extensions/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in in one big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/extensions/firefox/content/PdfStreamConverter.jsm b/extensions/firefox/content/PdfStreamConverter.jsm
index ea91a71a..0d59dad1 100644
--- a/extensions/firefox/content/PdfStreamConverter.jsm
+++ b/extensions/firefox/content/PdfStreamConverter.jsm
@@ -773,7 +773,8 @@ class RequestListener {
response = function sendResponse(aResponse) {
try {
var listener = doc.createEvent("CustomEvent");
- let detail = Cu.cloneInto({ response: aResponse, }, doc.defaultView);
+ let detail = Cu.cloneInto({ response: aResponse, },
+ doc.defaultView);
listener.initCustomEvent("pdf.js.response", true, false, detail);
return message.dispatchEvent(listener);
} catch (e) {
```
2017-06-01 20:22:23 +09:00
|
|
|
translate: translateFragment,
|
2012-05-12 05:14:50 +09:00
|
|
|
};
|
2012-05-12 07:35:43 +09:00
|
|
|
})(this);
|