2024-06-22 17:05:14 +02:00
|
|
|
import { type ThemeType, fallbackLanguageTag } from "../../shared/constants";
|
2023-07-24 00:49:12 +02:00
|
|
|
import { crawl } from "../../tools/crawl";
|
|
|
|
import { join as pathJoin } from "path";
|
|
|
|
import { readFileSync } from "fs";
|
|
|
|
import { symToStr } from "tsafe/symToStr";
|
|
|
|
import { removeDuplicates } from "evt/tools/reducers/removeDuplicates";
|
|
|
|
import * as recast from "recast";
|
|
|
|
import * as babelParser from "@babel/parser";
|
|
|
|
import babelGenerate from "@babel/generator";
|
|
|
|
import * as babelTypes from "@babel/types";
|
2024-06-08 14:02:07 +02:00
|
|
|
import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
|
2024-06-21 02:13:31 +02:00
|
|
|
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
|
|
|
import * as fs from "fs";
|
2023-07-24 00:49:12 +02:00
|
|
|
|
|
|
|
export function generateMessageProperties(params: {
|
|
|
|
themeSrcDirPath: string;
|
|
|
|
themeType: ThemeType;
|
|
|
|
}): { languageTag: string; propertiesFileSource: string }[] {
|
|
|
|
const { themeSrcDirPath, themeType } = params;
|
|
|
|
|
|
|
|
let files = crawl({
|
2024-05-20 15:48:51 +02:00
|
|
|
dirPath: pathJoin(themeSrcDirPath, themeType),
|
|
|
|
returnedPathsType: "absolute"
|
2023-07-24 00:49:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
files = files.filter(file => {
|
|
|
|
const regex = /\.(js|ts|tsx)$/;
|
|
|
|
return regex.test(file);
|
|
|
|
});
|
|
|
|
|
|
|
|
files = files.sort((a, b) => {
|
|
|
|
const regex = /\.i18n\.(ts|js|tsx)$/;
|
|
|
|
const aIsI18nFile = regex.test(a);
|
|
|
|
const bIsI18nFile = regex.test(b);
|
|
|
|
return aIsI18nFile === bIsI18nFile ? 0 : aIsI18nFile ? -1 : 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
files = files.sort((a, b) => a.length - b.length);
|
|
|
|
|
2024-05-20 15:48:51 +02:00
|
|
|
files = files.filter(file =>
|
|
|
|
readFileSync(file).toString("utf8").includes("createUseI18n")
|
|
|
|
);
|
2023-07-24 00:49:12 +02:00
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
const messageBundles = files
|
2023-07-24 00:49:12 +02:00
|
|
|
.map(file => {
|
|
|
|
const root = recast.parse(readFileSync(file).toString("utf8"), {
|
2024-05-20 15:48:51 +02:00
|
|
|
parser: {
|
|
|
|
parse: (code: string) =>
|
|
|
|
babelParser.parse(code, {
|
|
|
|
sourceType: "module",
|
|
|
|
plugins: ["typescript"]
|
|
|
|
}),
|
|
|
|
generator: babelGenerate,
|
|
|
|
types: babelTypes
|
2023-07-24 00:49:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const codes: string[] = [];
|
|
|
|
|
|
|
|
recast.visit(root, {
|
2024-05-20 15:48:51 +02:00
|
|
|
visitCallExpression: function (path) {
|
|
|
|
if (
|
|
|
|
path.node.callee.type === "Identifier" &&
|
|
|
|
path.node.callee.name === "createUseI18n"
|
|
|
|
) {
|
2023-07-24 00:49:12 +02:00
|
|
|
codes.push(babelGenerate(path.node.arguments[0] as any).code);
|
|
|
|
}
|
|
|
|
this.traverse(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return codes;
|
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
.map(code => {
|
2024-06-22 17:05:14 +02:00
|
|
|
let messageBundle: {
|
2024-05-20 15:48:51 +02:00
|
|
|
[languageTag: string]: Record<string, string>;
|
|
|
|
} = {};
|
2023-07-24 00:49:12 +02:00
|
|
|
|
|
|
|
try {
|
2024-06-22 17:05:14 +02:00
|
|
|
eval(`${symToStr({ messageBundle })} = ${code}`);
|
2023-07-24 00:49:12 +02:00
|
|
|
} catch {
|
|
|
|
console.warn(
|
|
|
|
[
|
|
|
|
"WARNING: Make sure that the first argument of createUseI18n can be evaluated in a javascript",
|
|
|
|
"runtime where only the node globals are available.",
|
|
|
|
"This is important because we need to put your i18n messages in messages_*.properties files",
|
|
|
|
"or they won't be available server side.",
|
|
|
|
"\n",
|
|
|
|
"The following code could not be evaluated:",
|
|
|
|
"\n",
|
|
|
|
code
|
|
|
|
].join(" ")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
return messageBundle;
|
2023-07-24 00:49:12 +02:00
|
|
|
});
|
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
const languageTags_messageBundle = messageBundles
|
|
|
|
.map(extraMessage => Object.keys(extraMessage))
|
|
|
|
.flat()
|
|
|
|
.reduce(...removeDuplicates<string>());
|
2023-07-24 00:49:12 +02:00
|
|
|
|
|
|
|
const keyValueMapByLanguageTag: Record<string, Record<string, string>> = {};
|
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
languageTags_messageBundle.forEach(languageTag_messageBundle => {
|
2024-06-21 02:13:31 +02:00
|
|
|
const keyValueMap: Record<string, string> = {
|
|
|
|
termsText: ""
|
|
|
|
};
|
2023-07-24 00:49:12 +02:00
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
for (const messageBundle of messageBundles) {
|
|
|
|
const keyValueMap_i = messageBundle[languageTag_messageBundle];
|
2023-07-24 00:49:12 +02:00
|
|
|
|
|
|
|
if (keyValueMap_i === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(keyValueMap_i)) {
|
2024-06-21 21:24:04 +02:00
|
|
|
if (key !== "termsText" && keyValueMap[key] !== undefined) {
|
2023-07-24 00:49:12 +02:00
|
|
|
console.warn(
|
|
|
|
[
|
|
|
|
"WARNING: The following key is defined multiple times:",
|
|
|
|
"\n",
|
|
|
|
key,
|
|
|
|
"\n",
|
|
|
|
"The following value will be ignored:",
|
|
|
|
"\n",
|
|
|
|
value,
|
|
|
|
"\n",
|
|
|
|
"The following value was already defined:",
|
|
|
|
"\n",
|
|
|
|
keyValueMap[key]
|
|
|
|
].join(" ")
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyValueMap[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 17:05:14 +02:00
|
|
|
keyValueMapByLanguageTag[languageTag_messageBundle] = keyValueMap;
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.readdirSync(
|
|
|
|
pathJoin(getThisCodebaseRootDirPath(), "src", themeType, "i18n", "baseMessages")
|
|
|
|
)
|
|
|
|
.filter(baseName => baseName !== "index.ts")
|
|
|
|
.map(baseName => baseName.replace(/\.ts$/, ""))
|
|
|
|
.filter(languageTag => !languageTags_messageBundle.includes(languageTag))
|
|
|
|
.forEach(
|
|
|
|
languageTag_noMessageBundle =>
|
|
|
|
(keyValueMapByLanguageTag[languageTag_noMessageBundle] =
|
|
|
|
keyValueMapByLanguageTag[fallbackLanguageTag] ??
|
|
|
|
keyValueMapByLanguageTag[
|
|
|
|
Object.keys(keyValueMapByLanguageTag)[0]
|
|
|
|
] ?? {
|
|
|
|
termsText: ""
|
|
|
|
})
|
|
|
|
);
|
2023-07-24 00:49:12 +02:00
|
|
|
|
|
|
|
const out: { languageTag: string; propertiesFileSource: string }[] = [];
|
|
|
|
|
|
|
|
for (const [languageTag, keyValueMap] of Object.entries(keyValueMapByLanguageTag)) {
|
|
|
|
const propertiesFileSource = Object.entries(keyValueMap)
|
2024-06-08 14:02:07 +02:00
|
|
|
.map(([key, value]) => `${key}=${escapeStringForPropertiesFile(value)}`)
|
2023-07-24 00:49:12 +02:00
|
|
|
.join("\n");
|
|
|
|
|
|
|
|
out.push({
|
|
|
|
languageTag,
|
2024-06-21 02:13:31 +02:00
|
|
|
propertiesFileSource: ["", "parent=base", "", propertiesFileSource, ""].join(
|
|
|
|
"\n"
|
|
|
|
)
|
2023-07-24 00:49:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|