Skip to content

Commit

Permalink
Merge branch 'KnpLabs:main' into durationNB
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaaje authored Jul 31, 2023
2 parents 5213801 + 166a768 commit 45de8bf
Show file tree
Hide file tree
Showing 6 changed files with 53 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.
20 changes: 20 additions & 0 deletions translations/time.ru.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@
<source>diff.in.year</source>
<target>через %count% год|через %count% года|через %count% лет</target>
</trans-unit>
<trans-unit id="14">
<source>duration.second</source>
<target>1 секунда|%count% секунд</target>
</trans-unit>
<trans-unit id="15">
<source>duration.minute</source>
<target>1 минута|%count% минут</target>
</trans-unit>
<trans-unit id="16">
<source>duration.hour</source>
<target>1 час|%count% часов</target>
</trans-unit>
<trans-unit id="17">
<source>duration.day</source>
<target>1 день|%count% дней</target>
</trans-unit>
<trans-unit id="18">
<source>duration.none</source>
<target>&lt; 1 секунды</target>
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 45de8bf

Please sign in to comment.