Passed
Push — master ( 5274cf...3733ff )
by Radovan
02:25 queued 11s
created

NetteTranslator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 4 1
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
class NetteTranslator implements Localization\Translator
21
{
22
	private Translator $translator;
23
24
	/** @api */
25
	public function __construct(
26
		private readonly TranslatorProvider $translatorProvider,
27
		private readonly LocaleProvider $localeProvider,
28
	) {}
29
30
	/**
31
	 * @api
32
	 */
33
	public function getLocale(): string
34
	{
35
		return $this->localeProvider->resolve();
36
	}
37
38
	/**
39
	 * @api
40
	 */
41
	#[\Override]
42
	public function translate(\Stringable|string $message, mixed ...$parameters): string
43
	{
44
		return $this->getTranslator()->translate($message, ...$parameters);
45
	}
46
47
	private function getTranslator(): Translator
48
	{
49
		/** @psalm-suppress RedundantPropertyInitializationCheck */
50
		return $this->translator ??= $this->translatorProvider->getTranslator($this->getLocale());
51
	}
52
}
53