From 7459850f6ae1c6b519a06dd1423fd72fb8a76502 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 26 Jan 2026 12:44:57 +0000 Subject: [PATCH] Fix storybook language addon (#31880) It was not working for languages like de_DE which were normalised to de in one codepath but not the other Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- packages/shared-components/.storybook/main.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/shared-components/.storybook/main.ts b/packages/shared-components/.storybook/main.ts index 20631c27ed..834cd6dc5b 100644 --- a/packages/shared-components/.storybook/main.ts +++ b/packages/shared-components/.storybook/main.ts @@ -13,7 +13,18 @@ import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; // Get a list of available languages so the language selector can display them at runtime -const languages = fs.readdirSync("src/i18n/strings").map((f) => f.slice(0, -5)); +const languageFiles = fs.readdirSync("src/i18n/strings").map((f) => f.slice(0, -5)); + +const languages: Record = {}; +for (const lang of languageFiles) { + const normalizedLanguage = lang.toLowerCase().replace("_", "-"); + const languageParts = normalizedLanguage.split("-"); + if (languageParts.length === 2 && languageParts[0] === languageParts[1]) { + languages[languageParts[0]] = `${lang}.json`; + } else { + languages[normalizedLanguage] = `${lang}.json`; + } +} /** * This function is used to resolve the absolute path of a package. @@ -50,19 +61,8 @@ const config: StorybookConfig = { server.middlewares.use((req, res, next) => { if (req.url === "/i18n/languages.json") { // Dynamically generate a languages.json file based on what files are available - const langJson: Record = {}; - for (const lang of languages) { - const normalizedLanguage = lang.toLowerCase().replace("_", "-"); - const languageParts = normalizedLanguage.split("-"); - if (languageParts.length === 2 && languageParts[0] === languageParts[1]) { - langJson[languageParts[0]] = `${lang}.json`; - } else { - langJson[normalizedLanguage] = `${lang}.json`; - } - } - res.setHeader("Content-Type", "application/json"); - res.end(JSON.stringify(langJson)); + res.end(JSON.stringify(languages)); } else if (req.url?.startsWith("/i18n/")) { // Serve the individual language files, which annoyingly can't be a simple // static dir because the directory structure in src doesn't match what @@ -90,7 +90,7 @@ const config: StorybookConfig = { }, env: (config) => ({ ...config, - STORYBOOK_LANGUAGES: JSON.stringify(languages), + STORYBOOK_LANGUAGES: JSON.stringify(Object.keys(languages)), }), }; export default config;