Skip to content

Commit

Permalink
feat: use i18n in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Dec 16, 2024
1 parent 1e7acf2 commit 6387065
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions frontend/src/dictionaries/i18n
26 changes: 26 additions & 0 deletions frontend/src/dictionaries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Resource } from "i18next";

import de from "./i18n/de.json";
import en from "./i18n/en.json";
import es from "./i18n/es.json";
import fr from "./i18n/fr.json";
import it from "./i18n/it.json";
import pt from "./i18n/pt.json";

const dictionaries = {
de,
en,
es,
fr,
it,
pt,
};

const resources: Resource = {};
Object.keys(dictionaries).forEach((k: string) => {
resources[k] = {
magneto: dictionaries[k],
};
});

export { resources };
35 changes: 22 additions & 13 deletions frontend/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@ import LanguageDetector from "i18next-browser-languagedetector";
import Backend from "i18next-http-backend";
import { initReactI18next } from "react-i18next";

import { isEntNgContext } from "./utils/context.utils";
import { resources } from "~/dictionaries";

i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
backend: {
loadPath: (_lngs: string[], namespaces: string[]) => {
const urls = namespaces.map((namespace: string) => {
if (namespace === "common") {
return `/i18n`;
...(() => {
return isEntNgContext
? {
backend: {
loadPath: (_lngs: string[], namespaces: string[]) => {
const urls = namespaces.map((namespace: string) => {
if (namespace === "common") {
return `/i18n`;
}
return `/${namespace}/i18n`;
});
return urls;
},
parse: function (data: string) {
return JSON.parse(data);
},
},
}
return `/${namespace}/i18n`;
});
return urls;
},
parse: function (data: string) {
return JSON.parse(data);
},
},
: { resources };
})(),
defaultNS: "common",
// you can add name of the app directly in the ns array
ns: ["common", "magneto"],
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/utils/context.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ const OdeClientProvider = isEntNgContext

const ThemeProvider = isEntNgContext ? EdificeThemeProvider : OpenThemeProvider;

export { Layout, useOdeClient, OdeClientProvider, ThemeProvider };
export { isEntNgContext, Layout, useOdeClient, OdeClientProvider, ThemeProvider };

0 comments on commit 6387065

Please sign in to comment.