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>
This commit is contained in:
Michael Telatynski 2026-01-26 12:44:57 +00:00 committed by GitHub
parent 0947517746
commit 7459850f6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<string, string> = {};
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<string, string> = {};
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;