Skip to content

Commit

Permalink
Merge pull request getkirby#4162 from getkirby/fix/4160-t-tc-locale
Browse files Browse the repository at this point in the history
Support `$locale` paramenter in `t()` and `tc()`
  • Loading branch information
distantnative authored Feb 17, 2022
2 parents 0c3bc1b + e96968e commit 01c4d2f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
11 changes: 7 additions & 4 deletions config/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,23 +765,26 @@ function svg($file)
*
* @param string|array $key
* @param string|null $fallback
* @param string|null $locale
* @return array|string|null
*/
function t($key, string $fallback = null)
function t($key, string $fallback = null, string $locale = null)
{
return I18n::translate($key, $fallback);
return I18n::translate($key, $fallback, $locale);
}

/**
* Translates a count
*
* @param string $key
* @param int $count
* @param string|null $locale
* @param bool $formatNumber If set to `false`, the count is not formatted
* @return mixed
*/
function tc(string $key, int $count)
function tc(string $key, int $count, string $locale = null, bool $formatNumber = true)
{
return I18n::translateCount($key, $count);
return I18n::translateCount($key, $count, $locale, $formatNumber);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/Cms/Helpers/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,47 @@ public function testTimestampWithInvalidDate()
$this->assertNull($result);
}

public function testTcHelper()
{
$this->kirby->clone([
'translations' => [
'en' => [
'car' => ['No cars', 'One car', 'Two cars', 'Many cars']
]
]
]);

$this->assertSame('No cars', tc('car', 0));
$this->assertSame('One car', tc('car', 1));
$this->assertSame('Two cars', tc('car', 2));
$this->assertSame('Many cars', tc('car', 3));
$this->assertSame('Many cars', tc('car', 4));
}

public function testTcHelperWithPlaceholders()
{
$this->kirby->clone([
'translations' => [
'en' => [
'car' => ['No cars', 'One car', '{{ count }} cars']
],
'de' => [
'car' => ['Keine Autos', 'Ein Auto', '{{ count }} Autos']
]
]
]);

$this->assertSame('2 cars', tc('car', 2));
$this->assertSame('3 cars', tc('car', 3));
$this->assertSame('1,234,567 cars', tc('car', 1234567));
$this->assertSame('1,234,567 cars', tc('car', 1234567, null));
$this->assertSame('1,234,567 cars', tc('car', 1234567, null, true));
$this->assertSame('1234567 cars', tc('car', 1234567, null, false));
$this->assertSame('1.234.567 Autos', tc('car', 1234567, 'de'));
$this->assertSame('1.234.567 Autos', tc('car', 1234567, 'de', true));
$this->assertSame('1234567 Autos', tc('car', 1234567, 'de', false));
}

public function testTwitter()
{
// simple
Expand Down

0 comments on commit 01c4d2f

Please sign in to comment.