1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Translator; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Translation\TranslatorBagInterface; |
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
16
|
|
|
use Translation\Translator\Translator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This is a bridge between Symfony's translator service and Translation\Translator\Translator. |
20
|
|
|
* |
21
|
|
|
* @author Tobias Nyholm <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class FallbackTranslator implements TranslatorInterface, TranslatorBagInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TranslatorInterface|TranslatorBagInterface |
27
|
|
|
*/ |
28
|
|
|
private $symfonyTranslator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Translator |
32
|
|
|
*/ |
33
|
|
|
private $externalTranslator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $defaultLocale; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $defaultLocale |
42
|
|
|
* @param TranslatorInterface $symfonyTranslator |
43
|
|
|
* @param Translator $externalTranslator |
44
|
|
|
*/ |
45
|
1 |
|
public function __construct($defaultLocale, TranslatorInterface $symfonyTranslator, Translator $externalTranslator) |
46
|
|
|
{ |
47
|
1 |
|
$this->symfonyTranslator = $symfonyTranslator; |
48
|
1 |
|
$this->externalTranslator = $externalTranslator; |
49
|
1 |
|
$this->defaultLocale = $defaultLocale; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function trans($id, array $parameters = [], $domain = null, $locale = null) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$id = (string) $id; |
58
|
|
|
if (empty($domain)) { |
59
|
|
|
$domain = 'messages'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$catalogue = $this->getCatalogue($locale); |
63
|
|
|
if ($catalogue->defines($id, $domain)) { |
64
|
|
|
return $this->symfonyTranslator->trans($id, $parameters, $domain, $locale); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$locale = $catalogue->getLocale(); |
68
|
|
|
if ($locale === $this->defaultLocale) { |
69
|
|
|
// we cant do anything... |
70
|
|
|
return $id; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$orgString = $this->symfonyTranslator->trans($id, $parameters, $domain, $this->defaultLocale); |
74
|
|
|
|
75
|
|
|
return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$id = (string) $id; |
84
|
|
|
if (empty($domain)) { |
85
|
|
|
$domain = 'messages'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$catalogue = $this->getCatalogue($locale); |
89
|
|
|
if ($catalogue->defines($id, $domain)) { |
90
|
|
|
return $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $locale); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$locale = $catalogue->getLocale(); |
94
|
|
|
if ($locale === $this->defaultLocale) { |
95
|
|
|
// we cant do anything... |
96
|
|
|
return $id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$orgString = $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $this->defaultLocale); |
100
|
|
|
|
101
|
|
|
return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function setLocale($locale) |
108
|
|
|
{ |
109
|
|
|
$this->symfonyTranslator->setLocale($locale); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getLocale() |
116
|
|
|
{ |
117
|
|
|
return $this->symfonyTranslator->getLocale(); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getCatalogue($locale = null) |
124
|
|
|
{ |
125
|
|
|
return $this->symfonyTranslator->getCatalogue($locale); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Passes through all unknown calls onto the translator object. |
130
|
|
|
*/ |
131
|
|
|
public function __call($method, $args) |
132
|
|
|
{ |
133
|
|
|
return call_user_func_array([$this->symfonyTranslator, $method], $args); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $orgString This is the string in the default locale. It has the values of $parameters in the string already. |
138
|
|
|
* @param string $locale you wan to translate to. |
139
|
|
|
* @param array $parameters |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
1 |
|
private function translateWithSubstitutedParameters($orgString, $locale, array $parameters) |
144
|
|
|
{ |
145
|
|
|
// Replace parameters |
146
|
1 |
|
$replacements = []; |
147
|
1 |
|
foreach ($parameters as $placeholder => $nonTranslatableValue) { |
148
|
1 |
|
$replacements[(string) $nonTranslatableValue] = uniqid(); |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
$replacedString = str_replace(array_keys($replacements), array_values($replacements), $orgString); |
152
|
1 |
|
$translatedString = $this->externalTranslator->translate($replacedString, $this->defaultLocale, $locale); |
153
|
|
|
|
154
|
1 |
|
if (null === $translatedString) { |
155
|
|
|
// Could not be translated |
156
|
|
|
return $orgString; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
return str_replace(array_values($replacements), array_keys($replacements), $translatedString); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.