Skip to content

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

Fix the Default Locale field rendering blank for hyphenated language tags (#7687)

Game On184497613c8c
diff --git a/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.test.tsx b/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.test.tsx
new file mode 100644
index 0000000000..eefb921360
--- /dev/null
+++ b/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.test.tsx
@@ -0,0 +1,102 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { render, screen } from "@testing-library/react";
+import { MantineProvider } from "@mantine/core";
+
+const h = vi.hoisted(() => ({
+ settings: {} as Record<string, unknown>,
+}));
+
+vi.mock("@app/services/apiClient", () => ({
+ default: { get: vi.fn().mockResolvedValue({ data: {} }), post: vi.fn() },
+}));
+vi.mock("@app/hooks/useAdminSettings", () => ({
+ useAdminSettings: () => ({
+ settings: h.settings,
+ rawSettings: {},
+ loading: false,
+ saving: false,
+ setSettings: vi.fn(),
+ fetchSettings: vi.fn(),
+ saveSettings: vi.fn(),
+ isFieldPending: () => false,
+ hasPendingChanges: () => false,
+ }),
+}));
+vi.mock("@app/components/shared/config/useRestartServer", () => ({
+ useRestartServer: () => ({
+ restartModalOpened: false,
+ showRestartModal: vi.fn(),
+ closeRestartModal: vi.fn(),
+ restartServer: vi.fn(),
+ }),
+}));
+vi.mock("@app/hooks/useSettingsDirty", () => ({
+ useSettingsDirty: () => ({
+ isDirty: false,
+ resetToSnapshot: vi.fn(),
+ markSaved: vi.fn(),
+ }),
+}));
+vi.mock("@app/contexts/PreferencesContext", () => ({
+ usePreferences: () => ({ preferences: {}, updatePreference: vi.fn() }),
+}));
+vi.mock("@app/contexts/UnsavedChangesContext", () => ({
+ useUnsavedChanges: () => ({ markClean: vi.fn() }),
+}));
+vi.mock("@app/hooks/useLoginRequired", () => ({
+ useLoginRequired: () => ({
+ loginEnabled: true,
+ validateLoginEnabled: () => true,
+ }),
+}));
+// Partial mocks: @app/i18n initialises i18next at import time and needs the
+// real initReactI18next, and the component only touches two router hooks.
+vi.mock("react-i18next", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("react-i18next")>()),
+ useTranslation: () => ({
+ t: (_key: string, fallback?: string) => fallback ?? _key,
+ i18n: { changeLanguage: vi.fn() },
+ }),
+}));
+vi.mock("react-router-dom", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("react-router-dom")>()),
+ useLocation: () => ({ hash: "", pathname: "/settings/adminGeneral" }),
+ useNavigate: () => vi.fn(),
+}));
+
+import AdminGeneralSection from "@app/components/shared/config/configSections/AdminGeneralSection";
+
+function renderWithLocale(defaultLocale: string) {
+ h.settings = { ui: {}, system: { defaultLocale } };
+ render(
+ <MantineProvider>
+ <AdminGeneralSection />
+ </MantineProvider>,
+ );
+ return screen.getByPlaceholderText("en_US") as HTMLInputElement;
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+});
+
+describe("AdminGeneralSection default locale", () => {
+ // The Select's options are keyed on the underscore form, but
+ // SYSTEM_DEFAULTLOCALE accepts a hyphenated tag too, and the backend stores
+ // whichever form was configured.
+ it("shows the configured language for the underscore form", () => {
+ expect(renderWithLocale("en_GB").value).toBe("English (UK) (en-GB)");
+ });
+
+ it("shows the configured language for the hyphenated form", () => {
+ expect(renderWithLocale("en-GB").value).toBe("English (UK) (en-GB)");
+ });
+
+ it("matches a lowercased region tag", () => {
+ expect(renderWithLocale("en-gb").value).toBe("English (UK) (en-GB)");
+ });
+
+ it("leaves the field empty when no locale is configured", () => {
+ expect(renderWithLocale("").value).toBe("");
+ });
+});
diff --git a/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.tsx b/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.tsx
index caa19e8d25..9d0a092c80 100644
--- a/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.tsx
+++ b/frontend/editor/src/proprietary/components/shared/config/configSections/AdminGeneralSection.tsx
@@ -28,6 +28,7 @@ import LoginRequiredBanner from "@app/components/shared/config/LoginRequiredBann
import { usePreferences } from "@app/contexts/PreferencesContext";
import { useUnsavedChanges } from "@app/contexts/UnsavedChangesContext";
import {
+ normalizeLanguageCode,
supportedLanguages,
toUnderscoreFormat,
toUnderscoreLanguages,
@@ -254,6 +255,17 @@ export default function AdminGeneralSection() {
() => toUnderscoreLanguages(settings.ui?.languages || []),
[settings.ui?.languages],
);
+ // SYSTEM_DEFAULTLOCALE accepts en-GB as well as en_GB, but every option below
+ // is keyed on the underscore form, so match the stored value to it before
+ // handing it to the Select. Normalising only what is displayed keeps the
+ // configured value untouched when the field is left alone.
+ const defaultLocaleValue = useMemo(
+ () =>
+ toUnderscoreFormat(
+ normalizeLanguageCode(settings.system?.defaultLocale || ""),
+ ),
+ [settings.system?.defaultLocale],
+ );
const watchedFoldersInput = useMemo(
() => (settings.customPaths?.pipeline?.watchedFoldersDirs || []).join("\n"),
[settings.customPaths?.pipeline?.watchedFoldersDirs],
@@ -611,7 +623,7 @@ export default function AdminGeneralSection() {
"admin.settings.general.defaultLocale.description",
"The default language for new users (e.g., en_US, es_ES)",
)}
- value={settings.system?.defaultLocale || ""}
+ value={defaultLocaleValue}
onChange={(value) =>
setSettings({
...settings,