Skip to content

Commit

Permalink
fix: refs not translated correctly, improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 15, 2025
1 parent 5c905c7 commit 1b7a1b8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/Api/Controllers/MergedIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use FoF\Linguist\Repositories\StringRepository;
use FoF\Linguist\TextString;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Tobscure\JsonApi\Document;

class MergedIndexController extends AbstractListController
Expand All @@ -26,12 +28,19 @@ class MergedIndexController extends AbstractListController
*/
protected $strings;

/**
* @var TranslatorInterface
*/
protected $translator;

public function __construct(
DefaultStringsRepository $defaultStrings,
StringRepository $strings
StringRepository $strings,
TranslatorInterface $translator
) {
$this->defaultStrings = $defaultStrings;
$this->strings = $strings;
$this->translator = $translator;
}

protected function data(ServerRequestInterface $request, Document $document)
Expand All @@ -45,7 +54,7 @@ protected function data(ServerRequestInterface $request, Document $document)
// Retrieve all translations from the default repository and returns a Collection.
// @example
// array:2 [▼
// "key" => "glowingblue-integrable.ui.discussions.show_more"
// "key" => "acme-foobar.forum.example_string"
// "locales" => array:4 [▼
// "en" => "${count} more ${ count === 1 ? `question` : `questions` }"
// "de" => "${count} weitere ${ count === 1 ? `Frage` : `Fragen` }"
Expand All @@ -54,23 +63,29 @@ protected function data(ServerRequestInterface $request, Document $document)
// ]
// ]
$all = $this->defaultStrings->allTranslations($filter);
$allKeys = $all->pluck('key');

foreach ($all as $translationKey) {
$key = Arr::get($translationKey, 'key');
$resultsForKey = $this->strings->getByKey($key);
$modified = $this->strings->getByKeys($allKeys->toArray());

foreach ($modified as $textString) {
$updates = [];
$locale = $textString->locale;
$translation = $textString->value;

foreach ($resultsForKey as $textString) {
/** @var TextString $textString */
$locale = $textString->locale;
$translation = $textString->value;
if (Str::startsWith($translation, '=>')) {
$key = trim(Str::after($translation, '=>'));

$updates[$locale] = $translation;
if (empty($key)) {
continue;
}
$translation = $this->translator->trans($key, [], null, $locale);
}

$locales = array_merge($translationKey['locales'], $updates);
$all->put($key, ['key' => $key, 'locales' => $locales]);
$updates[$locale] = $translation;

$locales = array_merge($all[$textString->key]['locales'], $updates);

$all->put($textString->key, ['key' => $textString->key, 'locales' => $locales]);
}

return $all;
Expand Down
10 changes: 10 additions & 0 deletions src/Repositories/StringRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FoF\Linguist\Repositories;

use Flarum\Database\Eloquent\Collection;
use FoF\Linguist\TextString;
use FoF\Linguist\Validators\StringValidator;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -51,6 +52,15 @@ public function getByKey($key)
return $this->query()->where('key', $key)->get();
}

/**
* @param array $keys
* @return Collection<TextString>
*/
public function getByKeys(array $keys): Collection
{
return $this->query()->whereIn('key', $keys)->get();
}

/**
* @param array $attributes
* @return TextString
Expand Down

0 comments on commit 1b7a1b8

Please sign in to comment.