Skip to content

Commit 56f367f

Browse files
committed
Add Language fallback
1 parent 41065e7 commit 56f367f

3 files changed

Lines changed: 74 additions & 15 deletions

File tree

system/Language/Language.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,32 @@ public function getLine(string $line, array $args = [])
115115
{
116116
// Parse out the file name and the actual alias.
117117
// Will load the language file and strings.
118-
list($file, $parsedLine) = $this->parseLine($line);
118+
[
119+
$file,
120+
$parsedLine,
121+
] = $this->parseLine($line, $this->locale);
122+
123+
$output = $this->language[$this->locale][$file][$parsedLine] ?? null;
119124

120-
$output = $this->language[$this->locale][$file][$parsedLine] ?? $line;
125+
if (empty($output) && strpos($this->locale, '-'))
126+
{
127+
[$locale] = explode('-', $this->locale, 2);
128+
129+
[
130+
$file,
131+
$parsedLine,
132+
] = $this->parseLine($line, $locale);
133+
134+
$output = $this->language[$locale][$file][$parsedLine] ?? null;
135+
}
136+
137+
$output = $output ?? $line;
121138

122139
if (! empty($args))
123140
{
124141
$output = $this->formatMessage($output, $args);
125142
}
143+
126144
return $output;
127145
}
128146

@@ -133,10 +151,11 @@ public function getLine(string $line, array $args = [])
133151
* filename as the first segment (separated by period).
134152
*
135153
* @param string $line
154+
* @param string $locale
136155
*
137156
* @return array
138157
*/
139-
protected function parseLine(string $line): array
158+
protected function parseLine(string $line, string $locale): array
140159
{
141160
// If there's no possibility of a filename being in the string
142161
// simply return the string, and they can parse the replacement
@@ -152,9 +171,9 @@ protected function parseLine(string $line): array
152171
$file = substr($line, 0, strpos($line, '.'));
153172
$line = substr($line, strlen($file) + 1);
154173

155-
if (! isset($this->language[$this->locale][$file]) || ! array_key_exists($line, $this->language[$this->locale][$file]))
174+
if (! isset($this->language[$locale][$file]) || ! array_key_exists($line, $this->language[$locale][$file]))
156175
{
157-
$this->load($file, $this->locale);
176+
$this->load($file, $locale);
158177
}
159178

160179
return [

tests/_support/Language/MockLanguage.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ class MockLanguage extends Language
2020
* 'requireFile()' method to allow easy overrides
2121
* during testing.
2222
*
23-
* @param $data
23+
* @param array $data
24+
* @param string $file
25+
* @param string|null $locale
2426
*
2527
* @return $this
2628
*/
27-
public function setData($data)
29+
public function setData(string $file, array $data, string $locale = null)
2830
{
29-
$this->data = $data;
31+
$this->language[$locale ?? $this->locale][$file] = $data;
3032

3133
return $this;
3234
}

tests/system/Language/LanguageTest.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testGetLineReturnsLine()
1919
{
2020
$lang = new MockLanguage('en');
2121

22-
$lang->setData([
22+
$lang->setData('books', [
2323
'bookSaved' => 'We kept the book free from the boogeyman',
2424
'booksSaved' => 'We saved some more',
2525
]);
@@ -29,11 +29,49 @@ public function testGetLineReturnsLine()
2929

3030
//--------------------------------------------------------------------
3131

32+
public function testGetLineReturnsFallbackLine()
33+
{
34+
$lang = new MockLanguage('en-US');
35+
$lang->setData('equivalent', [
36+
'touchWood' => 'touch wood',
37+
'lieOfLand' => 'lie of the land',
38+
'leaseOfLife' => 'a new lease of life',
39+
'slowcoach' => 'slowcoach',
40+
], 'en');
41+
$lang->setData('equivalent', [
42+
'lieOfLand' => 'lay of the land',
43+
'slowcoach' => 'slowpoke',
44+
], 'en-US');
45+
46+
$this->assertEquals(
47+
'lay of the land',
48+
$lang->getLine('equivalent.lieOfLand')
49+
);
50+
$this->assertEquals(
51+
'slowpoke',
52+
$lang->getLine('equivalent.slowcoach')
53+
);
54+
$this->assertEquals(
55+
'a new lease of life',
56+
$lang->getLine('equivalent.leaseOfLife')
57+
);
58+
$this->assertEquals(
59+
'touch wood',
60+
$lang->getLine('equivalent.touchWood')
61+
);
62+
$this->assertEquals(
63+
'equivalent.unknown',
64+
$lang->getLine('equivalent.unknown')
65+
);
66+
}
67+
68+
//--------------------------------------------------------------------
69+
3270
public function testGetLineArrayReturnsLineArray()
3371
{
3472
$lang = new MockLanguage('en');
3573

36-
$lang->setData([
74+
$lang->setData('books', [
3775
'booksList' => [
3876
'The Boogeyman',
3977
'We Saved',
@@ -58,7 +96,7 @@ public function testGetLineFormatsMessage()
5896

5997
$lang = new MockLanguage('en');
6098

61-
$lang->setData([
99+
$lang->setData('books', [
62100
'bookCount' => '{0, number, integer} books have been saved.',
63101
]);
64102

@@ -77,7 +115,7 @@ public function testGetLineArrayFormatsMessages()
77115

78116
$lang = new MockLanguage('en');
79117

80-
$lang->setData([
118+
$lang->setData('books', [
81119
'bookList' => [
82120
'{0, number, integer} related books.'
83121
],
@@ -107,7 +145,7 @@ public function testLangDoesntFormat()
107145
$lang = new MockLanguage('en');
108146
$lang->disableIntlSupport();
109147

110-
$lang->setData([
148+
$lang->setData('books', [
111149
'bookList' => [
112150
'{0, number, integer} related books.'
113151
],
@@ -160,13 +198,13 @@ public function testLanguageSameKeyAndFileName()
160198
$lang = new MockLanguage('en');
161199

162200
// first file data | example.message
163-
$lang->setData(['message' => 'This is an example message']);
201+
$lang->setData('example', ['message' => 'This is an example message']);
164202

165203
// force loading data into file Example
166204
$this->assertEquals('This is an example message', $lang->getLine('example.message'));
167205

168206
// second file data | another.example
169-
$lang->setData(['example' => 'Another example']);
207+
$lang->setData('another', ['example' => 'Another example']);
170208

171209
$this->assertEquals('Another example', $lang->getLine('another.example'));
172210
}

0 commit comments

Comments
 (0)