mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-06 11:09:29 +00:00
Register synonyms for element types as global translation parameters
This commit is contained in:
parent
c372109a2f
commit
d0a65cbb94
2 changed files with 94 additions and 1 deletions
|
|
@ -64,7 +64,7 @@ class SettingsController extends AbstractController
|
||||||
$this->settingsManager->save($settings);
|
$this->settingsManager->save($settings);
|
||||||
|
|
||||||
//It might be possible, that the tree settings have changed, so clear the cache
|
//It might be possible, that the tree settings have changed, so clear the cache
|
||||||
$cache->invalidateTags(['tree_tools', 'tree_treeview', 'sidebar_tree_update']);
|
$cache->invalidateTags(['tree_tools', 'tree_treeview', 'sidebar_tree_update', 'synonyms']);
|
||||||
|
|
||||||
$this->addFlash('success', t('settings.flash.saved'));
|
$this->addFlash('success', t('settings.flash.saved'));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\EventListener;
|
||||||
|
|
||||||
|
use App\Services\ElementTypeNameGenerator;
|
||||||
|
use App\Services\ElementTypes;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
|
||||||
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\Translation\Translator;
|
||||||
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
use Symfony\Contracts\Cache\TagAwareCacheInterface;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
#[AsEventListener]
|
||||||
|
readonly class RegisterSynonymsAsTranslationParameters
|
||||||
|
{
|
||||||
|
private Translator $translator;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
#[Autowire(service: 'translator.default')] TranslatorInterface $translator,
|
||||||
|
private TagAwareCacheInterface $cache,
|
||||||
|
private ElementTypeNameGenerator $typeNameGenerator)
|
||||||
|
{
|
||||||
|
if (!$translator instanceof Translator) {
|
||||||
|
throw new \RuntimeException('Translator must be an instance of Symfony\Component\Translation\Translator or this listener cannot be used.');
|
||||||
|
}
|
||||||
|
$this->translator = $translator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSynonymPlaceholders(): array
|
||||||
|
{
|
||||||
|
return $this->cache->get('partdb_synonym_placeholders', function (ItemInterface $item) {
|
||||||
|
$item->tag('synonyms');
|
||||||
|
|
||||||
|
|
||||||
|
$placeholders = [];
|
||||||
|
|
||||||
|
//Generate a placeholder for each element type
|
||||||
|
foreach (ElementTypes::cases() as $elementType) {
|
||||||
|
//We have a placeholder for singular
|
||||||
|
$placeholders['{' . $elementType->value . '}'] = $this->typeNameGenerator->typeLabel($elementType);
|
||||||
|
//We have a placeholder for plural
|
||||||
|
$placeholders['{{' . $elementType->value . '}}'] = $this->typeNameGenerator->typeLabelPlural($elementType);
|
||||||
|
|
||||||
|
//And we have lowercase versions for both
|
||||||
|
$placeholders['[' . $elementType->value . ']'] = mb_strtolower($this->typeNameGenerator->typeLabel($elementType));
|
||||||
|
$placeholders['[' . $elementType->value . ']'] = mb_strtolower($this->typeNameGenerator->typeLabelPlural($elementType));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $placeholders;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __invoke(RequestEvent $event): void
|
||||||
|
{
|
||||||
|
//If we already added the parameters, skip adding them again
|
||||||
|
if (isset($this->translator->getGlobalParameters()['@@partdb_synonyms_registered@@'])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Register all placeholders for synonyms
|
||||||
|
$placeholders = $this->getSynonymPlaceholders();
|
||||||
|
foreach ($placeholders as $key => $value) {
|
||||||
|
$this->translator->addGlobalParameter($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Register the marker parameter to avoid double registration
|
||||||
|
$this->translator->addGlobalParameter('@@partdb_synonyms_registered@@', 'registered');
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue