-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
XliffFileProvider.php
189 lines (172 loc) · 5.81 KB
/
XliffFileProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Neos\Flow\I18n\Xliff\Service;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\I18n;
use Neos\Flow\I18n\Locale;
use Neos\Flow\I18n\Xliff\Model\FileAdapter;
use Neos\Flow\I18n\Xliff\V12\XliffParser as V12XliffParser;
use Neos\Flow\Package\PackageInterface;
use Neos\Flow\Package\PackageManagerInterface;
use Neos\Utility\Arrays;
use Neos\Utility\Files;
/**
* A provider service for XLIFF file objects within the application
*
* @Flow\Scope("singleton")
*/
class XliffFileProvider
{
/**
* @Flow\Inject
* @var PackageManagerInterface
*/
protected $packageManager;
/**
* @Flow\Inject
* @var I18n\Service
*/
protected $localizationService;
/**
* @Flow\Inject
* @var XliffReader
*/
protected $xliffReader;
/**
* @var VariableFrontend
*/
protected $cache;
/**
* The path relative to a package where translation files reside.
*
* @var string
*/
protected $xliffBasePath = 'Private/Translations/';
/**
* @var array
*/
protected $files = [];
/**
* Injects the Flow_I18n_XmlModelCache cache
*
* @param VariableFrontend $cache
* @return void
*/
public function injectCache(VariableFrontend $cache)
{
$this->cache = $cache;
}
/**
* When it's called, XML file is parsed (using parser set in $xmlParser)
* or cache is loaded, if available.
*
* @return void
*/
public function initializeObject()
{
$this->files = $this->cache->get('translationFiles') ?: [];
}
/**
* @param string $fileId
* @param Locale $locale
* @return array
* @todo Add XLIFF 2.0 support
*/
public function getMergedFileData($fileId, Locale $locale): array
{
if (!isset($this->files[$fileId][(string)$locale])) {
$parsedData = [
'fileIdentifier' => $fileId
];
$localeChain = $this->localizationService->getLocaleChain($locale);
// Walk locale chain in reverse, so that translations higher in the chain overwrite fallback translations
foreach (array_reverse($localeChain) as $localeChainItem) {
foreach ($this->packageManager->getActivePackages() as $package) {
/** @var PackageInterface $package */
$translationPath = $package->getResourcesPath() . $this->xliffBasePath . $localeChainItem;
if (is_dir($translationPath)) {
$this->readDirectoryRecursively($translationPath, $parsedData, $fileId, $package->getPackageKey());
}
}
}
$generalTranslationPath = FLOW_PATH_DATA . 'Translations';
if (is_dir($generalTranslationPath)) {
$this->readDirectoryRecursively(FLOW_PATH_DATA . 'Translations', $parsedData, $fileId);
}
$this->files[$fileId][(string)$locale] = $parsedData;
$this->cache->set('translationFiles', $this->files);
}
return $this->files[$fileId][(string)$locale];
}
/**
* @param string $translationPath
* @param array $parsedData
* @param string $fileId
* @param string $defaultPackageName
* @return void
*/
protected function readDirectoryRecursively(string $translationPath, array & $parsedData, string $fileId, string $defaultPackageName = 'Neos.Flow')
{
foreach (Files::readDirectoryRecursively($translationPath) as $filePath) {
$defaultSource = trim(str_replace($translationPath, '', $filePath), '/');
$defaultSource = substr($defaultSource, 0, strrpos($defaultSource, '.'));
$relevantOffset = null;
$documentVersion = null;
$this->xliffReader->readFiles($filePath,
function (\XMLReader $file, $offset, $version) use ($fileId, &$documentVersion, &$relevantOffset, $defaultPackageName, $defaultSource) {
$documentVersion = $version;
switch ($version) {
case '1.2':
$packageName = $file->getAttribute('product-name') ?: $defaultPackageName;
$source = $file->getAttribute('original') ?: $defaultSource;
break;
default:
return;
}
if ($fileId === $packageName . ':' . $source) {
$relevantOffset = $offset;
}
}
);
if (!is_null($relevantOffset)) {
$xliffParser = $this->getParser($documentVersion);
if ($xliffParser) {
$fileData = $xliffParser->getFileDataFromDocument($filePath, $relevantOffset);
$parsedData = Arrays::arrayMergeRecursiveOverrule($parsedData, $fileData);
}
}
}
}
/**
* @param string $fileId
* @param Locale $locale
* @return FileAdapter
*/
public function getFile($fileId, Locale $locale)
{
return new FileAdapter($this->getMergedFileData($fileId, $locale), $locale);
}
/**
* @param string $documentVersion
* @return null|V12XliffParser
*/
public function getParser($documentVersion)
{
switch ($documentVersion) {
case '1.2':
return new V12XliffParser();
break;
default:
return null;
}
}
}