Skip to content

Commit

Permalink
fix: pluralization not being used for duration translations
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored and weaverryan committed Jul 31, 2023
1 parent 3fe7960 commit 924d84b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function formatDuration(float $seconds, string $locale = null): string
|| $index === \count($timeFormats) - 1
) {
if (2 === \count($format)) {
return $this->translator->trans($format[1], [], 'time', $locale);
return $this->translator->trans($format[1], ['%count%' => 1], 'time', $locale);
}

return $this->translator->trans(
Expand Down
45 changes: 27 additions & 18 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,40 @@ final class IntegrationTest extends TestCase
{
private TimeBundleIntegrationTestKernel $kernel;

public function testServiceWiring(): void
public function testTimeDiffTwigExtension(): void
{
$result = $this->kernel->getContainer()->get('public.twig')->render('@integration_test/template.twig', [
$result = $this->kernel->getContainer()->get('public.twig')->render('@integration_test/time_diff.twig', [
'yesterday' => (new \DateTime('-1 day')),
]);

$this->assertStringContainsString('Yesterday: 1 day ago', $result);
$this->assertStringContainsString('Now: now', $result);

$this->assertStringContainsString('1 day ago', $result);
$this->assertStringContainsString('Zero: < 1 second', $result);
$this->assertStringContainsString('Less than a second: < 1 second', $result);
$this->assertStringContainsString('One second: 1 second', $result);
$this->assertStringContainsString('Multiple seconds: 59 seconds', $result);
$this->assertStringContainsString('One minute: 1 minute', $result);
$this->assertStringContainsString('Multiple minutes: 59 minutes', $result);
$this->assertStringContainsString('One hour: 1 hour', $result);
$this->assertStringContainsString('Multiple hours: 23 hours', $result);
$this->assertStringContainsString('One day: 1 day', $result);
$this->assertStringContainsString('Multiple days: 99 days', $result);
$this->assertStringContainsString("Yesterday: 1 day ago\n", $result);
$this->assertStringContainsString("Now: now\n", $result);
}

public function testLocalTranslation(): void
public function testDurationTwigExtension(): void
{
$result = $this->kernel->getContainer()->get('public.twig')->render('@integration_test/templateSpecificLocale.twig', [
$result = $this->kernel->getContainer()->get('public.twig')->render('@integration_test/duration.twig', [
'yesterday' => (new \DateTime('-1 day')),
]);

$this->assertStringContainsString("Zero: < 1 second\n", $result);
$this->assertStringContainsString("Less than a second: < 1 second\n", $result);
$this->assertStringContainsString("One second: 1 second\n", $result);
$this->assertStringContainsString("Multiple seconds: 59 seconds\n", $result);
$this->assertStringContainsString("One minute: 1 minute\n", $result);
$this->assertStringContainsString("Over one minute: 1 minute\n", $result);
$this->assertStringContainsString("Multiple minutes: 59 minutes\n", $result);
$this->assertStringContainsString("One hour: 1 hour\n", $result);
$this->assertStringContainsString("Over one hour: 1 hour\n", $result);
$this->assertStringContainsString("Multiple hours: 23 hours\n", $result);
$this->assertStringContainsString("One day: 1 day\n", $result);
$this->assertStringContainsString("Over one day: 1 day\n", $result);
$this->assertStringContainsString("Multiple days: 99 days\n", $result);
}

public function testTimeDiffLocalTranslation(): void
{
$result = $this->kernel->getContainer()->get('public.twig')->render('@integration_test/time_diff_specific_locale.twig', [
'yesterday' => (new \DateTime('-1 day')),
'monthAgo' => (new \DateTime('-32 days')),
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/template.twig → tests/fixtures/duration.twig
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Yesterday: {{ yesterday|ago }}
Now: {{ date()|time_diff }}

Zero: {{ 0|duration }}
Less than a second: {{ 0.1|duration }}
One second: {{ 1|duration }}
Multiple seconds: {{ 59|duration }}
One minute: {{ 60|duration }}
Over one minute: {{ 61|duration }}
Multiple minutes: {{ 3599|duration }}
One hour: {{ 3600|duration }}
Over one hour: {{ 3700|duration }}
Multiple hours: {{ 86399|duration }}
One day: {{ 86400|duration }}
Over one day: {{ 88458|duration }}
Multiple days: {{ 8554100|duration }}
2 changes: 2 additions & 0 deletions tests/fixtures/time_diff.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Yesterday: {{ yesterday|ago }}
Now: {{ date()|time_diff }}
File renamed without changes.

0 comments on commit 924d84b

Please sign in to comment.