Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

perf: preload translation files and optimize language switching to avoid redundant reloads (#7939)

brios302367dc8517
diff --git a/frontend/editor/index.html b/frontend/editor/index.html
index 7b752f9f6e..f37df46d72 100644
--- a/frontend/editor/index.html
+++ b/frontend/editor/index.html
@@ -15,6 +15,34 @@
/>
<link rel="apple-touch-icon" href="modern-logo/logo192.png" />
<link rel="manifest" href="manifest.json" />
+ <!-- en-US ships on every boot as primary language or fallback; preload it
+ so the download runs with the JS instead of after. Relative so <base>
+ keeps it right on subpath deploys, matching the i18next loadPath. -->
+ <link
+ rel="preload"
+ href="locales/en-US/translation.toml"
+ as="fetch"
+ crossorigin
+ />
+ <!-- Returning visitors may run another language (stored pick at boot).
+ Preload that file too; identical URLs dedupe to one fetch. -->
+ <script>
+ (function () {
+ try {
+ var lng = localStorage.getItem("i18nextLng");
+ if (!lng || lng === "en-US" || !/^[A-Za-z-]{2,12}$/.test(lng)) return;
+ var link = document.createElement("link");
+ link.rel = "preload";
+ link.as = "fetch";
+ link.crossOrigin = "anonymous";
+ link.href =
+ "locales/" + encodeURIComponent(lng) + "/translation.toml";
+ document.head.appendChild(link);
+ } catch (e) {
+ /* storage blocked: static en-US preload above still applies */
+ }
+ })();
+ </script>
<title>Stirling PDF - 30M+ Downloads</title>
diff --git a/frontend/editor/src/core/i18n.test.ts b/frontend/editor/src/core/i18n.test.ts
new file mode 100644
index 0000000000..dfde62cf6b
--- /dev/null
+++ b/frontend/editor/src/core/i18n.test.ts
@@ -0,0 +1,50 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@app/i18n/tomlBackend", () => ({
+ default: {
+ type: "backend",
+ init() {},
+ read(
+ _lng: string,
+ _ns: string,
+ callback: (err: unknown, res: unknown) => void,
+ ) {
+ callback(null, {});
+ },
+ },
+}));
+
+import i18n, { updateSupportedLanguages } from "@app/i18n";
+import { I18N_STORAGE_KEYS, LanguageSource } from "@app/i18n/languages";
+
+describe("updateSupportedLanguages", () => {
+ beforeEach(() => {
+ localStorage.clear();
+ });
+
+ it("skips the reload when the server default matches the current language", async () => {
+ await i18n.changeLanguage("en-US");
+ const spy = vi.spyOn(i18n, "changeLanguage");
+ updateSupportedLanguages(null, "en-US");
+ expect(spy).not.toHaveBeenCalled();
+ spy.mockRestore();
+ });
+
+ it("still records the source when the reload is skipped", async () => {
+ await i18n.changeLanguage("en-US");
+ updateSupportedLanguages(null, "en-US");
+ expect(localStorage.getItem(I18N_STORAGE_KEYS.LANGUAGE)).toBe("en-US");
+ expect(localStorage.getItem(I18N_STORAGE_KEYS.LANGUAGE_SOURCE)).toBe(
+ String(LanguageSource.ServerDefault),
+ );
+ });
+
+ it("switches when the server default differs", async () => {
+ await i18n.changeLanguage("en-US");
+ const spy = vi.spyOn(i18n, "changeLanguage");
+ updateSupportedLanguages(null, "de-DE");
+ expect(spy).toHaveBeenCalledWith("de-DE");
+ spy.mockRestore();
+ await i18n.changeLanguage("en-US");
+ });
+});
diff --git a/frontend/editor/src/core/i18n.ts b/frontend/editor/src/core/i18n.ts
index 6bb5269611..d3340f4b1d 100644
--- a/frontend/editor/src/core/i18n.ts
+++ b/frontend/editor/src/core/i18n.ts
@@ -119,7 +119,12 @@ function setLanguageWithPriority(
// Only apply if new source has higher priority
if (newPriority >= currentPriority) {
- i18n.changeLanguage(language);
+ if (
+ normalizeLanguageCode(i18n.language || "") !==
+ normalizeLanguageCode(language)
+ ) {
+ i18n.changeLanguage(language);
+ }
localStorage.setItem(I18N_STORAGE_KEYS.LANGUAGE, language);
localStorage.setItem(I18N_STORAGE_KEYS.LANGUAGE_SOURCE, String(source));
return true;