2021-03-05 00:23:50 +01:00
|
|
|
import "minimal-polyfills/Object.fromEntries";
|
2021-02-28 18:40:57 +01:00
|
|
|
import * as fs from "fs";
|
2023-03-30 22:13:32 +02:00
|
|
|
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";
|
|
|
|
import { downloadBuiltinKeycloakTheme } from "../src/bin/download-builtin-keycloak-theme";
|
|
|
|
import { getProjectRoot } from "../src/bin/tools/getProjectRoot";
|
|
|
|
import { getLogger } from "../src/bin/tools/logger";
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-29 09:54:29 +02:00
|
|
|
// NOTE: To run without argument when we want to generate src/i18n/generated_kcMessages files,
|
2022-07-29 23:10:35 +02:00
|
|
|
// update the version array for generating for newer version.
|
|
|
|
|
2021-02-28 18:40:57 +01:00
|
|
|
//@ts-ignore
|
|
|
|
const propertiesParser = require("properties-parser");
|
|
|
|
|
2023-04-19 05:10:25 +02:00
|
|
|
const isSilent = true;
|
|
|
|
|
2022-09-08 12:06:26 +03:00
|
|
|
const logger = getLogger({ isSilent });
|
|
|
|
|
2023-03-29 09:54:29 +02:00
|
|
|
async function main() {
|
2023-03-20 00:58:35 +01:00
|
|
|
const keycloakVersion = "21.0.1";
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
const tmpDirPath = pathJoin(getProjectRoot(), "tmp_xImOef9dOd44");
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
fs.rmSync(tmpDirPath, { "recursive": true, "force": true });
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
await downloadBuiltinKeycloakTheme({
|
|
|
|
keycloakVersion,
|
|
|
|
"destDirPath": tmpDirPath,
|
|
|
|
isSilent
|
|
|
|
});
|
|
|
|
|
|
|
|
type Dictionary = { [idiomId: string]: string };
|
|
|
|
|
|
|
|
const record: { [typeOfPage: string]: { [language: string]: Dictionary } } = {};
|
|
|
|
|
|
|
|
{
|
|
|
|
const baseThemeDirPath = pathJoin(tmpDirPath, "base");
|
2023-03-30 22:13:32 +02:00
|
|
|
const re = new RegExp(`^([^\\${pathSep}]+)\\${pathSep}messages\\${pathSep}messages_([^.]+).properties$`);
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
crawl(baseThemeDirPath).forEach(filePath => {
|
2023-03-30 22:13:32 +02:00
|
|
|
const match = filePath.match(re);
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
if (match === null) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
const [, typeOfPage, language] = match;
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
(record[typeOfPage] ??= {})[language.replace(/_/g, "-")] = Object.fromEntries(
|
|
|
|
Object.entries(propertiesParser.parse(fs.readFileSync(pathJoin(baseThemeDirPath, filePath)).toString("utf8"))).map(
|
|
|
|
([key, value]: any) => [key, value.replace(/''/g, "'")]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2021-10-06 17:22:52 +02:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
fs.rmSync(tmpDirPath, { recursive: true, force: true });
|
2021-10-06 17:22:52 +02:00
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
Object.keys(record).forEach(themeType => {
|
|
|
|
const recordForPageType = record[themeType];
|
2021-02-28 18:40:57 +01:00
|
|
|
|
2023-03-20 05:14:25 +01:00
|
|
|
if (themeType !== "login" && themeType !== "account") {
|
2023-03-20 00:58:35 +01:00
|
|
|
return;
|
2023-03-19 16:58:26 +01:00
|
|
|
}
|
|
|
|
|
2023-03-20 00:58:35 +01:00
|
|
|
const baseMessagesDirPath = pathJoin(getProjectRoot(), "src", themeType, "i18n", "baseMessages");
|
|
|
|
|
|
|
|
const languages = Object.keys(recordForPageType);
|
|
|
|
|
|
|
|
const generatedFileHeader = [
|
|
|
|
`//This code was automatically generated by running ${pathRelative(getProjectRoot(), __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"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
logger.log(`${filePath} wrote`);
|
2022-07-31 18:57:30 +02:00
|
|
|
});
|
2023-03-20 00:58:35 +01:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
pathJoin(baseMessagesDirPath, "index.ts"),
|
|
|
|
Buffer.from(
|
|
|
|
[
|
|
|
|
generatedFileHeader,
|
|
|
|
"export async function getMessages(currentLanguageTag: string) {",
|
|
|
|
" const { default: messages } = await (() => {",
|
|
|
|
" switch (currentLanguageTag) {",
|
|
|
|
...languages.map(language => ` case "${language}": return import("./${language}");`),
|
|
|
|
' default: return { "default": {} };',
|
|
|
|
" }",
|
|
|
|
" })();",
|
|
|
|
" return messages;",
|
|
|
|
"}"
|
|
|
|
].join("\n"),
|
|
|
|
"utf8"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2023-03-29 09:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) {
|
2023-04-03 20:59:34 +02:00
|
|
|
main();
|
2023-03-29 09:54:29 +02:00
|
|
|
}
|