mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 11:09:29 +00:00
Use defined synonyms in ElementTypeNameGenerator
This commit is contained in:
parent
96418db4e9
commit
e95197b069
2 changed files with 68 additions and 6 deletions
|
|
@ -34,6 +34,7 @@ use App\Entity\PriceInformations\Pricedetail;
|
||||||
use App\Entity\ProjectSystem\Project;
|
use App\Entity\ProjectSystem\Project;
|
||||||
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
use App\Entity\ProjectSystem\ProjectBOMEntry;
|
||||||
use App\Exceptions\EntityNotSupportedException;
|
use App\Exceptions\EntityNotSupportedException;
|
||||||
|
use App\Settings\SystemSettings\DataSourceSynonymsSettings;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -42,7 +43,11 @@ use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
final readonly class ElementTypeNameGenerator
|
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);
|
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,
|
* Gets a localized label for the type of the entity. If user defined synonyms are defined,
|
||||||
* these are used instead of the default labels.
|
* these are used instead of the default labels.
|
||||||
|
|
@ -75,7 +106,8 @@ final readonly class ElementTypeNameGenerator
|
||||||
{
|
{
|
||||||
$type = ElementTypes::fromValue($entity);
|
$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);
|
$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
|
} else { //Target does not have a name
|
||||||
$tmp = sprintf(
|
$tmp = sprintf(
|
||||||
'<i>%s</i>: %s',
|
'<i>%s</i>: %s',
|
||||||
$this->getLocalizedTypeLabel($entity),
|
$this->typeLabel($entity),
|
||||||
$entity->getID()
|
$entity->getID()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -181,7 +214,7 @@ final readonly class ElementTypeNameGenerator
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'<i>%s</i>: %s [%s]',
|
'<i>%s</i>: %s [%s]',
|
||||||
$this->getLocalizedTypeLabel($class),
|
$this->typeLabel($class),
|
||||||
$id,
|
$id,
|
||||||
$this->translator->trans('log.target_deleted')
|
$this->translator->trans('log.target_deleted')
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -66,8 +66,37 @@ class DataSourceSynonymsSettings
|
||||||
*/
|
*/
|
||||||
public array $customTypeLabels = [];
|
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]);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue