From 5c14c559dde1e89344138f7982c11918fac1b64e Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Thu, 19 Oct 2023 14:33:37 +0200
Subject: [PATCH] Initialize the `L10n`-instance as soon as possible in Firefox
 (PR 17115 follow-up)

Given that there's now a bit more asynchronicity in the l10n-initialization in the Firefox PDF Viewer, after PR 17115, try to limit the impact of that by moving it to occur a tiny bit earlier in the default viewer initialization.
---
 web/app.js        | 39 +++++++++++++++++++--------------------
 web/chromecom.js  |  2 +-
 web/firefoxcom.js |  2 +-
 web/genericcom.js |  5 +++--
 4 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/web/app.js b/web/app.js
index 65a9a2e92..8c69dc245 100644
--- a/web/app.js
+++ b/web/app.js
@@ -112,7 +112,7 @@ class DefaultExternalServices {
     throw new Error("Not implemented: createPreferences");
   }
 
-  static async createL10n(options) {
+  static async createL10n() {
     throw new Error("Not implemented: createL10n");
   }
 
@@ -234,6 +234,12 @@ const PDFViewerApplication = {
 
   // Called once when the document is loaded.
   async initialize(appConfig) {
+    let l10nPromise;
+    // In the (various) extension builds, where the locale is set automatically,
+    // initialize the `L10n`-instance as soon as possible.
+    if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
+      l10nPromise = this.externalServices.createL10n();
+    }
     this.appConfig = appConfig;
 
     if (
@@ -255,7 +261,18 @@ const PDFViewerApplication = {
       await this._parseHashParams();
     }
     this._forceCssTheme();
-    await this._initializeL10n();
+
+    // Ensure that the `L10n`-instance has been initialized before creating
+    // e.g. the various viewer components.
+    if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
+      l10nPromise = this.externalServices.createL10n();
+    }
+    this.l10n = await l10nPromise;
+    document.getElementsByTagName("html")[0].dir = this.l10n.getDirection();
+    // Connect Fluent, when necessary, and translate what we already have.
+    if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
+      this.l10n.translate(appConfig.appContainer || document.documentElement);
+    }
 
     if (
       this.isViewerEmbedded &&
@@ -357,24 +374,6 @@ const PDFViewerApplication = {
     }
   },
 
-  /**
-   * @private
-   */
-  async _initializeL10n() {
-    this.l10n = await this.externalServices.createL10n(
-      typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")
-        ? { locale: AppOptions.get("locale") }
-        : null
-    );
-    document.getElementsByTagName("html")[0].dir = this.l10n.getDirection();
-    if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
-      const appContainer =
-        this.appConfig.appContainer || document.documentElement;
-      // Connect Fluent and translate what we already have.
-      this.l10n.translate(appContainer);
-    }
-  },
-
   /**
    * @private
    */
diff --git a/web/chromecom.js b/web/chromecom.js
index d00c895b5..4d19823fc 100644
--- a/web/chromecom.js
+++ b/web/chromecom.js
@@ -435,7 +435,7 @@ class ChromeExternalServices extends DefaultExternalServices {
     return new ChromePreferences();
   }
 
-  static async createL10n(options) {
+  static async createL10n() {
     return new GenericL10n(navigator.language);
   }
 
diff --git a/web/firefoxcom.js b/web/firefoxcom.js
index bc907f7b6..9628f929a 100644
--- a/web/firefoxcom.js
+++ b/web/firefoxcom.js
@@ -422,7 +422,7 @@ class FirefoxExternalServices extends DefaultExternalServices {
     FirefoxCom.request("updateEditorStates", data);
   }
 
-  static async createL10n(_options) {
+  static async createL10n() {
     const [localeProperties] = await Promise.all([
       FirefoxCom.requestAsync("getLocaleProperties", null),
       document.l10n.ready,
diff --git a/web/genericcom.js b/web/genericcom.js
index 9a0121536..384f2f22d 100644
--- a/web/genericcom.js
+++ b/web/genericcom.js
@@ -14,6 +14,7 @@
  */
 
 import { DefaultExternalServices, PDFViewerApplication } from "./app.js";
+import { AppOptions } from "./app_options.js";
 import { BasePreferences } from "./preferences.js";
 import { DownloadManager } from "./download_manager.js";
 import { GenericL10n } from "./genericl10n.js";
@@ -46,8 +47,8 @@ class GenericExternalServices extends DefaultExternalServices {
     return new GenericPreferences();
   }
 
-  static async createL10n({ locale = "en-US" }) {
-    return new GenericL10n(locale);
+  static async createL10n() {
+    return new GenericL10n(AppOptions.get("locale") || "en-US");
   }
 
   static createScripting({ sandboxBundleSrc }) {