11import KeycloakAdminClient from "@keycloak/keycloak-admin-client" ;
2+ import RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation" ;
23import { FormErrorText } from "@keycloak/keycloak-ui-shared" ;
34import {
45 Alert ,
@@ -9,6 +10,7 @@ import {
910 TextInput ,
1011} from "@patternfly/react-core" ;
1112import { GlobeRouteIcon } from "@patternfly/react-icons" ;
13+ import { TFunction } from "i18next" ;
1214import { useEffect } from "react" ;
1315import { useFormContext , useWatch } from "react-hook-form" ;
1416import { 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+
7084export 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