From e95197b0699c0529b9bf2653f71fb55a96a47e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 10 Nov 2025 00:23:00 +0100 Subject: [PATCH] Use defined synonyms in ElementTypeNameGenerator --- src/Services/ElementTypeNameGenerator.php | 43 ++++++++++++++++--- .../DataSourceSynonymsSettings.php | 31 ++++++++++++- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/Services/ElementTypeNameGenerator.php b/src/Services/ElementTypeNameGenerator.php index d4b039c8..2d8bb106 100644 --- a/src/Services/ElementTypeNameGenerator.php +++ b/src/Services/ElementTypeNameGenerator.php @@ -34,6 +34,7 @@ use App\Entity\PriceInformations\Pricedetail; use App\Entity\ProjectSystem\Project; use App\Entity\ProjectSystem\ProjectBOMEntry; use App\Exceptions\EntityNotSupportedException; +use App\Settings\SystemSettings\DataSourceSynonymsSettings; use Symfony\Contracts\Translation\TranslatorInterface; /** @@ -42,7 +43,11 @@ use Symfony\Contracts\Translation\TranslatorInterface; final readonly class ElementTypeNameGenerator { - public function __construct(private TranslatorInterface $translator, private EntityURLGenerator $entityURLGenerator) + public function __construct( + private TranslatorInterface $translator, + private EntityURLGenerator $entityURLGenerator, + private DataSourceSynonymsSettings $synonymsSettings, + ) { } @@ -64,6 +69,32 @@ final readonly class ElementTypeNameGenerator return $this->typeLabel($entity); } + private function resolveSynonymLabel(ElementTypes $type, ?string $locale, bool $plural): ?string + { + $locale ??= $this->translator->getLocale(); + + if ($this->synonymsSettings->isSynonymDefinedForType($type)) { + if ($plural) { + $syn = $this->synonymsSettings->getSingularSynonymForType($type, $locale); + } else { + $syn = $this->synonymsSettings->getPluralSynonymForType($type, $locale); + } + + if ($syn === null) { + //Try to fall back to english + if ($plural) { + $syn = $this->synonymsSettings->getSingularSynonymForType($type, 'en'); + } else { + $syn = $this->synonymsSettings->getPluralSynonymForType($type, 'en'); + } + } + + return $syn; + } + + return null; + } + /** * Gets a localized label for the type of the entity. If user defined synonyms are defined, * these are used instead of the default labels. @@ -75,7 +106,8 @@ final readonly class ElementTypeNameGenerator { $type = ElementTypes::fromValue($entity); - return $this->translator->trans($type->getDefaultLabelKey(), locale: $locale); + return $this->resolveSynonymLabel($type, $locale, false) + ?? $this->translator->trans($type->getDefaultLabelKey(), locale: $locale); } /** @@ -88,7 +120,8 @@ final readonly class ElementTypeNameGenerator { $type = ElementTypes::fromValue($entity); - return $this->translator->trans($type->getDefaultPluralLabelKey(), locale: $locale); + return $this->resolveSynonymLabel($type, $locale, true) + ?? $this->translator->trans($type->getDefaultPluralLabelKey(), locale: $locale); } @@ -137,7 +170,7 @@ final readonly class ElementTypeNameGenerator } else { //Target does not have a name $tmp = sprintf( '%s: %s', - $this->getLocalizedTypeLabel($entity), + $this->typeLabel($entity), $entity->getID() ); } @@ -181,7 +214,7 @@ final readonly class ElementTypeNameGenerator { return sprintf( '%s: %s [%s]', - $this->getLocalizedTypeLabel($class), + $this->typeLabel($class), $id, $this->translator->trans('log.target_deleted') ); diff --git a/src/Settings/SystemSettings/DataSourceSynonymsSettings.php b/src/Settings/SystemSettings/DataSourceSynonymsSettings.php index 9ebe557d..03cb75e3 100644 --- a/src/Settings/SystemSettings/DataSourceSynonymsSettings.php +++ b/src/Settings/SystemSettings/DataSourceSynonymsSettings.php @@ -66,8 +66,37 @@ class DataSourceSynonymsSettings */ public array $customTypeLabels = []; - public function isCustomLabelDefinedForType(ElementTypes $type): bool + /** + * Checks if there is any synonym defined for the given type (no matter which language). + * @param ElementTypes $type + * @return bool + */ + public function isSynonymDefinedForType(ElementTypes $type): bool { return isset($this->customTypeLabels[$type->value]); } + + /** + * Returns the singular synonym for the given type and locale, or null if none is defined. + * @param ElementTypes $type + * @param string $locale + * @return string|null + */ + public function getSingularSynonymForType(ElementTypes $type, string $locale): ?string + { + return $this->customTypeLabels[$type->value][$locale]['singular'] ?? null; + } + + /** + * Returns the plural synonym for the given type and locale, or null if none is defined. + * @param ElementTypes $type + * @param string|null $locale + * @return string|null + */ + public function getPluralSynonymForType(ElementTypes $type, ?string $locale): ?string + { + return $this->customTypeLabels[$type->value][$locale]['plural'] + ?? $this->customTypeLabels[$type->value][$locale]['singular'] + ?? null; + } }