feat: dynamic import on build time

This commit is contained in:
Andres 2023-10-20 11:10:45 +02:00
parent ee73374c29
commit 6978495963
8 changed files with 67 additions and 15 deletions

View file

@ -63,5 +63,6 @@
"optional": "Optional", "optional": "Optional",
"destination": "Destination", "destination": "Destination",
"username": "Username", "username": "Username",
"password": "Password" "password": "Password",
"yourLanguage": "English"
} }

View file

@ -63,5 +63,6 @@
"optional": "Opcional", "optional": "Opcional",
"destination": "Destino", "destination": "Destino",
"username": "Nombre de usuario", "username": "Nombre de usuario",
"password": "Contraseña" "password": "Contraseña",
"yourLanguage": "Español"
} }

View file

@ -2,19 +2,14 @@ import {
Accordion, Accordion,
AccordionSummary, AccordionSummary,
AccordionDetails, AccordionDetails,
Checkbox,
Divider,
Grid, Grid,
Typography, Typography,
TextField,
Select, Select,
} from "@material-ui/core"; } from "@material-ui/core";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore"; import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import API from "utils/API";
import { parseValue, replaceValue, setValue } from "utils/ChangeHelper";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import localesList from "../../utils/localesList.json";
function Settings() { function Settings() {
const { t, i18n } = useTranslation(); const { t, i18n } = useTranslation();
@ -31,8 +26,11 @@ function Settings() {
<AccordionDetails> <AccordionDetails>
<Grid item> <Grid item>
<Select native value={i18n.language} onChange={handleChange()}> <Select native value={i18n.language} onChange={handleChange()}>
<option value={"en"}>English</option> {localesList.map((locale) => (
<option value={"es-ES"}>Español</option> <option key={locale.code} value={locale.code}>
{locale.name}
</option>
))}
</Select> </Select>
</Grid> </Grid>
</AccordionDetails> </AccordionDetails>

View file

@ -3,7 +3,8 @@ import languageDetector from "i18next-browser-languagedetector";
import { initReactI18next } from "react-i18next"; import { initReactI18next } from "react-i18next";
import Backend from "i18next-http-backend"; import Backend from "i18next-http-backend";
const userLanguage = window.navigator.language; import localesList from "./utils/localesList.json";
const supportedLngs = localesList.map((locale) => locale.code);
i18n i18n
.use(languageDetector) .use(languageDetector)
@ -23,7 +24,7 @@ i18n
react: { react: {
useSuspense: true, useSuspense: true,
}, },
supportedLngs: ["en", "es-ES"], supportedLngs,
backend: { backend: {
loadPath: "/locales/{{lng}}/{{ns}}.json", loadPath: "/locales/{{lng}}/{{ns}}.json",
}, },

View file

@ -0,0 +1,10 @@
[
{
"code": "en",
"name": "English"
},
{
"code": "es-ES",
"name": "Español"
}
]

View file

@ -5,7 +5,8 @@
"baseUrl": "src", "baseUrl": "src",
"module": "NodeNext", "module": "NodeNext",
"moduleResolution": "NodeNext", "moduleResolution": "NodeNext",
"jsx": "preserve" "jsx": "preserve",
"resolveJsonModule": true
}, },
"include": ["src"] "include": ["src"]
} }

40
generateLocalesList.js Normal file
View file

@ -0,0 +1,40 @@
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.");
}

View file

@ -13,8 +13,8 @@
"format": "yarn prettier", "format": "yarn prettier",
"format:fix": "yarn prettier --write .", "format:fix": "yarn prettier --write .",
"lint": "yarn workspaces foreach --all --parallel run lint", "lint": "yarn workspaces foreach --all --parallel run lint",
"dev": "concurrently \"cd frontend && cross-env FAST_REFRESH=true yarn start\" \"cd backend && cross-env NODE_ENV=development ZU_DEFAULT_USERNAME=admin ZU_DEFAULT_PASSWORD=zero-ui nodemon ./bin/www --ignore data/db.json\"", "dev": "concurrently \"node generateLocalesList.js && cd frontend && cross-env FAST_REFRESH=true yarn start\" \"cd backend && cross-env NODE_ENV=development ZU_DEFAULT_USERNAME=admin ZU_DEFAULT_PASSWORD=zero-ui nodemon ./bin/www --ignore data/db.json\"",
"build": "cd frontend && cross-env GENERATE_SOURCEMAP=false yarn build", "build": "node generateLocalesList.js && cd frontend && cross-env GENERATE_SOURCEMAP=false yarn build",
"prod": "cd backend && cross-env NODE_ENV=production ZU_SECURE_HEADERS=false yarn start", "prod": "cd backend && cross-env NODE_ENV=production ZU_SECURE_HEADERS=false yarn start",
"release": "standard-version && git push --follow-tags origin main && git add CHANGELOG.md", "release": "standard-version && git push --follow-tags origin main && git add CHANGELOG.md",
"commit": "yarn git-cz" "commit": "yarn git-cz"