Skip to content

Commit

Permalink
feat: extensibility of merged strings (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland authored Jan 17, 2025
1 parent fb5356b commit 4bf313e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 60 deletions.
70 changes: 10 additions & 60 deletions src/Api/Controllers/MergedIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,28 @@
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
{
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)
Expand All @@ -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);
}
}
76 changes: 76 additions & 0 deletions src/Repositories/MergedStringsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace FoF\Linguist\Repositories;

use Illuminate\Support\Str;
use Symfony\Contracts\Translation\TranslatorInterface;

class MergedStringsRepository
{
/**
* @var DefaultStringsRepository
*/
protected $defaultStrings;

/**
* @var StringRepository
*/
protected $strings;

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

public function __construct(
DefaultStringsRepository $defaultStrings,
StringRepository $strings,
TranslatorInterface $translator
) {
$this->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;
}
}

0 comments on commit 4bf313e

Please sign in to comment.