mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2024-09-20 06:56:05 +08:00
feat: i18n - vite plugin
This commit is contained in:
parent
6978495963
commit
7db15c26ab
48
frontend/vite-plugin-generate-locales.js.js
Normal file
48
frontend/vite-plugin-generate-locales.js.js
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
export default function GenerateLocalesPlugin() {
|
||||||
|
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,
|
||||||
|
name: commonFile.yourLanguage || locale,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
code: locale,
|
||||||
|
name: locale,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Save the array to a JSON file
|
||||||
|
const outputPath = path.resolve(
|
||||||
|
__dirname,
|
||||||
|
"src",
|
||||||
|
"utils",
|
||||||
|
"localesList.json"
|
||||||
|
);
|
||||||
|
fs.writeFileSync(outputPath, JSON.stringify(localesList, null, 2));
|
||||||
|
|
||||||
|
console.log(`Locales list saved to ${outputPath}`);
|
||||||
|
} else {
|
||||||
|
console.error("Locales directory not found.");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
import process from "node:process";
|
import process from "node:process";
|
||||||
import { defineConfig, searchForWorkspaceRoot } from "vite";
|
import { defineConfig, searchForWorkspaceRoot } from "vite";
|
||||||
import react from "@vitejs/plugin-react";
|
import react from "@vitejs/plugin-react";
|
||||||
|
import GenerateLocalesPlugin from "./vite-plugin-generate-locales.js";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
base: "/app",
|
base: "/app",
|
||||||
|
@ -27,5 +28,5 @@ export default defineConfig({
|
||||||
outDir: "build",
|
outDir: "build",
|
||||||
chunkSizeWarningLimit: 1000,
|
chunkSizeWarningLimit: 1000,
|
||||||
},
|
},
|
||||||
plugins: [react()],
|
plugins: [react(), GenerateLocalesPlugin()],
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
const fs = require("fs");
|
|
||||||
const path = require("path");
|
|
||||||
|
|
||||||
const localesDir = path.join(__dirname, "frontend", "public", "locales"); // Adjust the path if necessary
|
|
||||||
|
|
||||||
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 = require(commonFilePath);
|
|
||||||
return {
|
|
||||||
code: locale,
|
|
||||||
name: commonFile.yourLanguage || locale,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
code: locale,
|
|
||||||
name: locale,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Save the array to a JSON file
|
|
||||||
const outputPath = path.join(
|
|
||||||
__dirname,
|
|
||||||
"frontend",
|
|
||||||
"src",
|
|
||||||
"utils",
|
|
||||||
"localesList.json"
|
|
||||||
);
|
|
||||||
fs.writeFileSync(outputPath, JSON.stringify(localesList, null, 2));
|
|
||||||
|
|
||||||
console.log(`Locales list saved to ${outputPath}`);
|
|
||||||
} else {
|
|
||||||
console.error("Locales directory not found.");
|
|
||||||
}
|
|
Loading…
Reference in a new issue