Skip to content

Commit 7c27d26

Browse files
committed
Fixed language locale setting issue
Attempted to access an array that had been filtered and therefore could have holes within, including as position 0 which would then be accessed. Also added cs language to internal map Related to BookStackApp#4068
1 parent c7e33d1 commit 7c27d26

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

app/Util/LanguageManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class LanguageManager
2424
'bg' => ['iso' => 'bg_BG', 'windows' => 'Bulgarian'],
2525
'bs' => ['iso' => 'bs_BA', 'windows' => 'Bosnian (Latin)'],
2626
'ca' => ['iso' => 'ca', 'windows' => 'Catalan'],
27+
'cs' => ['iso' => 'cs_CZ', 'windows' => 'Czech'],
2728
'da' => ['iso' => 'da_DK', 'windows' => 'Danish'],
2829
'de' => ['iso' => 'de_DE', 'windows' => 'German'],
2930
'de_informal' => ['iso' => 'de_DE', 'windows' => 'German'],
@@ -120,14 +121,14 @@ public function setPhpDateTimeLocale(string $language): void
120121
$isoLang = $this->localeMap[$language]['iso'] ?? '';
121122
$isoLangPrefix = explode('_', $isoLang)[0];
122123

123-
$locales = array_filter([
124+
$locales = array_values(array_filter([
124125
$isoLang ? $isoLang . '.utf8' : false,
125126
$isoLang ?: false,
126127
$isoLang ? str_replace('_', '-', $isoLang) : false,
127128
$isoLang ? $isoLangPrefix . '.UTF-8' : false,
128129
$this->localeMap[$language]['windows'] ?? false,
129130
$language,
130-
]);
131+
]));
131132

132133
if (!empty($locales)) {
133134
setlocale(LC_TIME, $locales[0], ...array_slice($locales, 1));

tests/LanguageTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class LanguageTest extends TestCase
66
{
7-
protected $langs;
7+
protected array $langs;
88

99
/**
1010
* LanguageTest constructor.
@@ -81,4 +81,13 @@ public function test_rtl_config_set_if_lang_is_rtl()
8181
$this->get('/');
8282
$this->assertTrue(config('app.rtl'), 'App RTL config should have been set to true by middleware');
8383
}
84+
85+
public function test_unknown_lang_does_not_break_app()
86+
{
87+
config()->set('app.locale', 'zz');
88+
89+
$loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
90+
$loginReq->assertOk();
91+
$loginReq->assertSee('Log In');
92+
}
8493
}

0 commit comments

Comments
 (0)