Skip to content

Commit

Permalink
Initialize the L10n-instance as soon as possible in Firefox (PR 171…
Browse files Browse the repository at this point in the history
…15 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.
  • Loading branch information
Snuffleupagus committed Oct 19, 2023
1 parent d094e8a commit 5c14c55
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
39 changes: 19 additions & 20 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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 (
Expand All @@ -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 &&
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion web/chromecom.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class ChromeExternalServices extends DefaultExternalServices {
return new ChromePreferences();
}

static async createL10n(options) {
static async createL10n() {
return new GenericL10n(navigator.language);
}

Expand Down
2 changes: 1 addition & 1 deletion web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions web/genericcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 }) {
Expand Down

0 comments on commit 5c14c55

Please sign in to comment.