|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the openl10n package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Matthieu Moquet <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Openl10n\Component\Translation; |
|
13
|
|
|
|
|
14
|
|
|
use Openl10n\Component\Translation\MessageCatalogue\MessageCatalogueLoader; |
|
15
|
|
|
use Symfony\Component\Translation\MessageCatalogueInterface; |
|
16
|
|
|
use Symfony\Component\Translation\MessageSelector; |
|
17
|
|
|
use Symfony\Component\Translation\TranslatorBagInterface; |
|
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
class SimpleTranslator implements TranslatorInterface, TranslatorBagInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The default locale. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $locale; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The loader of message catalogue. |
|
31
|
|
|
* |
|
32
|
|
|
* @var MessageCatalogueLoader |
|
33
|
|
|
*/ |
|
34
|
|
|
private $catalogueLoader; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The message selector for pluralization. |
|
38
|
|
|
* |
|
39
|
|
|
* @var MessageSelector |
|
40
|
|
|
*/ |
|
41
|
|
|
private $selector; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var MessageCatalogueInterface[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private $catalogues = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructor. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $locale The default locale |
|
52
|
|
|
* @param MessageCatalogueLoader $catalogueLoader The loader of message catalogue |
|
53
|
|
|
* @param MessageSelector|null $selector The message selector for pluralization |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct($locale, MessageCatalogueLoader $catalogueLoader, MessageSelector $selector = null) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->setLocale($locale); |
|
58
|
|
|
$this->catalogueLoader = $catalogueLoader; |
|
59
|
|
|
$this->selector = $selector ?: new MessageSelector(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
66
|
|
|
{ |
|
67
|
|
|
if (null === $domain) { |
|
68
|
|
|
$domain = 'messages'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$message = $this->getMessage($locale, $id, $domain); |
|
72
|
|
|
|
|
73
|
|
|
return strtr($message, $parameters); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
80
|
|
|
{ |
|
81
|
|
|
if (null === $domain) { |
|
82
|
|
|
$domain = 'messages'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$pluralizedMessage = $this->getMessage($locale, $id, $domain); |
|
86
|
|
|
|
|
87
|
|
|
$message = $this->selector->choose($pluralizedMessage, (int) $number, $locale); |
|
88
|
|
|
|
|
89
|
|
|
return strtr($message, $parameters); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setLocale($locale) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->locale = $locale; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getLocale() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->locale; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getCatalogue($locale = null) |
|
112
|
|
|
{ |
|
113
|
|
|
if (null === $locale) { |
|
114
|
|
|
$locale = $this->getLocale(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (!isset($this->catalogues[$locale])) { |
|
118
|
|
|
$this->catalogues[$locale] = $this->catalogueLoader->loadCatalogue($locale); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->catalogues[$locale]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Retrieve translated message from localized catalogue. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $locale Locale |
|
128
|
|
|
* @param string $id Message identifier |
|
129
|
|
|
* @param string $domain Domain name |
|
130
|
|
|
* |
|
131
|
|
|
* @return string Translated message |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getMessage($locale, $id, $domain) |
|
134
|
|
|
{ |
|
135
|
|
|
// Load the catalogue |
|
136
|
|
|
$catalogue = $this->getCatalogue($locale); |
|
137
|
|
|
|
|
138
|
|
|
// Retrieve the message from the catalogue |
|
139
|
|
|
$message = $catalogue->get((string) $id, $domain); |
|
140
|
|
|
|
|
141
|
|
|
return $message; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|