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;