forked from move-elevator/composer-translation-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXliffParser.php
More file actions
207 lines (166 loc) · 5.65 KB
/
Copy pathXliffParser.php
File metadata and controls
207 lines (166 loc) · 5.65 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
/*
* This file is part of the "composer-translation-validator" Composer plugin.
*
* (c) 2025-2026 Konrad Michalik <km@move-elevator.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MoveElevator\ComposerTranslationValidator\Parser;
use InvalidArgumentException;
use SimpleXMLElement;
use function preg_match;
use function strtolower;
/**
* XliffParser.
*
* @author Konrad Michalik <km@move-elevator.de>
* @license GPL-3.0-or-later
*/
class XliffParser extends AbstractParser implements ParserInterface
{
private readonly SimpleXMLElement|bool $xml;
private readonly bool $isVersion2;
/**
* @param string $filePath Path to the XLIFF file
*
* @throws InvalidArgumentException If file cannot be parsed as
* valid XML
*/
public function __construct(string $filePath)
{
parent::__construct($filePath);
$xmlContent = file_get_contents($filePath);
if (false === $xmlContent) {
throw new InvalidArgumentException("Failed to read file: {$filePath}");
}
libxml_use_internal_errors(true);
$this->xml = simplexml_load_string($xmlContent);
if (false === $this->xml) {
throw new InvalidArgumentException("Failed to parse XML content from file: {$filePath}");
}
libxml_clear_errors();
$this->isVersion2 = version_compare((string) ($this->xml['version'] ?? ''), '2.0', '>=');
}
/**
* @return array<int, string>|null
*/
public function extractKeys(): ?array
{
$units = $this->getTranslationUnits();
if (null === $units) {
return [];
}
$keys = [];
foreach ($units as $unit) {
$keys[] = (string) $unit['id'];
}
return $keys;
}
public function getContentByKey(string $key): ?string
{
$units = $this->getTranslationUnits();
if (null === $units) {
return null;
}
$attribute = $this->hasTargetLanguage() ? 'target' : 'source';
foreach ($units as $unit) {
if ((string) $unit['id'] !== $key) {
continue;
}
$source = $this->getUnitContent($unit, 'source');
$target = $this->getUnitContent($unit, 'target');
$primary = 'target' === $attribute ? $target : $source;
if ('' !== $primary) {
return $primary;
}
$fallback = 'target' === $attribute ? $source : $target;
if ('' !== $fallback) {
return $fallback;
}
}
return null;
}
/**
* @return array<int, string>
*/
public static function getSupportedFileExtensions(): array
{
return ['xliff', 'xlf'];
}
public function getLanguage(): string
{
return $this->getLanguageFromFileName() ?? $this->getSourceLanguage();
}
public function isVersion2(): bool
{
return $this->isVersion2;
}
/**
* Extracts the expected locale from the filename, supporting both
* prefix convention (de.locallang.xlf, TYPO3 style) and
* suffix convention (messages.de.xlf, Symfony/Laravel style).
* Returns null if the filename carries no locale.
*/
public function getLanguageFromFileName(): ?string
{
$fileName = $this->getFileName();
// Prefix convention: de.locallang.xlf, de_AT.locallang.xlf, de_DE.locallang.xlf
if (preg_match('/^([a-z]{2})(?:[-_][A-Z]{2})?\./i', $fileName, $matches)) {
return strtolower($matches[1]);
}
// Suffix convention: messages.de.xlf, messages.de_AT.xlf, messages.de_DE.xlf
if (preg_match('/\.([a-z]{2})(?:[-_][A-Z]{2})?\.(?:xlf|xliff)$/i', $fileName, $matches)) {
return strtolower($matches[1]);
}
return null;
}
/**
* Returns the normalized target language declared in the XLIFF file, or null if not set.
* Region suffix is stripped to match getLanguageFromFileName() behavior (e.g. "de-AT" → "de").
* XLIFF 1.x: target-language attribute on <file>.
* XLIFF 2.x: trgLang attribute on <xliff>.
*/
public function getTargetLanguage(): ?string
{
$lang = $this->isVersion2
? (string) ($this->xml['trgLang'] ?? '')
: (string) ($this->xml->file['target-language'] ?? '');
if ('' === $lang) {
return null;
}
return strtolower((string) preg_replace('/[-_][^-_]+$/', '', $lang));
}
private function getSourceLanguage(): string
{
if ($this->isVersion2) {
return (string) ($this->xml['srcLang'] ?? '');
}
return (string) ($this->xml->file['source-language'] ?? '');
}
private function getTranslationUnits(): ?SimpleXMLElement
{
if ($this->isVersion2) {
$units = $this->xml->file->unit;
return $units->count() > 0 ? $units : null;
}
$units = $this->xml->file->body->{'trans-unit'};
return $units->count() > 0 ? $units : null;
}
private function getUnitContent(SimpleXMLElement $unit, string $element): string
{
if ($this->isVersion2) {
return (string) ($unit->segment->{$element} ?? '');
}
return (string) ($unit->{$element} ?? '');
}
private function hasTargetLanguage(): bool
{
if ($this->isVersion2) {
return !empty((string) ($this->xml['trgLang'] ?? ''));
}
return !empty((string) $this->xml->file['target-language']);
}
}