From fabbe544abf90d30b76bfbcadb737fcfa84fbd64 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 23 Aug 2024 12:31:55 +0200 Subject: [PATCH] Introduce a `L10n`-method to translate an element once, and use that in `PDFLayerViewer` Currently we *manually* fetch the "pdfjs-additional-layers" string and update the DOM-element, which was needed since we want to avoid triggering a bunch of otherwise unnecessary translation when appending the entire layer-tree to the DOM. By introducing a new helper method in the `L10n`-class we can avoid this, and instead use a "data-l10n-id" attribute on the element (as most other viewer code does nowadays). --- web/l10n.js | 9 +++++++++ web/pdf_layer_viewer.js | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/web/l10n.js b/web/l10n.js index f86cb0fcc0182..058ec9758ddbd 100644 --- a/web/l10n.js +++ b/web/l10n.js @@ -80,6 +80,15 @@ class L10n { } } + /** @inheritdoc */ + async translateOnce(element) { + try { + await this.#l10n.translateElements([element]); + } catch (ex) { + console.error(`translateOnce: "${ex}".`); + } + } + /** @inheritdoc */ async destroy() { for (const element of this.#elements) { diff --git a/web/pdf_layer_viewer.js b/web/pdf_layer_viewer.js index 207d6d65b9309..381030f38bf39 100644 --- a/web/pdf_layer_viewer.js +++ b/web/pdf_layer_viewer.js @@ -93,13 +93,16 @@ class PDFLayerViewer extends BaseTreeViewer { /** * @private */ - async _setNestedName(element, { name = null }) { + _setNestedName(element, { name = null }) { if (typeof name === "string") { element.textContent = this._normalizeTextContent(name); return; } - element.textContent = await this._l10n.get("pdfjs-additional-layers"); + element.setAttribute("data-l10n-id", "pdfjs-additional-layers"); element.style.fontStyle = "italic"; + // Trigger translation manually, since translation is paused when + // the final layer-tree is appended to the DOM. + this._l10n.translateOnce(element); } /**