pdf.js/extensions/firefox/tools/l10n.js
Jonas Jenwald de36b2aaba 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-26 12:34:24 +01:00

132 lines
3.4 KiB
JavaScript

"use strict";
// Small subset of the webL10n API by Fabien Cazenave for PDF.js extension.
(function(window) {
var gLanguage = "";
var gExternalLocalizerServices = null;
var gReadyState = "loading";
// fetch an l10n objects
function getL10nData(key) {
var response = gExternalLocalizerServices.getStrings(key);
var data = JSON.parse(response);
if (!data) {
console.warn("[l10n] #" + key + " missing for [" + gLanguage + "]");
}
return data;
}
// replace {{arguments}} with their values
function substArguments(text, args) {
if (!args) {
return text;
}
return text.replace(/\{\{\s*(\w+)\s*\}\}/g, function(all, name) {
return name in args ? args[name] : "{{" + name + "}}";
});
}
// translate a string
function translateString(key, args, fallback) {
var i = key.lastIndexOf(".");
var name, property;
if (i >= 0) {
name = key.substring(0, i);
property = key.substring(i + 1);
} else {
name = key;
property = "textContent";
}
var data = getL10nData(name);
var value = (data && data[property]) || fallback;
if (!value) {
return "{{" + key + "}}";
}
return substArguments(value, args);
}
// translate an HTML element
function translateElement(element) {
if (!element || !element.dataset) {
return;
}
// get the related l10n object
var key = element.dataset.l10nId;
var data = getL10nData(key);
if (!data) {
return;
}
// get arguments (if any)
// TODO: more flexible parser?
var args;
if (element.dataset.l10nArgs) {
try {
args = JSON.parse(element.dataset.l10nArgs);
} catch (e) {
console.warn("[l10n] could not parse arguments for #" + key + "");
}
}
// translate element
// TODO: security check?
for (var k in data) {
element[k] = substArguments(data[k], args);
}
}
// translate an HTML subtree
function translateFragment(element) {
element = element || document.querySelector("html");
// check all translatable children (= w/ a `data-l10n-id' attribute)
var children = element.querySelectorAll("*[data-l10n-id]");
var elementCount = children.length;
for (var i = 0; i < elementCount; i++) {
translateElement(children[i]);
}
// translate element itself if necessary
if (element.dataset.l10nId) {
translateElement(element);
}
}
// Public API
document.mozL10n = {
// get a localized string
get: translateString,
// get the document language
getLanguage() {
return gLanguage;
},
// get the direction (ltr|rtl) of the current language
getDirection() {
// http://www.w3.org/International/questions/qa-scripts
// Arabic, Hebrew, Farsi, Pashto, Urdu
var rtlList = ["ar", "he", "fa", "ps", "ur"];
// use the short language code for "full" codes like 'ar-sa' (issue 5440)
var shortCode = gLanguage.split("-")[0];
return rtlList.includes(shortCode) ? "rtl" : "ltr";
},
getReadyState() {
return gReadyState;
},
setExternalLocalizerServices(externalLocalizerServices) {
gExternalLocalizerServices = externalLocalizerServices;
gLanguage = gExternalLocalizerServices.getLocale();
gReadyState = "complete";
},
// translate an element or document fragment
translate: translateFragment,
};
})(this);