Skip to content

Commit 4f43dfd

Browse files
authored
Check overrides for parent locale when compiling language files (#242)
Credit to @mjauvin and @jaxwilko
1 parent affdcce commit 4f43dfd

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

modules/system/console/WinterUtil.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,33 @@ protected function utilCompileLang()
194194
$srcPath = base_path() . '/modules/system/lang/'.$locale.'/client.php';
195195

196196
$messages = require $fallbackPath;
197+
197198
if (File::isFile($srcPath) && $fallbackPath != $srcPath) {
198199
$messages = array_replace_recursive($messages, require $srcPath);
199200
}
200201

201202
/*
202203
* Load possible replacements from /lang
203204
*/
205+
$overrides = [];
206+
$parentOverrides = [];
207+
204208
$overridePath = base_path() . '/lang/'.$locale.'/system/client.php';
205209
if (File::isFile($overridePath)) {
206-
$messages = array_replace_recursive($messages, require $overridePath);
210+
$overrides = require $overridePath;
207211
}
208212

213+
if (str_contains($locale, '-')) {
214+
list($parentLocale, $country) = explode('-', $locale);
215+
216+
$parentOverridePath = base_path() . '/lang/'.$parentLocale.'/system/client.php';
217+
if (File::isFile($parentOverridePath)) {
218+
$parentOverrides = require $parentOverridePath;
219+
}
220+
}
221+
222+
$messages = array_replace_recursive($messages, $parentOverrides, $overrides);
223+
209224
/*
210225
* Compile from stub and save file
211226
*/
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
class WinterUtilTest extends TestCase
4+
{
5+
public function testCompileLang()
6+
{
7+
$defaultClient = base_path('/modules/system/lang/en/client.php');
8+
9+
// mv file so we can inject a test
10+
rename($defaultClient, $defaultClient . '.backup');
11+
12+
file_put_contents($defaultClient, '<?php return [\'winter\' => \'is coming\'];');
13+
14+
// execute compile
15+
$this->artisan('winter:util compile lang')->execute();
16+
17+
// validate default lang handling
18+
$lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js'));
19+
$this->assertStringContainsString('winter', $lang);
20+
$this->assertStringContainsString('is coming', $lang);
21+
22+
// simulate override
23+
$created = [];
24+
25+
foreach (['lang/en/system', 'lang/en-gb/system'] as $slug) {
26+
$path = rtrim(base_path(), '/');
27+
foreach (explode('/', $slug) as $dir) {
28+
$path = $path . '/' . $dir;
29+
if (!is_dir($path)) {
30+
mkdir($path, 0755);
31+
$created[] = $path;
32+
}
33+
}
34+
}
35+
36+
$langClient = base_path('lang/en/system/client.php');
37+
// handle existing file
38+
if (file_exists($langClient)) {
39+
rename($langClient, $langClient . '.backup');
40+
}
41+
42+
file_put_contents($langClient, '<?php return [\'winter\' => \'is epic\'];');
43+
44+
$langCountryClient = base_path('lang/en-gb/system/client.php');
45+
// handle existing file
46+
if (file_exists($langCountryClient)) {
47+
rename($langCountryClient, $langCountryClient . '.backup');
48+
}
49+
50+
file_put_contents($langCountryClient, '<?php return [\'whats_epic\' => \'winter\'];');
51+
52+
// execute compile
53+
$this->artisan('winter:util compile lang')->execute();
54+
55+
// validate override handling
56+
$lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en.js'));
57+
$this->assertStringContainsString('winter', $lang);
58+
$this->assertStringContainsString('is epic', $lang);
59+
60+
// check that lang subset has included parent overrides
61+
$lang = file_get_contents(base_path('modules/system/assets/js/lang/lang.en-gb.js'));
62+
$this->assertStringContainsString('winter', $lang);
63+
$this->assertStringContainsString('is epic', $lang);
64+
$this->assertStringContainsString('whats_epic', $lang);
65+
$this->assertStringContainsString('winter', $lang);
66+
67+
// restore
68+
unlink($defaultClient);
69+
rename($defaultClient . '.backup', $defaultClient);
70+
71+
foreach ([$langClient, $langCountryClient] as $client) {
72+
unlink($client);
73+
if (file_exists($client . '.backup')) {
74+
rename($client . '.backup', $client);
75+
}
76+
}
77+
78+
foreach (array_reverse($created) as $dir) {
79+
rmdir($dir);
80+
}
81+
82+
// regenerate original compiled lang
83+
$this->artisan('winter:util compile lang')->execute();
84+
}
85+
}

0 commit comments

Comments
 (0)