|
1
|
|
|
<?php declare (strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\l10n\Format; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\l10n\Contracts\Format\TranslatorInterface; |
|
22
|
|
|
use Limoncello\l10n\Contracts\Messages\BundleStorageInterface; |
|
23
|
|
|
use MessageFormatter; |
|
24
|
|
|
use function assert; |
|
25
|
|
|
use function is_object; |
|
26
|
|
|
use function is_scalar; |
|
27
|
|
|
use function method_exists; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @package Limoncello\l10n |
|
31
|
|
|
*/ |
|
32
|
|
|
class Translator implements TranslatorInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var BundleStorageInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $storage; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param BundleStorageInterface $storage |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function __construct(BundleStorageInterface $storage) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$this->setStorage($storage); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritdoc |
|
49
|
|
|
* |
|
50
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function translateMessage(string $locale, string $namespace, string $message, array $args = []): string |
|
53
|
|
|
{ |
|
54
|
4 |
|
$translation = $this->getStorage()->get($locale, $namespace, $message); |
|
55
|
4 |
|
if ($translation !== null) { |
|
56
|
4 |
|
$message = $translation[BundleStorageInterface::INDEX_PAIR_VALUE]; |
|
57
|
4 |
|
$locale = $translation[BundleStorageInterface::INDEX_PAIR_LOCALE]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
return static::formatMessage($locale, $message, $args); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return BundleStorageInterface |
|
65
|
|
|
*/ |
|
66
|
4 |
|
public function getStorage(): BundleStorageInterface |
|
67
|
|
|
{ |
|
68
|
4 |
|
return $this->storage; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param BundleStorageInterface $storage |
|
73
|
|
|
* |
|
74
|
|
|
* @return TranslatorInterface |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function setStorage(BundleStorageInterface $storage): TranslatorInterface |
|
77
|
|
|
{ |
|
78
|
4 |
|
$this->storage = $storage; |
|
79
|
|
|
|
|
80
|
4 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $locale |
|
85
|
|
|
* @param string $message |
|
86
|
|
|
* @param array $args |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
* |
|
90
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
91
|
|
|
*/ |
|
92
|
4 |
|
protected static function formatMessage(string $locale, string $message, array $args): string |
|
93
|
|
|
{ |
|
94
|
|
|
// underlying `format` method cannot work with arguments that are not convertible to string |
|
95
|
|
|
// therefore we have to check that only those that actually can be used |
|
96
|
|
|
assert(call_user_func(function () use ($args) : bool { |
|
97
|
4 |
|
$result = true; |
|
98
|
4 |
|
foreach ($args as $arg) { |
|
99
|
1 |
|
$result = $result && |
|
100
|
1 |
|
is_scalar($arg) === true || |
|
101
|
1 |
|
$arg === null || |
|
102
|
1 |
|
(is_object($arg) === true && method_exists($arg, '__toString') === true); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
return $result; |
|
106
|
4 |
|
})); |
|
107
|
|
|
|
|
108
|
4 |
|
$formatter = MessageFormatter::create($locale, $message); |
|
109
|
4 |
|
$message = $formatter->format($args); |
|
110
|
4 |
|
assert($message !== false, $formatter->getErrorMessage()); |
|
111
|
|
|
|
|
112
|
4 |
|
return $message; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|