Skip to content

Commit

Permalink
Feature: Possibility to change format of translation key (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeleznypa authored Nov 25, 2022
1 parent bc8461e commit eea8999
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Contributte\Translation\Tracy\Panel;
use Contributte\Translation\Wrappers\Message;
use Contributte\Translation\Wrappers\NotTranslate;
use Generator;
use Nette\Localization\ITranslator;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
Expand Down Expand Up @@ -45,6 +46,9 @@ class Translator extends SymfonyTranslator implements ITranslator

private string $initLang;

/** @var callable(mixed[] $params): Generator|null */
private $translateKeyGenerator = null;

/**
* @param array<string> $cacheVary
*/
Expand Down Expand Up @@ -154,6 +158,27 @@ public function addPrefix(
return $this;
}

public function getTranslateKeyGenerator(): callable
{
if ($this->translateKeyGenerator === null) {
$this->translateKeyGenerator = function (array $params): Generator {
foreach ($params as $k1 => $v1) {
yield '%' . $k1 . '%' => $v1;
}
};
}

return $this->translateKeyGenerator;
}

public function setTranslateKeyGenerator(
callable $generator
): self
{
$this->translateKeyGenerator = $generator;
return $this;
}

/**
* @throws \Contributte\Translation\Exceptions\InvalidArgument
*/
Expand Down Expand Up @@ -316,13 +341,9 @@ public function translate(
[$domain, $message] = Helpers::extractMessage($message);
}

$tmp = [];

foreach ($params as $k1 => $v1) {
$tmp['%' . $k1 . '%'] = $v1;
}
$generator = $this->getTranslateKeyGenerator();

$params = $tmp;
$params = iterator_to_array($generator($params));

if (Validators::isNumeric($count)) {
$params += ['%count%' => $count];
Expand Down
45 changes: 45 additions & 0 deletions tests/Tests/CustomTranslateKeyTest.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace Tests;

use Contributte\Translation\FallbackResolver;
use Contributte\Translation\LocaleResolver;
use Contributte\Translation\Translator;
use Contributte\Translation\Wrappers\Message;
use Generator;
use Mockery;
use Nette\DI\Container;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Tester\Assert;

$container = require __DIR__ . '/../bootstrap.php';

final class CustomTranslateKeyTest extends TestAbstract
{

public function test01(): void
{
$translations = [
'hi' => 'Hi :name!',
];

$translator = new Translator(new LocaleResolver(Mockery::mock(Container::class)), new FallbackResolver(), 'en', $this->container->getParameters()['tempDir'], true);
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', $translations, 'en');

Assert::same('Hi :name!', $translator->translate('hi', ['name' => 'Ales']));
Assert::same('Hi :name!', $translator->translate(new Message('hi', ['name' => 'Ales'])));

$translator->setTranslateKeyGenerator(function (array $params): Generator {
foreach ($params as $k1 => $v1) {
yield ':' . $k1 => $v1;
}
});

Assert::same('Hi Ales!', $translator->translate('hi', ['name' => 'Ales']));
Assert::same('Hi Ales!', $translator->translate(new Message('hi', ['name' => 'Ales'])));
}

}

(new CustomTranslateKeyTest($container))->run();

0 comments on commit eea8999

Please sign in to comment.