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:
@@ -13,7 +13,18 @@ import { dirname } from "node:path";
|
|||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
// Get a list of available languages so the language selector can display them at runtime
|
// 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.
|
* 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) => {
|
server.middlewares.use((req, res, next) => {
|
||||||
if (req.url === "/i18n/languages.json") {
|
if (req.url === "/i18n/languages.json") {
|
||||||
// Dynamically generate a languages.json file based on what files are available
|
// 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.setHeader("Content-Type", "application/json");
|
||||||
res.end(JSON.stringify(langJson));
|
res.end(JSON.stringify(languages));
|
||||||
} else if (req.url?.startsWith("/i18n/")) {
|
} else if (req.url?.startsWith("/i18n/")) {
|
||||||
// Serve the individual language files, which annoyingly can't be a simple
|
// Serve the individual language files, which annoyingly can't be a simple
|
||||||
// static dir because the directory structure in src doesn't match what
|
// static dir because the directory structure in src doesn't match what
|
||||||
@@ -90,7 +90,7 @@ const config: StorybookConfig = {
|
|||||||
},
|
},
|
||||||
env: (config) => ({
|
env: (config) => ({
|
||||||
...config,
|
...config,
|
||||||
STORYBOOK_LANGUAGES: JSON.stringify(languages),
|
STORYBOOK_LANGUAGES: JSON.stringify(Object.keys(languages)),
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
export default config;
|
export default config;
|
||||||
|
|||||||
Reference in New Issue
Block a user