keycloak_theme/scripts/generate-i18n-messages.ts

136 lines
4.8 KiB
TypeScript
Raw Normal View History

import "minimal-polyfills/Object.fromEntries";
2021-02-28 18:40:57 +01:00
import * as fs from "fs";
import { join as pathJoin, relative as pathRelative, dirname as pathDirname, sep as pathSep } from "path";
2023-03-18 02:12:12 +01:00
import { crawl } from "../src/bin/tools/crawl";
2024-05-19 10:46:26 +02:00
import { downloadKeycloakDefaultTheme } from "../src/bin/shared/downloadKeycloakDefaultTheme";
2024-02-11 23:17:39 +01:00
import { getThisCodebaseRootDirPath } from "../src/bin/tools/getThisCodebaseRootDirPath";
import { rmSync } from "../src/bin/tools/fs.rmSync";
2021-02-28 18:40:57 +01:00
// NOTE: To run without argument when we want to generate src/i18n/generated_kcMessages files,
// update the version array for generating for newer version.
2021-02-28 18:40:57 +01:00
//@ts-ignore
const propertiesParser = require("properties-parser");
async function main() {
2024-05-08 16:54:04 +02:00
const keycloakVersion = "24.0.4";
2021-02-28 18:40:57 +01:00
2024-02-11 23:17:39 +01:00
const thisCodebaseRootDirPath = getThisCodebaseRootDirPath();
const tmpDirPath = pathJoin(thisCodebaseRootDirPath, "tmp_xImOef9dOd44");
2021-02-28 18:40:57 +01:00
rmSync(tmpDirPath, { "recursive": true, "force": true });
2021-02-28 18:40:57 +01:00
fs.mkdirSync(tmpDirPath);
fs.writeFileSync(pathJoin(tmpDirPath, ".gitignore"), Buffer.from("/*\n!.gitignore\n", "utf8"));
2024-05-19 10:46:26 +02:00
await downloadKeycloakDefaultTheme({
keycloakVersion,
2023-09-04 02:49:32 +02:00
"destDirPath": tmpDirPath,
"buildOptions": {
2024-02-11 23:17:39 +01:00
"cacheDirPath": pathJoin(thisCodebaseRootDirPath, "node_modules", ".cache", "keycloakify"),
"npmWorkspaceRootDirPath": thisCodebaseRootDirPath
2023-09-04 02:49:32 +02:00
}
});
type Dictionary = { [idiomId: string]: string };
const record: { [typeOfPage: string]: { [language: string]: Dictionary } } = {};
{
const baseThemeDirPath = pathJoin(tmpDirPath, "base");
const re = new RegExp(`^([^\\${pathSep}]+)\\${pathSep}messages\\${pathSep}messages_([^.]+).properties$`);
2021-02-28 18:40:57 +01:00
2023-06-21 04:00:28 +02:00
crawl({
"dirPath": baseThemeDirPath,
"returnedPathsType": "relative to dirPath"
}).forEach(filePath => {
const match = filePath.match(re);
2021-02-28 18:40:57 +01:00
if (match === null) {
return;
}
2021-02-28 18:40:57 +01:00
const [, typeOfPage, language] = match;
2021-02-28 18:40:57 +01:00
(record[typeOfPage] ??= {})[language.replace(/_/g, "-")] = Object.fromEntries(
Object.entries(propertiesParser.parse(fs.readFileSync(pathJoin(baseThemeDirPath, filePath)).toString("utf8"))).map(
2024-05-20 15:24:35 +02:00
([key, value]: any) => [key === "locale_pt_BR" ? "locale_pt-BR" : key, value.replace(/''/g, "'")]
)
);
});
}
rmSync(tmpDirPath, { "recursive": true });
Object.keys(record).forEach(themeType => {
const recordForPageType = record[themeType];
2021-02-28 18:40:57 +01:00
if (themeType !== "login" && themeType !== "account") {
return;
2023-03-19 16:58:26 +01:00
}
2024-02-11 23:17:39 +01:00
const baseMessagesDirPath = pathJoin(thisCodebaseRootDirPath, "src", themeType, "i18n", "baseMessages");
const languages = Object.keys(recordForPageType);
const generatedFileHeader = [
2024-02-11 23:17:39 +01:00
`//This code was automatically generated by running ${pathRelative(thisCodebaseRootDirPath, __filename)}`,
"//PLEASE DO NOT EDIT MANUALLY"
].join("\n");
languages.forEach(language => {
const filePath = pathJoin(baseMessagesDirPath, `${language}.ts`);
fs.mkdirSync(pathDirname(filePath), { "recursive": true });
fs.writeFileSync(
filePath,
Buffer.from(
[
generatedFileHeader,
"",
"/* spell-checker: disable */",
`const messages= ${JSON.stringify(recordForPageType[language], null, 2)};`,
"",
"export default messages;",
"/* spell-checker: enable */"
].join("\n"),
"utf8"
)
);
2024-05-19 04:02:36 +02:00
//console.log(`${filePath} wrote`);
2022-07-31 18:57:30 +02:00
});
fs.writeFileSync(
pathJoin(baseMessagesDirPath, "index.ts"),
Buffer.from(
[
generatedFileHeader,
`import * as en from "./en";`,
"",
"export async function getMessages(currentLanguageTag: string) {",
" const { default: messages } = await (() => {",
" switch (currentLanguageTag) {",
` case "en": return en;`,
...languages
.filter(language => language !== "en")
.map(language => ` case "${language}": return import("./${language}");`),
' default: return { "default": {} };',
" }",
" })();",
" return messages;",
"}"
].join("\n"),
"utf8"
)
);
});
}
if (require.main === module) {
main();
}