Split user language (UI translation) from locale (formatted values) (#4595)

This commit is contained in:
Kevin Papst
2024-01-30 00:09:53 +01:00
committed by GitHub
parent 12ef19df28
commit df3ca9d5a9
39 changed files with 685 additions and 881 deletions

View File

@@ -17,65 +17,102 @@ use PHPUnit\Framework\TestCase;
*/
class LocaleServiceTest extends TestCase
{
protected function getSut(array $settings)
protected function getSut(array $settings): LocaleService
{
return new LocaleService($settings);
}
protected function getDefaultSettings()
/**
* @return array<string, array{'date': string, 'time': string, 'rtl': bool, 'translation': bool}>
*/
protected function getDefaultSettings(): array
{
return [
'de' => [
'date' => 'd.m.Y',
'duration' => '%h:%m',
'time' => 'H:i',
'rtl' => false,
'translation' => true,
],
'en' => [
'date' => 'Y-m-d',
'duration' => '%h:%m',
'time' => 'H:i:s',
'rtl' => false,
'translation' => true,
],
'en_AU' => [
'date' => 'Y-m-d',
'time' => 'H:i:s',
'rtl' => false,
'translation' => false,
],
'pt_BR' => [
'date' => 'd-m-Y',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
'it' => [
'date' => 'd.m.Y',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
'fr' => [
'date' => 'd/m/Y',
'duration' => '%h h %m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
'fr_BE' => [
'date' => 'd/MM/yy',
'time' => 'HH:mm',
'rtl' => false,
'translation' => false,
],
'fr_CA' => [
'date' => 'y-MM-dd',
'time' => 'HH \'h\' mm',
'rtl' => false,
'translation' => true,
],
'es' => [
'date' => 'd.m.Y',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
'ru' => [
'date' => 'd.m.Y',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
'ar' => [
'date' => 'Y-m-d',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => true,
'translation' => true,
],
'hu' => [
'date' => 'Y.m.d.',
'duration' => '%h:%m',
'time' => 'HH:mm',
'rtl' => false,
'translation' => true,
],
];
}
public function testGetAllLocales()
public function testGetAllLocales(): void
{
$sut = $this->getSut([]);
$this->assertEquals([], $sut->getAllLocales());
$sut = $this->getSut($this->getDefaultSettings());
$this->assertEquals(['de', 'en', 'pt_BR', 'it', 'fr', 'es', 'ru', 'ar', 'hu'], $sut->getAllLocales());
$this->assertEquals(['de', 'en', 'en_AU', 'pt_BR', 'it', 'fr', 'fr_BE', 'fr_CA', 'es', 'ru', 'ar', 'hu'], $sut->getAllLocales());
}
public function testInvalidLocaleWithGivenLocale()
public function testInvalidLocaleWithGivenLocale(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown locale given: xx');
@@ -84,28 +121,54 @@ class LocaleServiceTest extends TestCase
$sut->getDateFormat('xx');
}
public function testGetDurationFormat()
public function testGetDurationFormat(): void
{
$sut = $this->getSut($this->getDefaultSettings());
$this->assertEquals('%h:%m', $sut->getDurationFormat('de'));
}
public function testGetDateFormat()
public function testGetDateFormat(): void
{
$sut = $this->getSut($this->getDefaultSettings());
$this->assertEquals('d.m.Y', $sut->getDateFormat('de'));
}
public function testGetDateTimeFormat()
public function testGetDateTimeFormat(): void
{
$sut = $this->getSut($this->getDefaultSettings());
$this->assertEquals('d.m.Y H:i', $sut->getDateTimeFormat('de'));
}
public function testGetTimeFormat()
public function testGetTimeFormat(): void
{
$sut = $this->getSut($this->getDefaultSettings());
$this->assertEquals('H:i', $sut->getTimeFormat('de'));
$this->assertEquals('H:i:s', $sut->getTimeFormat('en'));
}
/**
* @dataProvider getNearestTranslationLocaleData
*/
public function testGetNearestTranslationLocale(string $locale, string $expected): void
{
$sut = $this->getSut($this->getDefaultSettings());
$actual = $sut->getNearestTranslationLocale($locale);
$this->assertEquals($expected, $actual);
}
/**
* @return array<int, array<int, string>>
*/
public function getNearestTranslationLocaleData(): array
{
return [
['de', 'de'], // registered and translated: use it
['pt_BR', 'pt_BR'], // registered and translated: use it
['fr_CA', 'fr_CA'], // registered and translated: use it
['fr_BE', 'fr'], // not translated, fallback to base locale
['fr_DZ', 'fr'], // not registered region locale, but fallback to base locale
['en_AU', 'en'], // not translated, fallback to base locale
['uk_UA', 'en'], // not registered locales fallback to en
];
}
}