Skip to content

Commit b44cd94

Browse files
fix: simplify getContentByKey method by removing unused attribute parameter
1 parent eb58f1d commit b44cd94

19 files changed

Lines changed: 151 additions & 63 deletions

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
'@PSR2' => true,
1414
'@Symfony' => true,
1515
])
16+
->setUnsupportedPhpVersionAllowed(true)
1617
->setFinder($finder);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
],
6767
"fix:composer": "@composer normalize",
6868
"fix:editorconfig": "@lint:editorconfig --fix",
69-
"fix:php": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix",
69+
"fix:php": "php-cs-fixer fix",
7070
"lint": [
7171
"@lint:composer",
7272
"@lint:editorconfig",

src/Parser/JsonParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function extractKeys(): ?array
6060
return $extract($this->json);
6161
}
6262

63-
public function getContentByKey(string $key, string $attribute = 'source'): ?string
63+
public function getContentByKey(string $key): ?string
6464
{
6565
// Note: the $attribute parameter is required by ParserInterface
6666
// but is not used for JSON, since JSON has no source/target concept.

src/Parser/ParserInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function getSupportedFileExtensions(): array;
1818
*/
1919
public function extractKeys(): ?array;
2020

21-
public function getContentByKey(string $key, string $attribute = 'source'): ?string;
21+
public function getContentByKey(string $key): ?string;
2222

2323
public function getFileName(): string;
2424

src/Parser/PhpParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function extractKeys(): ?array
5454
return $extract($this->translations);
5555
}
5656

57-
public function getContentByKey(string $key, string $attribute = 'source'): ?string
57+
public function getContentByKey(string $key): ?string
5858
{
5959
// Note: the $attribute parameter is required by ParserInterface
6060
// but is not used for PHP files, since PHP has no source/target concept.

src/Parser/XliffParser.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,28 @@ public function extractKeys(): ?array
4545
return $keys;
4646
}
4747

48-
public function getContentByKey(string $key, string $attribute = 'source'): ?string
48+
public function getContentByKey(string $key): ?string
4949
{
50+
$attribute = $this->hasTargetLanguage() ? 'target' : 'source';
51+
5052
foreach ($this->xml->file->body->{'trans-unit'} as $unit) {
5153
if ((string) $unit['id'] === $key) {
5254
if ('' !== (string) $unit->{$attribute}) {
5355
return (string) $unit->{$attribute};
5456
}
5557

56-
if ('source' === $attribute) {
57-
return $this->getContentByKey($key, 'target');
58+
if ('target' === $attribute && $this->hasTargetLanguage()) {
59+
$fallbackContent = (string) $unit->source;
60+
if ('' !== $fallbackContent) {
61+
return $fallbackContent;
62+
}
63+
}
64+
65+
if ('source' === $attribute && !$this->hasTargetLanguage()) {
66+
$fallbackContent = (string) $unit->target;
67+
if ('' !== $fallbackContent) {
68+
return $fallbackContent;
69+
}
5870
}
5971
}
6072
}
@@ -84,4 +96,9 @@ public function getLanguage(): string
8496

8597
return $language;
8698
}
99+
100+
private function hasTargetLanguage(): bool
101+
{
102+
return !empty((string) $this->xml->file['target-language']);
103+
}
87104
}

src/Parser/YamlParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function extractKeys(): ?array
5252
return $extract($this->yaml);
5353
}
5454

55-
public function getContentByKey(string $key, string $attribute = 'source'): ?string
55+
public function getContentByKey(string $key): ?string
5656
{
5757
// Note: the $attribute parameter is required by ParserInterface
5858
// but is not used for YAML, since YAML has no source/target concept.

tests/src/Fixtures/translations/xliff/fail/de.locallang.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3-
<file source-language="en" datatype="plaintext" original="messages">
3+
<file target-language="de" source-language="en" datatype="plaintext" original="messages">
44
<header/>
55
<body>
66
<trans-unit id="key1">

tests/src/Fixtures/translations/xliff/fail/de.locallang_be.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
22
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3-
<file source-language="en" datatype="plaintext" original="messages">
3+
<file target-language="de" source-language="en" datatype="plaintext" original="messages">
44
<header/>
55
<body>
66
<trans-unit id="key1">

tests/src/Fixtures/translations/xliff/success/de.locallang.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
22
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
3-
<file source-language="en" datatype="plaintext" original="messages">
3+
<file target-language="de" source-language="en" datatype="plaintext" original="messages">
44
<header/>
55
<body>
66
<trans-unit id="key1">

0 commit comments

Comments
 (0)