Skip to content

Commit ae6a2b1

Browse files
claudeswissspidy
andcommitted
Treat "0" as a valid string
Strings were checked for truthiness in several places, so "0" was handled like an empty string: * The PO loader dropped untranslated entries with `msgid "0"`. * The PO generator wrote `msgstr ""` for a "0" translation and left out a "0" context, plural or previous string. * The MO generator skipped entries translated as "0", as well as a "0" context or plural. * `Translation::mergeWith()` replaced a "0" translation or plural with the other one, and ignored them when overriding. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Claude-Session: https://claude.ai/code/session_01UWTd5qmztfFv37ybV4xsjV
1 parent 73ee581 commit ae6a2b1

8 files changed

Lines changed: 168 additions & 24 deletions

File tree

‎src/Generator/MoGenerator.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ public function generateString(Translations $translations): string
3232
}
3333

3434
foreach ($translations as $translation) {
35-
if (!$translation->getTranslation() || $translation->isDisabled()) {
35+
if (!$translation->isTranslated() || $translation->isDisabled()) {
3636
continue;
3737
}
3838

39-
if ($context = $translation->getContext()) {
39+
$context = $translation->getContext();
40+
41+
if ($context !== null && $context !== '') {
4042
$originalString = "{$context}\x04{$translation->getOriginal()}";
4143
} else {
4244
$originalString = $translation->getOriginal();
@@ -142,7 +144,7 @@ public function generateString(Translations $translations): string
142144

143145
private static function hasPluralTranslations(Translation $translation): bool
144146
{
145-
if (!$translation->getPlural()) {
147+
if ($translation->getPlural() === null || $translation->getPlural() === '') {
146148
return false;
147149
}
148150

‎src/Generator/PoGenerator.php‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,38 @@ public function generateString(Translations $translations): string
6565

6666
$prefix = $translation->isDisabled() ? '#~ ' : '';
6767

68-
if ($context = $translation->getPreviousContext()) {
69-
$lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($context));
68+
$previousContext = $translation->getPreviousContext();
69+
if (self::hasValue($previousContext)) {
70+
$lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($previousContext));
7071
}
7172

72-
if ($original = $translation->getPreviousOriginal()) {
73-
$lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($original));
73+
$previousOriginal = $translation->getPreviousOriginal();
74+
if (self::hasValue($previousOriginal)) {
75+
$lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($previousOriginal));
7476
}
7577

76-
if ($plural = $translation->getPreviousPlural()) {
77-
$lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($plural));
78+
$previousPlural = $translation->getPreviousPlural();
79+
if (self::hasValue($previousPlural)) {
80+
$lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($previousPlural));
7881
}
7982

80-
if ($context = $translation->getContext()) {
83+
$context = $translation->getContext();
84+
if (self::hasValue($context)) {
8185
$lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context));
8286
}
8387

8488
self::appendLines($lines, $prefix, 'msgid', $translation->getOriginal());
8589

86-
if ($plural = $translation->getPlural()) {
90+
$plural = $translation->getPlural();
91+
if (self::hasValue($plural)) {
8792
self::appendLines($lines, $prefix, 'msgid_plural', $plural);
88-
self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?: '');
93+
self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?? '');
8994

9095
foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) {
9196
self::appendLines($lines, $prefix, sprintf('msgstr[%d]', $k + 1), $v);
9297
}
9398
} else {
94-
self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?: '');
99+
self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?? '');
95100
}
96101

97102
$lines[] = '';
@@ -100,6 +105,16 @@ public function generateString(Translations $translations): string
100105
return implode("\n", $lines);
101106
}
102107

108+
/**
109+
* Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value.
110+
*
111+
* @phpstan-assert-if-true string $value
112+
*/
113+
private static function hasValue(?string $value): bool
114+
{
115+
return $value !== null && $value !== '';
116+
}
117+
103118
/**
104119
* Add one or more lines depending whether the string is multiline or not.
105120
*/

‎src/Loader/PoLoader.php‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,10 @@ public static function decode(string $value): string
218218

219219
private static function isEmpty(Translation $translation): bool
220220
{
221-
if (!empty($translation->getOriginal())) {
221+
if ($translation->getOriginal() !== '') {
222222
return false;
223223
}
224224

225-
if (!empty($translation->getTranslation())) {
226-
return false;
227-
}
228-
229-
return true;
225+
return !$translation->isTranslated();
230226
}
231227
}

‎src/Translation.php‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,23 +261,23 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat
261261

262262
$override = (bool) ($strategy & Merge::TRANSLATIONS_OVERRIDE);
263263

