1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\AutoFallbackTranslationBundle\Translator; |
4
|
|
|
|
5
|
|
|
use Happyr\AutoFallbackTranslationBundle\Service\TranslatorService; |
6
|
|
|
use Symfony\Component\Translation\TranslatorBagInterface; |
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Tobias Nyholm <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FallbackTranslator implements TranslatorInterface, TranslatorBagInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var TranslatorInterface|TranslatorBagInterface |
16
|
|
|
*/ |
17
|
|
|
private $symfonyTranslator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var TranslatorService |
21
|
|
|
*/ |
22
|
|
|
private $translatorService; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $defaultLocale; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $defaultLocale |
31
|
|
|
* @param TranslatorInterface $symfonyTranslator |
32
|
|
|
* @param TranslatorService $translatorService |
33
|
|
|
*/ |
34
|
|
|
public function __construct($defaultLocale, TranslatorInterface $symfonyTranslator, TranslatorService $translatorService) |
35
|
|
|
{ |
36
|
|
|
$this->symfonyTranslator = $symfonyTranslator; |
37
|
|
|
$this->translatorService = $translatorService; |
38
|
|
|
$this->defaultLocale = $defaultLocale; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function trans($id, array $parameters = array(), $domain = null, $locale = null) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$id = (string) $id; |
47
|
|
|
if (!$domain) { |
|
|
|
|
48
|
|
|
$domain = 'messages'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$catalogue = $this->getCatalogue($locale); |
52
|
|
|
if ($catalogue->defines($id, $domain)) { |
53
|
|
|
return $this->symfonyTranslator->trans($id, $parameters, $domain, $locale); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$locale = $catalogue->getLocale(); |
57
|
|
|
if ($locale === $this->defaultLocale) { |
58
|
|
|
// we cant do anything... |
59
|
|
|
return $id; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$orgString = $this->symfonyTranslator->trans($id, $parameters, $domain, $this->defaultLocale); |
63
|
|
|
|
64
|
|
|
return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$id = (string) $id; |
73
|
|
|
if (!$domain) { |
|
|
|
|
74
|
|
|
$domain = 'messages'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$catalogue = $this->getCatalogue($locale); |
78
|
|
|
if ($catalogue->defines($id, $domain)) { |
79
|
|
|
return $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $locale); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$locale = $catalogue->getLocale(); |
83
|
|
|
if ($locale === $this->defaultLocale) { |
84
|
|
|
// we cant do anything... |
85
|
|
|
return $id; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$orgString = $this->symfonyTranslator->transChoice($id, $number, $parameters, $domain, $this->defaultLocale); |
89
|
|
|
|
90
|
|
|
return $this->translateWithSubstitutedParameters($orgString, $locale, $parameters); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function setLocale($locale) |
97
|
|
|
{ |
98
|
|
|
$this->symfonyTranslator->setLocale($locale); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getLocale() |
105
|
|
|
{ |
106
|
|
|
return $this->symfonyTranslator->getLocale(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getCatalogue($locale = null) |
113
|
|
|
{ |
114
|
|
|
return $this->symfonyTranslator->getCatalogue($locale); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Passes through all unknown calls onto the translator object. |
119
|
|
|
*/ |
120
|
|
|
public function __call($method, $args) |
121
|
|
|
{ |
122
|
|
|
return call_user_func_array(array($this->symfonyTranslator, $method), $args); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $orgString This is the string in the default locale. It has the values of $parameters in the string already. |
127
|
|
|
* @param string $locale you wan to translate to. |
128
|
|
|
* @param array $parameters |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
private function translateWithSubstitutedParameters($orgString, $locale, array $parameters) |
133
|
|
|
{ |
134
|
|
|
// Replace parameters |
135
|
|
|
$replacements = []; |
136
|
|
|
foreach ($parameters as $placeholder => $nonTranslatableValue) { |
137
|
|
|
$replacements[(string) $nonTranslatableValue] = uniqid(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$replacedString = str_replace(array_keys($replacements), array_values($replacements), $orgString); |
141
|
|
|
$translatedString = $this->getTranslatorService()->translate($replacedString, $this->defaultLocale, $locale); |
142
|
|
|
|
143
|
|
|
return str_replace(array_values($replacements), array_keys($replacements), $translatedString); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return TranslatorService |
148
|
|
|
*/ |
149
|
|
|
protected function getTranslatorService() |
150
|
|
|
{ |
151
|
|
|
return $this->translatorService; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
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.