1
|
|
|
<?php namespace Limoncello\l10n\Format; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\L10n\FormatterInterface; |
20
|
|
|
use Limoncello\l10n\Contracts\Format\TranslatorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package Limoncello\l10n |
24
|
|
|
*/ |
25
|
|
|
class Formatter implements FormatterInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $locale; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $namespace; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var TranslatorInterface |
39
|
|
|
*/ |
40
|
|
|
private $translator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $locale |
44
|
|
|
* @param string $namespace |
45
|
|
|
* @param TranslatorInterface $translator |
46
|
|
|
*/ |
47
|
|
|
public function __construct(string $locale, string $namespace, TranslatorInterface $translator) |
48
|
|
|
{ |
49
|
|
|
assert(empty($locale) === false && empty($namespace) === false); |
50
|
|
|
|
51
|
|
|
$this->locale = locale_canonicalize($locale); |
52
|
|
|
$this->namespace = $namespace; |
53
|
|
|
$this->translator = $translator; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getLocale(): string |
60
|
|
|
{ |
61
|
|
|
return $this->locale; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getNamespace(): string |
68
|
|
|
{ |
69
|
|
|
return $this->namespace; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return TranslatorInterface |
74
|
|
|
*/ |
75
|
|
|
public function getTranslator(): TranslatorInterface |
76
|
|
|
{ |
77
|
|
|
return $this->translator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function formatMessage(string $message, array $args = []): string |
84
|
|
|
{ |
85
|
|
|
$result = $this->getTranslator()->translateMessage($this->getLocale(), $this->getNamespace(), $message, $args); |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|