264-
if (!$merged->translation || ($translation->translation && $override)) {
264+
if (!self::hasValue($merged->translation) || (self::hasValue($translation->translation) && $override)) {
265265
$merged->translation = $translation->translation;
266266
}
267267

268-
if (!$merged->plural || ($translation->plural && $override)) {
268+
if (!self::hasValue($merged->plural) || (self::hasValue($translation->plural) && $override)) {
269269
$merged->plural = $translation->plural;
270270
}
271271

272-
if (!$merged->previousContext || ($translation->previousContext && $override)) {
272+
if (!self::hasValue($merged->previousContext) || (self::hasValue($translation->previousContext) && $override)) {
273273
$merged->previousContext = $translation->previousContext;
274274
}
275275

276-
if (!$merged->previousOriginal || ($translation->previousOriginal && $override)) {
276+
if (!self::hasValue($merged->previousOriginal) || (self::hasValue($translation->previousOriginal) && $override)) {
277277
$merged->previousOriginal = $translation->previousOriginal;
278278
}
279279

280-
if (!$merged->previousPlural || ($translation->previousPlural && $override)) {
280+
if (!self::hasValue($merged->previousPlural) || (self::hasValue($translation->previousPlural) && $override)) {
281281
$merged->previousPlural = $translation->previousPlural;
282282
}
283283

@@ -289,4 +289,12 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat
289289

290290
return $merged;
291291
}
292+
293+
/**
294+
* Checks whether a string is set and not empty. Unlike a truthiness check, "0" is a valid value.
295+
*/
296+
private static function hasValue(?string $value): bool
297+
{
298+
return $value !== null && $value !== '';
299+
}
292300
}

‎tests/BasePoLoaderTestCase.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,37 @@ public function testMultilineDisabledTranslations(): void
286286
$translation->getTranslation()
287287
);
288288
}
289+
290+
public function testZeroStrings(): void
291+
{
292+
$po = <<<'EOT'
293+
msgid ""
294+
msgstr ""
295+
"Language: gl\n"
296+
297+
msgid "0"
298+
msgstr ""
299+
300+
msgctxt "start of week"
301+
msgid "1"
302+
msgstr "0"
303+
304+
msgctxt "0"
305+
msgid "Zero context"
306+
msgstr "Translated"
307+
EOT;
308+
$translations = $this->createPoLoader()->loadString($po);
309+
310+
$this->assertCount(3, $translations);
311+
312+
$translation = $translations->find(null, '0');
313+
$this->assertNotNull($translation);
314+
$this->assertFalse($translation->isTranslated());
315+
316+
$translation = $translations->find('start of week', '1');
317+
$this->assertNotNull($translation);
318+
$this->assertSame('0', $translation->getTranslation());
319+
320+
$this->assertNotNull($translations->find('0', 'Zero context'));
321+
}
289322
}

‎tests/MoGeneratorTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,32 @@ public function testMoGenerator(): void
5454
$this->assertCount(4, $result);
5555
$this->assertCount(5, $result->getHeaders());
5656
}
57+
58+
public function testZeroStrings(): void
59+
{
60+
$translations = Translations::create();
61+
62+
$translation = Translation::create('0', '1');
63+
$translation->translate('0');
64+
$translations->add($translation);
65+
66+
$translation = Translation::create(null, 'One');
67+
$translation->setPlural('0');
68+
$translation->translate('Singular');
69+
$translation->translatePlural('Plural');
70+
$translations->add($translation);
71+
72+
$result = (new MoLoader())->loadString((new MoGenerator())->generateString($translations));
73+
74+
$this->assertCount(2, $result);
75+
76+
$translation = $result->find('0', '1');
77+
$this->assertNotNull($translation);
78+
$this->assertSame('0', $translation->getTranslation());
79+
80+
$translation = $result->find(null, 'One');
81+
$this->assertNotNull($translation);
82+
$this->assertSame('0', $translation->getPlural());
83+
$this->assertSame(['Plural'], $translation->getPluralTranslations());
84+
}
5785
}

‎tests/PoGeneratorTest.php‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,40 @@ public function testStringEncode(string $encoded, string $decoded): void
114114
{
115115
$this->assertSame($encoded, PoGenerator::encode($decoded));
116116
}
117+
118+
public function testZeroStrings(): void
119+
{
120+
$translations = Translations::create();
121+
122+
$translation = Translation::create('0', '1');
123+
$translation->translate('0');
124+
$translation->setPreviousContext('0');
125+
$translation->setPreviousOriginal('0');
126+
$translations->add($translation);
127+
128+
$translation = Translation::create(null, 'One');
129+
$translation->setPlural('0');
130+
$translation->translate('0');
131+
$translation->translatePlural('0');
132+
$translations->add($translation);
133+
134+
$expected = <<<'EOT'
135+
msgid ""
136+
msgstr ""
137+
138+
#| msgctxt "0"
139+
#| msgid "0"
140+
msgctxt "0"
141+
msgid "1"
142+
msgstr "0"
143+
144+
msgid "One"
145+
msgid_plural "0"
146+
msgstr[0] "0"
147+
msgstr[1] "0"
148+
149+
EOT;
150+
151+
$this->assertSame($expected, (new PoGenerator())->generateString($translations));
152+
}
117153
}

‎tests/TranslationTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Gettext\Comments;
77
use Gettext\Flags;
8+
use Gettext\Merge;
89
use Gettext\References;
910
use Gettext\Translation;
1011
use PHPUnit\Framework\TestCase;
@@ -99,4 +100,29 @@ public function testMergeTranslation(): void
99100
$this->assertNotSame($merged, $translation1);
100101
$this->assertNotSame($merged, $translation2);
101102
}
103+
104+
public function testMergeKeepsZeroStrings(): void
105+
{
106+
$translation1 = Translation::create(null, '1');
107+
$translation1->setPlural('0');
108+
$translation1->translate('0');
109+
110+
$translation2 = Translation::create(null, '1');
111+
$translation2->setPlural('Plural');
112+
$translation2->translate('One');
113+
114+
$merged = $translation1->mergeWith($translation2);
115+
116+
$this->assertSame('0', $merged->getTranslation());
117+
$this->assertSame('0', $merged->getPlural());
118+
119+
$merged = $translation1->mergeWith(Translation::create(null, '1'), Merge::TRANSLATIONS_OVERRIDE);
120+
121+
$this->assertSame('0', $merged->getTranslation());
122+
123+
$merged = $translation2->mergeWith($translation1, Merge::TRANSLATIONS_OVERRIDE);
124+
125+
$this->assertSame('0', $merged->getTranslation());
126+
$this->assertSame('0', $merged->getPlural());
127+
}
102128
}

0 commit comments

Comments
 (0)