Skip to content

Commit 7a8d181

Browse files
authored
small refactor to better understand (#37291)
fixes: #37268 Signed-off-by: Erik Jan de Wit <[email protected]>
1 parent 5a8c014 commit 7a8d181

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

js/apps/admin-ui/src/realm-settings/user-profile/attribute/AddTranslationsDialog.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ export const AddTranslationsDialog = ({
139139
spaceItems={{ default: "spaceItemsNone" }}
140140
>
141141
<FlexItem>
142-
<Trans i18nKey="addTranslationsModalTitle" values={{ fieldName }}>
142+
<Trans
143+
i18nKey="addTranslationsModalTitle"
144+
values={{ fieldName: t(fieldName) }}
145+
>
143146
You are able to translate the fieldName based on your locale or
144147
<strong>location</strong>
145148
</Trans>

js/apps/admin-ui/src/realm-settings/user-profile/attribute/TranslatableField.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import KeycloakAdminClient from "@keycloak/keycloak-admin-client";
2+
import RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
23
import { FormErrorText } from "@keycloak/keycloak-ui-shared";
34
import {
45
Alert,
@@ -9,6 +10,7 @@ import {
910
TextInput,
1011
} from "@patternfly/react-core";
1112
import { GlobeRouteIcon } from "@patternfly/react-icons";
13+
import { TFunction } from "i18next";
1214
import { useEffect } from "react";
1315
import { useFormContext, useWatch } from "react-hook-form";
1416
import { Trans, useTranslation } from "react-i18next";
@@ -67,6 +69,18 @@ type TranslatableFieldProps = {
6769
fieldName: string;
6870
};
6971

72+
function hasTranslation(value: string, t: TFunction) {
73+
return t(value) === value && value !== "";
74+
}
75+
76+
function isTranslationRequired(
77+
value: string,
78+
t: TFunction,
79+
realm?: RealmRepresentation,
80+
) {
81+
return realm?.internationalizationEnabled && hasTranslation(value, t);
82+
}
83+
7084
export const TranslatableField = ({
7185
attributeName,
7286
prefix,
@@ -84,16 +98,14 @@ export const TranslatableField = ({
8498
const requiredTranslationName = `${translationPrefix}.0.value`;
8599

86100
useEffect(() => {
87-
if (realm?.internationalizationEnabled) {
88-
if (t(value) === value && value !== "") {
89-
setValue(fieldName, `\${${prefix}.${value}}`);
90-
}
101+
if (isTranslationRequired(value, t, realm)) {
102+
setValue(fieldName, `\${${prefix}.${value}}`);
91103
}
92104
}, [value]);
93105

94106
return (
95107
<>
96-
{realm?.internationalizationEnabled && (
108+
{isTranslationRequired(value, t, realm) && (
97109
<input
98110
type="hidden"
99111
data-testid="requiredTranslationName"
@@ -139,7 +151,7 @@ export const TranslatableField = ({
139151
title={
140152
<Trans
141153
i18nKey="addTranslationsModalSubTitle"
142-
values={{ fieldName }}
154+
values={{ fieldName: t(fieldName) }}
143155
>
144156
You are able to translate the fieldName based on your locale or
145157
<strong>location</strong>

0 commit comments

Comments
 (0)