1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Util\Translator; |
4
|
|
|
|
5
|
|
|
use Zend\I18n\Translator\Loader\PhpMemoryArray; |
6
|
|
|
|
7
|
|
|
class Translator extends \Zend\I18n\Translator\Translator |
8
|
|
|
{ |
9
|
|
|
const SERVICE_NAME = 'magium'; |
10
|
|
|
|
11
|
|
|
protected $csvFiles = []; |
12
|
|
|
|
13
|
|
|
protected $loader; |
14
|
|
|
|
15
|
|
|
protected $skipServiceBuild = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param boolean $skipServiceBuild |
19
|
|
|
*/ |
20
|
|
|
public function setSkipServiceBuild($skipServiceBuild) |
21
|
|
|
{ |
22
|
|
|
$this->skipServiceBuild = $skipServiceBuild; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function addTranslationCsvFile($file, $locale) |
26
|
|
|
{ |
27
|
|
|
if (!isset($this->csvFiles[$locale])) { |
28
|
|
|
$this->csvFiles[$locale] = []; |
29
|
|
|
} |
30
|
|
|
$this->csvFiles[$locale][] = $file; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function buildTranslationService() |
34
|
|
|
{ |
35
|
|
|
if ($this->skipServiceBuild || $this->loader instanceof PhpMemoryArray) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
$translation = [ |
39
|
|
|
'default' => [ |
40
|
|
|
'en_US' => [] |
41
|
|
|
] |
42
|
|
|
]; |
43
|
|
|
foreach ($this->csvFiles as $locale => $fileList) { |
44
|
|
|
$translation['default'][$locale] = []; |
45
|
|
|
foreach ($fileList as $file) { |
46
|
|
|
if (file_exists($file)) { |
47
|
|
|
$fh = fopen($file, 'r'); |
48
|
|
|
while ($parts = fgetcsv($fh)) { |
49
|
|
|
$translation['default'][$locale][$parts[0]] = $parts[1]; |
50
|
|
|
} |
51
|
|
|
fclose($fh); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$this->loader = new PhpMemoryArray($translation); |
56
|
|
|
$this->getPluginManager()->setService(self::SERVICE_NAME, $this->loader); |
57
|
|
|
$this->addRemoteTranslations(self::SERVICE_NAME); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function translate($message, $textDomain = 'default', $locale = null) |
61
|
|
|
{ |
62
|
|
|
$this->buildTranslationService(); |
63
|
|
|
return parent::translate($message, $textDomain, $locale); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
public function translatePlaceholders($message, $textDomain = 'default', $locale = null) |
68
|
|
|
{ |
69
|
|
|
$this->buildTranslationService(); |
70
|
|
|
if (is_array($message)) { |
71
|
|
|
foreach ($message as $key => $value) { |
72
|
|
|
$value = $this->translatePlaceholders($value); |
73
|
|
|
$message[$key] = $value; |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
$results = []; |
77
|
|
|
preg_match_all('/\{\{([^\}]+)\}\}/', $message, $results); |
78
|
|
|
array_shift($results); |
79
|
|
|
|
80
|
|
|
foreach ($results as $result) { |
81
|
|
|
if (is_array($result)) { |
82
|
|
|
while (($part = array_shift($result)) !== null) { |
83
|
|
|
$message = $this->translatePart($message, $part, $textDomain, $locale); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$message = $this->translatePart($message, $result, $textDomain, $locale); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $message; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
protected function translatePart($translate, $result, $textDomain, $locale) |
95
|
|
|
{ |
96
|
|
|
$newResult = parent::translate($result, $textDomain, $locale); |
|
|
|
|
97
|
|
|
$translate = str_replace('{{' . $result . '}}', $newResult, $translate); |
98
|
|
|
return $translate; |
99
|
|
|
} |
100
|
|
|
} |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.