Browse Source

feat: Added translation capabilities.

master
Denis Thiessen 7 months ago
parent
commit
847e8d35f2
  1. 9
      package-lock.json
  2. 1
      package.json
  3. 3
      public/locales/de/translation.json
  4. 3
      public/locales/en/translation.json
  5. 29
      src/App.js
  6. 5
      src/core/i18n/I18NHandler.jsx
  7. 20
      src/core/i18n/i18n.js
  8. 2
      src/index.js
  9. 4
      src/pages/TestPage.jsx

9
package-lock.json

@ -12,6 +12,7 @@
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"i18next": "^23.11.3", "i18next": "^23.11.3",
"i18next-browser-languagedetector": "^7.2.1",
"i18next-http-backend": "^2.5.1", "i18next-http-backend": "^2.5.1",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
@ -9485,6 +9486,14 @@
"@babel/runtime": "^7.23.2" "@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": { "node_modules/i18next-http-backend": {
"version": "2.5.1", "version": "2.5.1",
"resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-2.5.1.tgz", "resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-2.5.1.tgz",

1
package.json

@ -7,6 +7,7 @@
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"i18next": "^23.11.3", "i18next": "^23.11.3",
"i18next-browser-languagedetector": "^7.2.1",
"i18next-http-backend": "^2.5.1", "i18next-http-backend": "^2.5.1",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",

3
public/locales/de/translation.json

@ -0,0 +1,3 @@
{
"hello_world": "Hallo Welt"
}

3
public/locales/en/translation.json

@ -0,0 +1,3 @@
{
"hello_world": "Hello World"
}

29
src/App.js

@ -1,16 +1,35 @@
import React from "react" import React from "react"
import { Route, Routes } from "react-router-dom" 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() { 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 ( return (
<React.Suspense fallback="loading">
<Routes> <Routes>
<Route path="/" element={<TestPage />} />
<Route path="/info" element={<TestPage />} />
<Route path="*" element={<NoPageFound />} />
<Route path="/" element={<React.Suspense fallback={<>...</>}><TestPage /></React.Suspense>} />
<Route path="/info" element={<React.Suspense fallback={<>...</>}><TestPage /></React.Suspense>} />
<Route path="*" element={<React.Suspense fallback={<>...</>}><NoPageFound /></React.Suspense>} />
</Routes> </Routes>
</React.Suspense>
); );
} }

5
src/core/i18n/I18NHandler.jsx

@ -0,0 +1,5 @@
import i18n from "./i18n";
export function getTranslation(translationKey) {
return i18n.t(translationKey);
}

20
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;

2
src/index.js

@ -6,13 +6,13 @@ import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from "react-router-dom"; import { BrowserRouter } from "react-router-dom";
import "./core/i18n/i18n"; import "./core/i18n/i18n";
const root = ReactDOM.createRoot(document.getElementById('root')); const root = ReactDOM.createRoot(document.getElementById('root'));
root.render( root.render(
<React.StrictMode> <React.StrictMode>
<BrowserRouter> <BrowserRouter>
<App /> <App />
</BrowserRouter> </BrowserRouter>
</React.StrictMode> </React.StrictMode>
); );

4
src/pages/TestPage.jsx

@ -1,8 +1,10 @@
import React from "react";
import InfoPageComponent from "../components/InfoPageComponent"; import InfoPageComponent from "../components/InfoPageComponent";
import { getTranslation } from "../core/i18n/I18NHandler";
export default class TestPage extends InfoPageComponent { export default class TestPage extends InfoPageComponent {
render() { render() {
return (<InfoPageComponent><p>Hello World</p></InfoPageComponent>);
return (<InfoPageComponent><p>{getTranslation("hello_world")}</p></InfoPageComponent>);
} }
} }
Loading…
Cancel
Save