keycloak_theme/src/bin/generate-i18n-messages.ts

88 lines
2.6 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 } from "path";
import { crawl } from "./tools/crawl";
import { downloadBuiltinKeycloakTheme } from "./install-builtin-keycloak-themes";
2021-02-28 18:40:57 +01:00
import { getProjectRoot } from "./tools/getProjectRoot";
import { rm_rf, rm_r } from "./tools/rm";
import { keycloakVersions } from "./KeycloakVersion";
2021-02-28 18:40:57 +01:00
//@ts-ignore
const propertiesParser = require("properties-parser");
for (const keycloakVersion of keycloakVersions) {
2021-02-28 18:40:57 +01:00
console.log({ keycloakVersion });
2021-02-28 18:40:57 +01:00
const tmpDirPath = pathJoin(getProjectRoot(), "tmp_xImOef9dOd44");
2021-02-28 18:40:57 +01:00
rm_rf(tmpDirPath);
2021-02-28 18:40:57 +01:00
downloadBuiltinKeycloakTheme({
keycloakVersion,
"destDirPath": tmpDirPath
});
2021-02-28 18:40:57 +01:00
type Dictionary = { [idiomId: string]: string };
2021-02-28 18:40:57 +01:00
const record: { [typeOfPage: string]: { [language: string]: Dictionary } } = {};
2021-02-28 18:40:57 +01:00
{
2021-02-28 18:40:57 +01:00
const baseThemeDirPath = pathJoin(tmpDirPath, "base");
2021-02-28 18:40:57 +01:00
crawl(baseThemeDirPath).forEach(filePath => {
2021-02-28 18:40:57 +01:00
const match = filePath.match(/^([^/]+)\/messages\/messages_([^.]+)\.properties$/);
if (match === null) {
return;
}
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(([key, value]: any) => [key, value.replace(/''/g, "'")])
);
2021-02-28 18:40:57 +01:00
});
2021-02-28 18:40:57 +01:00
}
rm_r(tmpDirPath);
2021-02-28 20:22:18 +01:00
const targetDirPath = pathJoin(getProjectRoot(), "src", "lib", "i18n", "generated_kcMessages", keycloakVersion);
2021-02-28 20:22:18 +01:00
fs.mkdirSync(targetDirPath, { "recursive": true });
2021-02-28 20:22:18 +01:00
Object.keys(record).forEach(pageType => {
2021-02-28 20:22:18 +01:00
const filePath = pathJoin(targetDirPath, `${pageType}.ts`);
fs.writeFileSync(
filePath,
Buffer.from(
[
`//This code was automatically generated by running ${pathRelative(getProjectRoot(), __filename)}`,
'//PLEASE DO NOT EDIT MANUALLY',
'',
'/* spell-checker: disable */',
`export const kcMessages= ${JSON.stringify(record[pageType], null, 2)};`,
'/* spell-checker: enable */'
].join("\n"), "utf8")
);
2021-02-28 20:22:18 +01:00
console.log(`${filePath} wrote`);
2021-02-28 20:22:18 +01:00
});
2021-02-28 20:22:18 +01:00
}