Add a constant for the default locale (#1023)

This commit is contained in:
Jon Koops 2021-08-19 15:56:00 +02:00 committed by GitHub
parent 5467d8acb5
commit c5abf8349e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

View file

@ -2,7 +2,7 @@ import type WhoAmIRepresentation from "keycloak-admin/lib/defs/whoAmIRepresentat
import type { AccessType } from "keycloak-admin/lib/defs/whoAmIRepresentation";
import React, { FunctionComponent, useState } from "react";
import environment from "../../environment";
import i18n from "../../i18n";
import i18n, { DEFAULT_LOCALE } from "../../i18n";
import useRequiredContext from "../../utils/useRequiredContext";
import { useAdminClient, useFetch } from "../auth/AdminClient";
@ -21,6 +21,10 @@ export class WhoAmI {
return this.me.displayName;
}
public getLocale() {
return this.me?.locale ?? DEFAULT_LOCALE;
}
public getRealm() {
return this.me?.realm ?? "";
}

View file

@ -25,6 +25,8 @@ import userFederationHelp from "./user-federation/help";
import identityProviders from "./identity-providers/messages";
import identityProvidersHelp from "./identity-providers/help";
export const DEFAULT_LOCALE = "en";
const initOptions = {
defaultNS: "common",
resources: {
@ -55,8 +57,8 @@ const initOptions = {
...userFederationHelp,
},
},
lng: "en",
fallbackLng: "en",
lng: DEFAULT_LOCALE,
fallbackLng: DEFAULT_LOCALE,
interpolation: {
escapeValue: false,

View file

@ -27,6 +27,7 @@ import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { AddMessageBundleModal } from "./AddMessageBundleModal";
import { useAlerts } from "../components/alert/Alerts";
import { useRealm } from "../context/realm-context/RealmContext";
import { DEFAULT_LOCALE } from "../i18n";
type LocalizationTabProps = {
save: (realm: RealmRepresentation) => void;
@ -54,7 +55,7 @@ export const LocalizationTab = ({
const [supportedLocalesOpen, setSupportedLocalesOpen] = useState(false);
const [defaultLocaleOpen, setDefaultLocaleOpen] = useState(false);
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
const [selectMenuLocale, setSelectMenuLocale] = useState("en");
const [selectMenuLocale, setSelectMenuLocale] = useState(DEFAULT_LOCALE);
const { getValues, control, handleSubmit, formState } = useFormContext();
const [valueSelected, setValueSelected] = useState(false);
@ -81,7 +82,8 @@ export const LocalizationTab = ({
try {
const result = await adminClient.realms.getRealmLocalizationTexts({
realm: realm.realm!,
selectedLocale: selectMenuLocale || getValues("defaultLocale") || "en",
selectedLocale:
selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE,
});
return Object.entries(result);
@ -144,7 +146,7 @@ export const LocalizationTab = ({
{
realm: currentRealm!,
selectedLocale:
selectMenuLocale || getValues("defaultLocale") || "en",
selectMenuLocale || getValues("defaultLocale") || DEFAULT_LOCALE,
key: pair.key,
},
pair.value
@ -286,7 +288,7 @@ export const LocalizationTab = ({
: realm.defaultLocale !== ""
? t(
`allSupportedLocales.${
realm.defaultLocale || "en"
realm.defaultLocale || DEFAULT_LOCALE
}`
)
: t("placeholderText")
@ -359,7 +361,7 @@ export const LocalizationTab = ({
selectMenuValueSelected
? t(`allSupportedLocales.${selectMenuLocale}`)
: realm.defaultLocale !== ""
? t(`allSupportedLocales.${"en"}`)
? t(`allSupportedLocales.${DEFAULT_LOCALE}`)
: t("placeholderText")
}
>