2022-04-27 15:29:57 +00:00
|
|
|
import i18n, { InitOptions, TOptions } from "i18next";
|
|
|
|
import HttpBackend, { LoadPathOption } from "i18next-http-backend";
|
2020-09-01 14:51:59 +00:00
|
|
|
import { initReactI18next } from "react-i18next";
|
2022-04-27 15:29:57 +00:00
|
|
|
import type KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2022-04-27 15:29:57 +00:00
|
|
|
import environment from "./environment";
|
2021-11-08 09:46:03 +00:00
|
|
|
|
2021-08-19 13:56:00 +00:00
|
|
|
export const DEFAULT_LOCALE = "en";
|
|
|
|
|
2022-04-27 15:29:57 +00:00
|
|
|
export async function initI18n(adminClient: KeycloakAdminClient) {
|
|
|
|
const options = await initOptions(adminClient);
|
|
|
|
await i18n.init(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
const initOptions = async (
|
|
|
|
adminClient: KeycloakAdminClient
|
|
|
|
): Promise<InitOptions> => {
|
|
|
|
const constructLoadPath: LoadPathOption = (_, namespaces) => {
|
|
|
|
if (namespaces[0] === "overrides") {
|
|
|
|
return `/admin/realms/${adminClient.realmName}/localization/{{lng}}`;
|
|
|
|
} else {
|
|
|
|
return `${environment.resourceUrl}/resources/{{lng}}/{{ns}}.json`;
|
|
|
|
}
|
|
|
|
};
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2022-04-27 15:29:57 +00:00
|
|
|
return {
|
|
|
|
defaultNS: "common",
|
|
|
|
fallbackLng: DEFAULT_LOCALE,
|
|
|
|
preload: [DEFAULT_LOCALE],
|
|
|
|
ns: [
|
|
|
|
"common",
|
|
|
|
"common-help",
|
|
|
|
"dashboard",
|
|
|
|
"clients",
|
|
|
|
"clients-help",
|
|
|
|
"client-scopes",
|
|
|
|
"client-scopes-help",
|
|
|
|
"groups",
|
|
|
|
"realm",
|
|
|
|
"roles",
|
|
|
|
"users",
|
|
|
|
"users-help",
|
|
|
|
"sessions",
|
|
|
|
"events",
|
|
|
|
"realm-settings",
|
|
|
|
"realm-settings-help",
|
|
|
|
"authentication",
|
|
|
|
"authentication-help",
|
|
|
|
"user-federation",
|
|
|
|
"user-federation-help",
|
|
|
|
"identity-providers",
|
|
|
|
"identity-providers-help",
|
|
|
|
"dynamic",
|
|
|
|
"overrides",
|
|
|
|
],
|
|
|
|
interpolation: {
|
|
|
|
escapeValue: false,
|
|
|
|
},
|
|
|
|
postProcess: ["overrideProcessor"],
|
|
|
|
backend: {
|
|
|
|
loadPath: constructLoadPath,
|
|
|
|
customHeaders: {
|
|
|
|
Authorization: `Bearer ${await adminClient.getAccessToken()}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-08-04 12:59:41 +00:00
|
|
|
};
|
|
|
|
|
2022-04-27 15:29:57 +00:00
|
|
|
const configuredI18n = i18n
|
|
|
|
.use({
|
|
|
|
type: "postProcessor",
|
|
|
|
name: "overrideProcessor",
|
|
|
|
process: function (
|
|
|
|
value: string,
|
|
|
|
key: string,
|
|
|
|
_: TOptions,
|
|
|
|
translator: any
|
|
|
|
) {
|
|
|
|
const override: string =
|
2022-05-18 08:51:29 +00:00
|
|
|
translator.resourceStore.data[translator.language].overrides?.[key];
|
2022-04-27 15:29:57 +00:00
|
|
|
return override || value;
|
|
|
|
},
|
|
|
|
})
|
2020-08-04 12:59:41 +00:00
|
|
|
.use(initReactI18next)
|
2022-04-27 15:29:57 +00:00
|
|
|
.use(HttpBackend);
|
2020-08-04 12:59:41 +00:00
|
|
|
|
2022-04-27 15:29:57 +00:00
|
|
|
export default configuredI18n;
|