2023-12-12 10:59:59 +00:00
|
|
|
import { LanguageDetectorModule, createInstance } from "i18next";
|
2022-10-10 10:14:19 +00:00
|
|
|
import HttpBackend from "i18next-http-backend";
|
|
|
|
import { initReactI18next } from "react-i18next";
|
2024-06-06 06:36:46 +00:00
|
|
|
|
|
|
|
import { environment } from "./environment";
|
2022-11-03 14:06:26 +00:00
|
|
|
import { joinPath } from "./utils/joinPath";
|
2022-10-10 10:14:19 +00:00
|
|
|
|
|
|
|
const DEFAULT_LOCALE = "en";
|
|
|
|
|
2023-11-16 09:25:14 +00:00
|
|
|
type KeyValue = { key: string; value: string };
|
|
|
|
|
2023-09-22 13:57:19 +00:00
|
|
|
// This type is aliased to any, so that we can find all the places where we use it.
|
|
|
|
// In the future all casts to this type should be removed from the code, so
|
|
|
|
// that we can have a proper type-safe translation function.
|
|
|
|
export type TFuncKey = any;
|
2023-06-19 14:59:21 +00:00
|
|
|
|
2023-12-12 10:59:59 +00:00
|
|
|
export const keycloakLanguageDetector: LanguageDetectorModule = {
|
|
|
|
type: "languageDetector",
|
|
|
|
|
|
|
|
detect() {
|
|
|
|
return environment.locale;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-10 10:14:19 +00:00
|
|
|
export const i18n = createInstance({
|
|
|
|
fallbackLng: DEFAULT_LOCALE,
|
|
|
|
interpolation: {
|
|
|
|
escapeValue: false,
|
|
|
|
},
|
|
|
|
backend: {
|
2023-11-16 09:25:14 +00:00
|
|
|
loadPath: joinPath(
|
2024-06-19 13:21:53 +00:00
|
|
|
environment.serverBaseUrl,
|
2023-11-16 09:25:14 +00:00
|
|
|
`resources/${environment.realm}/account/{{lng}}`,
|
|
|
|
),
|
|
|
|
parse: (data: string) => {
|
|
|
|
const messages = JSON.parse(data);
|
|
|
|
|
|
|
|
const result: Record<string, string> = {};
|
|
|
|
messages.forEach((v: KeyValue) => (result[v.key] = v.value));
|
|
|
|
return result;
|
|
|
|
},
|
2022-10-10 10:14:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
i18n.use(HttpBackend);
|
2023-12-12 10:59:59 +00:00
|
|
|
i18n.use(keycloakLanguageDetector);
|
2022-10-10 10:14:19 +00:00
|
|
|
i18n.use(initReactI18next);
|