diff --git a/frontend/public/locales/en/common.json b/frontend/public/locales/en/common.json index 3dc0905..50471ea 100644 --- a/frontend/public/locales/en/common.json +++ b/frontend/public/locales/en/common.json @@ -63,5 +63,6 @@ "optional": "Optional", "destination": "Destination", "username": "Username", - "password": "Password" + "password": "Password", + "languageName": "English" } diff --git a/frontend/public/locales/es-ES/common.json b/frontend/public/locales/es-ES/common.json index f314e3c..0592994 100644 --- a/frontend/public/locales/es-ES/common.json +++ b/frontend/public/locales/es-ES/common.json @@ -63,5 +63,6 @@ "optional": "Opcional", "destination": "Destino", "username": "Nombre de usuario", - "password": "Contraseña" + "password": "Contraseña", + "languageName": "Español" } diff --git a/frontend/src/components/Settings/Settings.jsx b/frontend/src/components/Settings/Settings.jsx index 0ee47aa..a81a1d8 100644 --- a/frontend/src/components/Settings/Settings.jsx +++ b/frontend/src/components/Settings/Settings.jsx @@ -2,19 +2,14 @@ import { Accordion, AccordionSummary, AccordionDetails, - Checkbox, - Divider, Grid, Typography, - TextField, Select, } from "@material-ui/core"; 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 localesList from "generated/localesList.json"; function Settings() { const { t, i18n } = useTranslation(); @@ -31,8 +26,11 @@ function Settings() { diff --git a/frontend/src/generated/localesList.json b/frontend/src/generated/localesList.json new file mode 100644 index 0000000..35f6fb1 --- /dev/null +++ b/frontend/src/generated/localesList.json @@ -0,0 +1,10 @@ +[ + { + "code": "en", + "name": "English" + }, + { + "code": "es-ES", + "name": "Español" + } +] diff --git a/frontend/src/i18n.js b/frontend/src/i18n.js index fd27dd5..8d5d959 100644 --- a/frontend/src/i18n.js +++ b/frontend/src/i18n.js @@ -3,7 +3,8 @@ import languageDetector from "i18next-browser-languagedetector"; import { initReactI18next } from "react-i18next"; 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 .use(languageDetector) @@ -23,7 +24,7 @@ i18n react: { useSuspense: true, }, - supportedLngs: ["en", "es-ES"], + supportedLngs, backend: { loadPath: "/locales/{{lng}}/{{ns}}.json", }, diff --git a/frontend/src/utils/localesList.json b/frontend/src/utils/localesList.json new file mode 100644 index 0000000..35f6fb1 --- /dev/null +++ b/frontend/src/utils/localesList.json @@ -0,0 +1,10 @@ +[ + { + "code": "en", + "name": "English" + }, + { + "code": "es-ES", + "name": "Español" + } +] diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json index 2c8bc1d..39c41ba 100644 --- a/frontend/tsconfig.json +++ b/frontend/tsconfig.json @@ -5,7 +5,8 @@ "baseUrl": "src", "module": "NodeNext", "moduleResolution": "NodeNext", - "jsx": "preserve" + "jsx": "preserve", + "resolveJsonModule": true }, "include": ["src"] } diff --git a/frontend/vite-plugin-generate-locales.js b/frontend/vite-plugin-generate-locales.js new file mode 100644 index 0000000..a2f4289 --- /dev/null +++ b/frontend/vite-plugin-generate-locales.js @@ -0,0 +1,51 @@ +import fs from "fs"; +import path from "path"; +import * as url from "url"; + +const __dirname = url.fileURLToPath(new URL(".", import.meta.url)); + +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.languageName || locale, + }; + } + return { + code: locale, + name: locale, + }; + }); + + // Save the array to a JSON file + const outputPath = path.resolve( + __dirname, + "src", + "generated", + "localesList.json" + ); + fs.writeFileSync(outputPath, JSON.stringify(localesList, null, 2)); + + console.log(`Locales list saved to ${outputPath}`); + } else { + console.error("Locales directory not found."); + } + }, + }; +} diff --git a/frontend/vite.config.js b/frontend/vite.config.js index 3709e65..d7fcd87 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -1,6 +1,7 @@ import process from "node:process"; import { defineConfig, searchForWorkspaceRoot } from "vite"; import react from "@vitejs/plugin-react"; +import GenerateLocalesPlugin from "./vite-plugin-generate-locales.js"; export default defineConfig({ base: "/app", @@ -21,11 +22,12 @@ export default defineConfig({ components: "/src/components", utils: "/src/utils", external: "/src/external", + generated: "/src/generated", }, }, build: { outDir: "build", chunkSizeWarningLimit: 1000, }, - plugins: [react()], + plugins: [react(), GenerateLocalesPlugin()], });