From a2bd105f01952f3dd50c04cb064a7d9d77045b80 Mon Sep 17 00:00:00 2001 From: IanM Date: Fri, 17 Jan 2025 10:00:00 +0000 Subject: [PATCH] feat: extensibility of merged strings --- src/Api/Controllers/MergedIndexController.php | 70 +++-------------- src/Repositories/MergedStringsRepository.php | 76 +++++++++++++++++++ 2 files changed, 86 insertions(+), 60 deletions(-) create mode 100644 src/Repositories/MergedStringsRepository.php diff --git a/src/Api/Controllers/MergedIndexController.php b/src/Api/Controllers/MergedIndexController.php index 330404b..4cc5f90 100644 --- a/src/Api/Controllers/MergedIndexController.php +++ b/src/Api/Controllers/MergedIndexController.php @@ -6,12 +6,8 @@ use Flarum\Http\RequestUtil; use FoF\Linguist\Api\Serializers\StringKeySerializer; use FoF\Linguist\Repositories\DefaultStringsRepository; -use FoF\Linguist\Repositories\StringRepository; -use FoF\Linguist\TextString; -use Illuminate\Support\Arr; -use Illuminate\Support\Str; +use FoF\Linguist\Repositories\MergedStringsRepository; use Psr\Http\Message\ServerRequestInterface; -use Symfony\Contracts\Translation\TranslatorInterface; use Tobscure\JsonApi\Document; class MergedIndexController extends AbstractListController @@ -19,28 +15,19 @@ class MergedIndexController extends AbstractListController public $serializer = StringKeySerializer::class; /** - * @var DefaultStringsRepository - */ - protected $defaultStrings; - - /** - * @var StringRepository + * @var MergedStringsRepository */ - protected $strings; + protected $mergedStrings; /** - * @var TranslatorInterface + * @var DefaultStringsRepository */ - protected $translator; + protected $defaultStrings; - public function __construct( - DefaultStringsRepository $defaultStrings, - StringRepository $strings, - TranslatorInterface $translator - ) { + public function __construct(MergedStringsRepository $mergedStrings, DefaultStringsRepository $defaultStrings) + { + $this->mergedStrings = $mergedStrings; $this->defaultStrings = $defaultStrings; - $this->strings = $strings; - $this->translator = $translator; } protected function data(ServerRequestInterface $request, Document $document) @@ -50,44 +37,7 @@ protected function data(ServerRequestInterface $request, Document $document) // Extract filters from the request. $filters = $this->extractFilter($request); $filter = $this->defaultStrings->getPrefix($filters); - - // Retrieve all translations from the default repository and returns a Collection. - // @example - // array:2 [▼ - // "key" => "acme-foobar.forum.example_string" - // "locales" => array:4 [▼ - // "en" => "${count} more ${ count === 1 ? `question` : `questions` }" - // "de" => "${count} weitere ${ count === 1 ? `Frage` : `Fragen` }" - // "it" => "${count} altre ${ count === 1 ? `domanda` : `domande` }" - // "fr" => "${count} autres ${ count === 1 ? `question` : `questions` }" - // ] - // ] - $all = $this->defaultStrings->allTranslations($filter); - $allKeys = $all->pluck('key'); - - $modified = $this->strings->getByKeys($allKeys->toArray()); - - foreach ($modified as $textString) { - $updates = []; - $locale = $textString->locale; - $translation = $textString->value; - - if (Str::startsWith($translation, '=>')) { - $key = trim(Str::after($translation, '=>')); - - if (empty($key)) { - continue; - } - $translation = $this->translator->trans($key, [], null, $locale); - } - - $updates[$locale] = $translation; - - $locales = array_merge($all[$textString->key]['locales'], $updates); - - $all->put($textString->key, ['key' => $textString->key, 'locales' => $locales]); - } - - return $all; + + return $this->mergedStrings->getTranslations($filter); } } diff --git a/src/Repositories/MergedStringsRepository.php b/src/Repositories/MergedStringsRepository.php new file mode 100644 index 0000000..cd36b69 --- /dev/null +++ b/src/Repositories/MergedStringsRepository.php @@ -0,0 +1,76 @@ +defaultStrings = $defaultStrings; + $this->strings = $strings; + $this->translator = $translator; + } + + public function getTranslations(?string $filter) + { + // Retrieve all translations from the default repository and returns a Collection. + // @example + // array:2 [▼ + // "key" => "acme-foobar.forum.example_string" + // "locales" => array:4 [▼ + // "en" => "${count} more ${ count === 1 ? `question` : `questions` }" + // "de" => "${count} weitere ${ count === 1 ? `Frage` : `Fragen` }" + // "it" => "${count} altre ${ count === 1 ? `domanda` : `domande` }" + // "fr" => "${count} autres ${ count === 1 ? `question` : `questions` }" + // ] + // ] + $all = $this->defaultStrings->allTranslations($filter); + $allKeys = $all->pluck('key'); + + $modified = $this->strings->getByKeys($allKeys->toArray()); + + foreach ($modified as $textString) { + $updates = []; + $locale = $textString->locale; + $translation = $textString->value; + + if (Str::startsWith($translation, '=>')) { + $key = trim(Str::after($translation, '=>')); + + if (empty($key)) { + continue; + } + $translation = $this->translator->trans($key, [], null, $locale); + } + + $updates[$locale] = $translation; + + $locales = array_merge($all[$textString->key]['locales'], $updates); + + $all->put($textString->key, ['key' => $textString->key, 'locales' => $locales]); + } + + return $all; + } +}