Skip to content

Commit

Permalink
Translation: Fix that null as count is still passed to the translator
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 10, 2023
1 parent 8d4a473 commit 24887b4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public function translateInDomain($domain, $message, $context = null)
*
* @param string $singular Singular message
* @param string $plural Plural message
* @param int $number Number to decide between the returned singular and plural forms
* @param ?int $number Number to decide between the returned singular and plural forms
* @param string $context Message context
*
* @return string Translated message or original message if no translation is found
*/
public function translatePlural($singular, $plural, $number, $context = null)
{
return $this->translationDomain === null
? StaticTranslator::$instance->translatePlural($singular, $plural, $number, $context)
? StaticTranslator::$instance->translatePlural($singular, $plural, $number ?? 0, $context)
: StaticTranslator::$instance->translatePluralInDomain(
$this->translationDomain,
$singular,
Expand All @@ -83,7 +83,7 @@ public function translatePlural($singular, $plural, $number, $context = null)
* @param string $domain
* @param string $singular Singular message
* @param string $plural Plural message
* @param int $number Number to decide between the returned singular and plural forms
* @param ?int $number Number to decide between the returned singular and plural forms
* @param string $context Message context
*
* @return string Translated message or original message if no translation is found
Expand Down
58 changes: 58 additions & 0 deletions tests/TranslationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace ipl\Tests\I18n;

use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;
use ipl\I18n\Translation;
use PHPUnit\Framework\TestCase;

class TranslationTest extends TestCase
{
protected function setUp(): void
{
StaticTranslator::$instance = new NoopTranslator();
}

public function testNullCountsAreProperlyHandledByTranslatePlural()
{
$this->assertSame(
'not one',
$this->createTestClassWithoutDomain()
->translatePlural('one', 'not one', null)
);
$this->assertSame(
'not one',
$this->createTestClassWithDomain()
->translatePlural('one', 'not one', null)
);
}

public function testNullCountsAreProperlyHandledByTranslatePluralInDomain()
{
$this->assertSame(
'not one',
$this->createTestClassWithoutDomain()
->translatePluralInDomain('test', 'one', 'not one', null)
);
}

private function createTestClassWithoutDomain()
{
return new class {
use Translation;
};
}

private function createTestClassWithDomain()
{
return new class {
use Translation;

public function __construct()
{
$this->translationDomain = 'test';
}
};
}
}

0 comments on commit 24887b4

Please sign in to comment.