2023-10-21 13:32:15 +08:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
2023-10-21 21:48:53 +08:00
|
|
|
import * as url from "url";
|
|
|
|
|
|
|
|
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
|
2023-10-21 13:32:15 +08:00
|
|
|
|
2024-07-10 02:58:54 +08:00
|
|
|
export default function generateLocalesPlugin() {
|
2023-10-21 13:32:15 +08:00
|
|
|
return {
|
|
|
|
name: "generate-locales",
|
|
|
|
buildStart() {
|
|
|
|
const localesDir = path.resolve(__dirname, "public", "locales");
|
|
|
|
|
|
|
|
if (fs.existsSync(localesDir)) {
|
|
|
|
const localesList = fs
|
|
|
|
.readdirSync(localesDir)
|
|
|
|
.filter((file) => {
|
|
|
|
return fs.statSync(path.join(localesDir, file)).isDirectory();
|
|
|
|
})
|
|
|
|
.map((locale) => {
|
|
|
|
const commonFilePath = path.join(localesDir, locale, "common.json");
|
|
|
|
if (fs.existsSync(commonFilePath)) {
|
|
|
|
const commonFile = JSON.parse(
|
|
|
|
fs.readFileSync(commonFilePath, "utf-8")
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
code: locale,
|
2023-10-21 13:38:18 +08:00
|
|
|
name: commonFile.languageName || locale,
|
2023-10-21 13:32:15 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
code: locale,
|
|
|
|
name: locale,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// Save the array to a JSON file
|
|
|
|
const outputPath = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
"src",
|
2024-05-01 21:11:05 +08:00
|
|
|
"generated",
|
2023-10-21 13:32:15 +08:00
|
|
|
"localesList.json"
|
|
|
|
);
|
|
|
|
fs.writeFileSync(outputPath, JSON.stringify(localesList, null, 2));
|
|
|
|
|
|
|
|
console.log(`Locales list saved to ${outputPath}`);
|
|
|
|
} else {
|
|
|
|
console.error("Locales directory not found.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|