-
-
Notifications
You must be signed in to change notification settings - Fork 750
/
TranslationsTest.php
120 lines (112 loc) · 4.91 KB
/
TranslationsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
// load functions
require_once dirname(__DIR__, 1) . "/vendor/autoload.php";
final class TranslationsTest extends TestCase
{
/**
* Test that all locales only contain phrases appearing for the locale "en" or "date_format"
*/
public function testAllPhrasesValid(): void
{
$translations = include "src/translations.php";
$locales = array_keys($translations);
$valid_phrases = [
"rtl",
"date_format",
"Total Contributions",
"Current Streak",
"Longest Streak",
"Week Streak",
"Longest Week Streak",
"Present",
"Excluding {days}",
"comma_separator",
];
foreach ($locales as $locale) {
// if it is a string, assert that the alias exists in the translations file
if (is_string($translations[$locale])) {
$alias = $translations[$locale];
$this->assertArrayHasKey($alias, $translations, "Alias '{$alias}' does not exist for locale '$locale'");
}
// otherwise, assert that all phrases are valid
else {
$phrases = array_keys($translations[$locale]);
$this->assertEmpty(array_diff($phrases, $valid_phrases), "Locale '$locale' contains invalid phrases");
}
}
}
/**
* Test that "en" is first and the remaining locales are sorted alphabetically
*/
public function testLocalesSortedAlphabetically(): void
{
$translations = include "src/translations.php";
$locales = array_keys($translations);
// check that "en" is first
$this->assertEquals("en", $locales[0]);
// check that the remaining locales are sorted alphabetically
$remaining_locales = array_slice($locales, 1);
$sorted_locales = call_user_func(function (array $arr) {
asort($arr);
return $arr;
}, $remaining_locales);
// check that the remaining locales are sorted alphabetically
$sorted = implode(", ", $sorted_locales);
$remaining = implode(", ", $remaining_locales);
$this->assertEquals($sorted, $remaining, "Locales are not sorted alphabetically");
}
/**
* Test that all keys are normalized - lowercase language code, title case script code, uppercase region code
*/
public function testKeysNormalized(): void
{
$translations = include "src/translations.php";
$locales = array_keys($translations);
foreach ($locales as $locale) {
// normalize locale code
$normalized = normalizeLocaleCode($locale);
// check that the locale is normalized
$this->assertEquals($locale, $normalized, "Locale '$locale' is not normalized. Expected '$normalized'.");
}
}
/**
* Test getTranslations() function
*/
public function testGetTranslations(): void
{
$translations = include "src/translations.php";
$en = $translations["en"];
// test alias
$this->assertEquals($translations["zh_Hans"] + $en, getTranslations("zh"), "Alias not resolved");
// test locale not normalized with script
$this->assertEquals($translations["zh_Hans"] + $en, getTranslations("zh-hans"), "Locale script not normalized");
// test locale not normalized with region
$this->assertEquals($translations["pt_BR"] + $en, getTranslations("pt-br"), "Locale region not normalized");
// test locale region not in translations, but language is
$this->assertEquals($translations["fr"] + $en, getTranslations("fr_XX"), "Locale missing region not resolved");
// test locale not found
$this->assertEquals($en, getTranslations("xx"), "Locale not found");
}
/**
* Test normalizeLocaleCode() function
*/
public function testNormalizeLocaleCode(): void
{
// test script not normalized
$this->assertEquals("zh_Hans", normalizeLocaleCode("zh_hans"), "Script not normalized");
// test region not normalized
$this->assertEquals("pt_BR", normalizeLocaleCode("pt_br"), "Region not normalized");
// test script and region not normalized
$this->assertEquals("zh_Hans_CN", normalizeLocaleCode("zh_hans_cn"), "Script and region not normalized");
// test numeric region
$this->assertEquals("es_419", normalizeLocaleCode("es_419"), "Numeric region not normalized");
// test dashes instead of underscores
$this->assertEquals("zh_Hans", normalizeLocaleCode("zh-hans"), "Dashes not normalized");
// test uppercase
$this->assertEquals("zh_Hans", normalizeLocaleCode("ZH-HANS"), "Uppercase not normalized");
// test invalid locale format
$this->assertEquals("en", normalizeLocaleCode("xxxx-XXX-XXXXX"), "Invalid locale format not normalized");
}
}