|
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\l10n\Contracts\Format\TranslatorInterface; |
|
20
|
|
|
use Limoncello\l10n\Contracts\Messages\BundleStorageInterface; |
|
21
|
|
|
use MessageFormatter; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\l10n |
|
25
|
|
|
*/ |
|
26
|
|
|
class Translator implements TranslatorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var BundleStorageInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $storage; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param BundleStorageInterface $storage |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(BundleStorageInterface $storage) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setStorage($storage); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
* |
|
44
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
45
|
|
|
*/ |
|
46
|
|
|
public function translateMessage(string $locale, string $namespace, string $key, array $args = []): string |
|
47
|
|
|
{ |
|
48
|
|
|
$translation = $this->getStorage()->get($locale, $namespace, $key); |
|
49
|
|
|
if ($translation !== null) { |
|
50
|
|
|
$message = $translation[BundleStorageInterface::INDEX_PAIR_VALUE]; |
|
51
|
|
|
$locale = $translation[BundleStorageInterface::INDEX_PAIR_LOCALE]; |
|
52
|
|
|
} else { |
|
53
|
|
|
$message = $key; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->formatMessage($locale, $message, $args); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return BundleStorageInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getStorage(): BundleStorageInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->storage; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param BundleStorageInterface $storage |
|
69
|
|
|
* |
|
70
|
|
|
* @return Translator |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setStorage(BundleStorageInterface $storage): Translator |
|
73
|
|
|
{ |
|
74
|
|
|
$this->storage = $storage; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $locale |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
* @param array $args |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
* |
|
86
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function formatMessage(string $locale, string $message, array $args): string |
|
89
|
|
|
{ |
|
90
|
|
|
return MessageFormatter::formatMessage($locale, $message, $args); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|