NetteTranslator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 6 3
A __construct() 0 4 1
A getTranslator() 0 4 1
A getLocale() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Nette extension for bckp/translator
7
 * (c) Radovan Kepák
8
 *
9
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
10
 *
11
 * @author Radovan Kepak <[email protected]>
12
 *  --------------------------------------------------------------------------
13
 */
14
15
namespace Bckp\Translator\Nette;
16
17
use Bckp\Translator\Translator;
18
use Nette\Localization;
19
20
use function array_walk;
21
22
class NetteTranslator implements Localization\Translator
23
{
24
	private Translator $translator;
25
26
	/** @api */
27
	public function __construct(
28
		private readonly TranslatorProvider $translatorProvider,
29
		private readonly LocaleProvider $localeProvider,
30
	) {}
31
32
	/**
33
	 * @api
34
	 */
35
	public function getLocale(): string
36
	{
37
		return $this->localeProvider->resolve();
38
	}
39
40
	/**
41
	 * @api
42
	 */
43
	#[\Override]
44
	public function translate(\Stringable|string $message, mixed ...$parameters): string
45
	{
46
		// Normalize nette to bckp/translator parameters
47
		array_walk($parameters, static fn(&$value): string|float|int => $value = is_int($value) || is_float($value) ? $value : (string) $value);
48
		return $this->getTranslator()->translate($message, ...$parameters);
49
	}
50
51
	private function getTranslator(): Translator
52
	{
53
		/** @psalm-suppress RedundantPropertyInitializationCheck */
54
		return $this->translator ??= $this->translatorProvider->getTranslator($this->getLocale());
55
	}
56
}
57