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\Storage; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader; |
15
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
16
|
|
|
use Symfony\Component\Translation\Writer\TranslationWriter; |
17
|
|
|
use Translation\Common\Model\Message; |
18
|
|
|
use Translation\Common\Storage; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This storage uses Symfony's writer and loader. |
22
|
|
|
* |
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class FileStorage implements Storage |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var TranslationWriter |
29
|
|
|
*/ |
30
|
|
|
private $writer; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TranslationLoader |
34
|
|
|
*/ |
35
|
|
|
private $loader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string directory path |
39
|
|
|
*/ |
40
|
|
|
private $dir; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MessageCatalogue[] Fetched catalogies |
44
|
|
|
*/ |
45
|
|
|
private $catalogues; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param TranslationWriter $writer |
49
|
|
|
* @param TranslationLoader $loader |
50
|
|
|
* @param array $dir |
51
|
|
|
*/ |
52
|
|
|
public function __construct(TranslationWriter $writer, TranslationLoader $loader, $dir) |
53
|
|
|
{ |
54
|
|
|
$this->writer = $writer; |
55
|
|
|
$this->loader = $loader; |
56
|
|
|
$this->dir = $dir; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function get($locale, $domain, $key) |
63
|
|
|
{ |
64
|
|
|
$catalogue = $this->getCatalogue($locale); |
65
|
|
|
|
66
|
|
|
$translation = $catalogue->get($key, $domain); |
67
|
|
|
|
68
|
|
|
return new Message($key, $domain, $locale, $translation); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function update(Message $message) |
75
|
|
|
{ |
76
|
|
|
$catalogue = $this->getCatalogue($message->getLocale()); |
77
|
|
|
$catalogue->set($message->getKey(), $message->getTranslation(), $message->getDomain()); |
78
|
|
|
$this->writeCatalogue($catalogue, $message->getLocale(), $message->getDomain()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function delete($locale, $domain, $key) |
85
|
|
|
{ |
86
|
|
|
$catalogue = $this->getCatalogue($locale); |
87
|
|
|
$messages = $catalogue->all($domain); |
88
|
|
|
unset($messages[$key]); |
89
|
|
|
|
90
|
|
|
$catalogue->replace($messages, $domain); |
91
|
|
|
$this->writeCatalogue($catalogue, $locale, $domain); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Save catalogue back to file. |
96
|
|
|
* |
97
|
|
|
* @param MessageCatalogue $catalogue |
98
|
|
|
* @param string $domain |
99
|
|
|
*/ |
100
|
|
|
private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain) |
101
|
|
|
{ |
102
|
|
|
$resources = $catalogue->getResources(); |
103
|
|
|
foreach ($resources as $resource) { |
104
|
|
|
$path = $resource->getResource(); |
|
|
|
|
105
|
|
|
if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) { |
106
|
|
|
$this->writer->writeTranslations($catalogue, $matches[1], ['path' => str_replace($matches[0], '', $path)]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $locale |
113
|
|
|
*/ |
114
|
|
|
private function getCatalogue($locale) |
115
|
|
|
{ |
116
|
|
|
if (empty($this->catalogues[$locale])) { |
117
|
|
|
$this->loadCatalogue($locale, [$this->dir]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->catalogues[$locale]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Load catalogue from files. |
125
|
|
|
* |
126
|
|
|
* @param $locale |
127
|
|
|
* @param array $dirs |
128
|
|
|
*/ |
129
|
|
|
private function loadCatalogue($locale, array $dirs) |
130
|
|
|
{ |
131
|
|
|
$currentCatalogue = new MessageCatalogue($locale); |
132
|
|
|
foreach ($dirs as $path) { |
133
|
|
|
if (is_dir($path)) { |
134
|
|
|
$this->loader->loadMessages($path, $currentCatalogue); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->catalogues[$locale] = $currentCatalogue; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..