2017-01-24 07:41:56 +09:00
|
|
|
"use strict";
|
2012-05-12 05:14:50 +09:00
|
|
|
|
2018-04-02 06:20:41 +09:00
|
|
|
// Small subset of the webL10n API by Fabien Cazenave for PDF.js extension.
|
2020-04-14 19:28:14 +09:00
|
|
|
(function (window) {
|
2022-03-01 05:24:38 +09:00
|
|
|
let gL10nData = null;
|
|
|
|
let gLanguage = "";
|
|
|
|
let gExternalLocalizerServices = null;
|
|
|
|
let gReadyState = "loading";
|
2012-05-12 05:14:50 +09:00
|
|
|
|
|
|
|
// fetch an l10n objects
|
|
|
|
function getL10nData(key) {
|
2022-03-01 05:24:38 +09:00
|
|
|
gL10nData ||= gExternalLocalizerServices.getStrings();
|
|
|
|
|
|
|
|
const data = gL10nData?.[key];
|
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
|
|
|
}
|
2020-04-14 19:28:14 +09:00
|
|
|
return text.replace(/\{\{\s*(\w+)\s*\}\}/g, function (all, name) {
|
2019-09-07 19:37:13 +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
|
|
|
|
2019-09-07 19:37:13 +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
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
translate: translateFragment,
|
2012-05-12 05:14:50 +09:00
|
|
|
};
|
2012-05-12 07:35:43 +09:00
|
|
|
})(this);
|