pdf.js/web/l10n_utils.js
Jonas Jenwald f07675a6a8 [api-minor] Re-factor NullL10n and remove the hard-coded l10n strings (PR 17115 follow-up)
*Please note:* These changes only affect the GENERIC build, since `NullL10n` is only a stub elsewhere (see PR 17135).

After the changes in PR 17115, which modernized and improved l10n-handling, the `NullL10n`-implementation is no longer a good fallback for the "proper" `L10n`-classes.
To improve this situation, especially for the *standalone* viewer-components, this patch makes the following changes:
 - Let the `NullL10n`-implementation extend an actual `L10n`-class, which is constant and lazily initialized, to ensure that it works *exactly* like the "proper" ones.

 - Automatically bundle the "en-US" l10n-strings in the build, via the pre-processor, such that we don't need to remember to manually update them.

 - Ensure that the *standalone* viewer-components register their DOM-elements for translation, similar to the default viewer, since this will allow future code improvements by using "data-l10n-id"/"data-l10n-args" in most (if not all) parts of the viewer.

 - Remove the `NullL10n` from the `AnnotationLayer`, to avoid affecting bundle size too much.
   For third-party users that access the `AnnotationLayer`, as exposed in the main PDF.js library, they'll now need to *manually* register it for translation. (However, the *standalone* viewer-components still works given the point above.)
2023-10-20 21:49:33 +02:00

81 lines
2.1 KiB
JavaScript

/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @typedef {import("./interfaces").IL10n} IL10n */
import { FluentBundle, FluentResource } from "fluent-bundle";
import { DOMLocalization } from "fluent-dom";
import { L10n } from "./l10n.js";
/**
* @implements {IL10n}
*/
class ConstL10n extends L10n {
static #instance;
constructor(lang) {
super({ lang });
this.setL10n(
new DOMLocalization([], ConstL10n.#generateBundles.bind(ConstL10n, lang))
);
}
static async *#generateBundles(lang) {
let text;
if (typeof PDFJSDev === "undefined") {
const url = new URL(`./locale/${lang}/viewer.ftl`, window.location.href);
const data = await fetch(url);
text = await data.text();
} else {
text = PDFJSDev.eval("DEFAULT_FTL");
}
const resource = new FluentResource(text);
const bundle = new FluentBundle(lang);
const errors = bundle.addResource(resource);
if (errors.length) {
console.error("L10n errors", errors);
}
yield bundle;
}
static get instance() {
return (this.#instance ||= new ConstL10n("en-US"));
}
}
/**
* No-op implementation of the localization service.
* @implements {IL10n}
*/
const NullL10n = {
getLanguage() {
return ConstL10n.instance.getLanguage();
},
getDirection() {
return ConstL10n.instance.getDirection();
},
async get(ids, args = null, fallback) {
return ConstL10n.instance.get(ids, args, fallback);
},
async translate(element) {
return ConstL10n.instance.translate(element);
},
};
export { NullL10n };