diff --git a/package-lock.json b/package-lock.json index 945e497..3549d1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "i18next": "^23.11.3", + "i18next-browser-languagedetector": "^7.2.1", "i18next-http-backend": "^2.5.1", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -9485,6 +9486,14 @@ "@babel/runtime": "^7.23.2" } }, + "node_modules/i18next-browser-languagedetector": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-7.2.1.tgz", + "integrity": "sha512-h/pM34bcH6tbz8WgGXcmWauNpQupCGr25XPp9cZwZInR9XHSjIFDYp1SIok7zSPsTOMxdvuLyu86V+g2Kycnfw==", + "dependencies": { + "@babel/runtime": "^7.23.2" + } + }, "node_modules/i18next-http-backend": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-2.5.1.tgz", diff --git a/package.json b/package.json index f70365f..d27c3c0 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "i18next": "^23.11.3", + "i18next-browser-languagedetector": "^7.2.1", "i18next-http-backend": "^2.5.1", "react": "^18.3.1", "react-dom": "^18.3.1", diff --git a/public/locales/de/translation.json b/public/locales/de/translation.json new file mode 100644 index 0000000..355bd89 --- /dev/null +++ b/public/locales/de/translation.json @@ -0,0 +1,3 @@ +{ + "hello_world": "Hallo Welt" +} \ No newline at end of file diff --git a/public/locales/en/translation.json b/public/locales/en/translation.json new file mode 100644 index 0000000..4ea0b47 --- /dev/null +++ b/public/locales/en/translation.json @@ -0,0 +1,3 @@ +{ + "hello_world": "Hello World" +} \ No newline at end of file diff --git a/src/App.js b/src/App.js index c964c55..2ff1265 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,35 @@ import React from "react" import { Route, Routes } from "react-router-dom" -import NoPageFound from "./pages/NoPageFound" -import TestPage from "./pages/TestPage"; +const NoPageFound = React.lazy(() => import("./pages/NoPageFound")); +const TestPage = React.lazy(() => import("./pages/TestPage")) function App() { + + // TODO FIX... + /* + There is an weird interaction/error with the lazy loading of the routers + and the translation or any other component, that needs to be loaded in + lazily... For now this works, but still, this is very hacky and I hate this... + */ + var wait = (ms) => { + const start = Date.now(); + let now = start; + while (now - start < ms) { + now = Date.now(); + } + } + + wait(500); + return ( + - } /> - } /> - } /> + ...}>} /> + ...}>} /> + ...}>} /> + ); } diff --git a/src/core/i18n/I18NHandler.jsx b/src/core/i18n/I18NHandler.jsx new file mode 100644 index 0000000..0254bba --- /dev/null +++ b/src/core/i18n/I18NHandler.jsx @@ -0,0 +1,5 @@ +import i18n from "./i18n"; + +export function getTranslation(translationKey) { + return i18n.t(translationKey); +} \ No newline at end of file diff --git a/src/core/i18n/i18n.js b/src/core/i18n/i18n.js new file mode 100644 index 0000000..f79e793 --- /dev/null +++ b/src/core/i18n/i18n.js @@ -0,0 +1,20 @@ +import i18n from "i18next"; +import { initReactI18next } from "react-i18next"; +import Backend from "i18next-http-backend"; +import LanguageDetector from 'i18next-browser-languagedetector'; + +i18n + .use(Backend) + .use(initReactI18next) + .use(LanguageDetector) + .init({ + fallbackLng: "en", + debug: true, + interpolation: { + escapeValue: false // react already safes from xss + } + }); + + i18n.changeLanguage('de'); // For now to control the language... Later on, not necessary... + + export default i18n; \ No newline at end of file diff --git a/src/index.js b/src/index.js index e0b7f9e..fab2226 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,13 @@ import reportWebVitals from './reportWebVitals'; import { BrowserRouter } from "react-router-dom"; import "./core/i18n/i18n"; - const root = ReactDOM.createRoot(document.getElementById('root')); root.render( - + + ); diff --git a/src/pages/TestPage.jsx b/src/pages/TestPage.jsx index 47009d0..7e6cfab 100644 --- a/src/pages/TestPage.jsx +++ b/src/pages/TestPage.jsx @@ -1,8 +1,10 @@ +import React from "react"; import InfoPageComponent from "../components/InfoPageComponent"; +import { getTranslation } from "../core/i18n/I18NHandler"; export default class TestPage extends InfoPageComponent { render() { - return (

Hello World

); + return (

{getTranslation("hello_world")}

); } } \ No newline at end of file