Skip to content

Commit

Permalink
Backport to make improvements for translations (keycloak#34878)
Browse files Browse the repository at this point in the history
Signed-off-by: Agnieszka Gancarczyk <[email protected]>
  • Loading branch information
agagancarczyk authored Nov 13, 2024
1 parent 90bd366 commit c15bc7c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3268,3 +3268,4 @@ groupDuplicated=Group duplicated
duplicateAGroup=Duplicate group
couldNotFetchClientRoleMappings=Could not fetch client role mappings\: {{error}}
duplicateGroupWarning=Duplication of groups with a large number of subgroups is not supported. Please ensure that the group you are duplicating does not have a large number of subgroups.
errorSavingTranslations=Error saving translations\: '{{error}}'
95 changes: 47 additions & 48 deletions js/apps/admin-ui/src/realm-settings/NewAttributeSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type {
UserProfileAttribute,
UserProfileConfig,
Expand Down Expand Up @@ -61,6 +62,7 @@ type UserProfileAttributeFormFields = Omit<
annotations: IndexedAnnotations[];
hasSelector: boolean;
hasRequiredScopes: boolean;
translations?: TranslationForm[];
};

type Attribute = {
Expand Down Expand Up @@ -172,7 +174,8 @@ export default function NewAttributeSettings() {

useFetch(
async () => {
const translationsToSave: any[] = [];
const translationsToSave: Translations[] = [];

await Promise.all(
combinedLocales.map(async (selectedLocale) => {
try {
Expand All @@ -183,55 +186,50 @@ export default function NewAttributeSettings() {
});

const formData = form.getValues();
const formattedKey = formData.displayName?.substring(
2,
formData.displayName.length - 1,
);
const filteredTranslations: Array<{
locale: string;
value: string;
}> = [];
const allTranslations = Object.entries(translations).map(
([key, value]) => ({
key,
const formattedKey =
formData.displayName?.substring(
2,
formData.displayName.length - 1,
) || "";

const filteredTranslations: TranslationForm[] = Object.entries(
translations,
)
.filter(([key]) => key === formattedKey)
.map(([_, value]) => ({
locale: selectedLocale,
value,
}),
);
}));

allTranslations.forEach((translation) => {
if (translation.key === formattedKey) {
filteredTranslations.push({
locale: selectedLocale,
value: translation.value,
});
}
});

const translationToSave: any = {
key: formattedKey,
translations: filteredTranslations,
};

translationsToSave.push(translationToSave);
if (filteredTranslations.length > 0) {
translationsToSave.push({
key: formattedKey,
translations: filteredTranslations,
});
}
} catch (error) {
console.error(
`Error fetching translations for ${selectedLocale}:`,
error,
);
addError("errorSavingTranslations", error);
}
}),
);

return translationsToSave;
},
(translationsToSaveData) => {
setTranslationsData(() => ({
key: translationsToSaveData[0].key,
translations: translationsToSaveData.flatMap(
(translationData) => translationData.translations,
),
}));
(translationsToSave) => {
if (translationsToSave && translationsToSave.length > 0) {
const allTranslations = translationsToSave.flatMap(
(translation) => translation.translations,
);

setTranslationsData({
key: translationsToSave[0].key,
translations: allTranslations,
});

form.setValue("translations", allTranslations);
}
},
[combinedLocales],
[combinedLocales, realmName, form],
);

useFetch(
Expand Down Expand Up @@ -282,8 +280,9 @@ export default function NewAttributeSettings() {

const saveTranslations = async () => {
try {
const nonEmptyTranslations = translationsData.translations.map(
async (translation) => {
const nonEmptyTranslations = translationsData.translations
.filter((translation) => translation.value.trim() !== "")
.map(async (translation) => {
try {
await adminClient.realms.addLocalization(
{
Expand All @@ -293,11 +292,11 @@ export default function NewAttributeSettings() {
},
translation.value,
);
} catch {
console.error(`Error saving translation for ${translation.locale}`);
} catch (error) {
addError(t("errorSavingTranslations"), error);
}
},
);
});

await Promise.all(nonEmptyTranslations);
} catch (error) {
console.error(`Error saving translations: ${error}`);
Expand Down Expand Up @@ -377,7 +376,7 @@ export default function NewAttributeSettings() {
(translation) => translation.value.trim() !== "",
);

if (!hasNonEmptyTranslations && !formFields.displayName) {
if (!hasNonEmptyTranslations) {
addError("createAttributeError", t("translationError"));
return;
}
Expand Down
Loading

0 comments on commit c15bc7c

Please sign in to comment.