Skip to content

Commit 11e1faa

Browse files
authored
Merge pull request #6209 from kenjis/fix-lang-on-cli
fix: lang() causes Error on CLI
2 parents 516e7dd + a2a23a6 commit 11e1faa

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

system/Config/Services.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
use Config\Toolbar as ToolbarConfig;
7676
use Config\Validation as ValidationConfig;
7777
use Config\View as ViewConfig;
78+
use Locale;
7879

7980
/**
8081
* Services Configuration file.
@@ -363,8 +364,14 @@ public static function language(?string $locale = null, bool $getShared = true)
363364
return static::getSharedInstance('language', $locale)->setLocale($locale);
364365
}
365366

367+
if (AppServices::request() instanceof IncomingRequest) {
368+
$requestLocale = AppServices::request()->getLocale();
369+
} else {
370+
$requestLocale = Locale::getDefault();
371+
}
372+
366373
// Use '?:' for empty string check
367-
$locale = $locale ?: AppServices::request()->getLocale();
374+
$locale = $locale ?: $requestLocale;
368375

369376
return new Language($locale);
370377
}

tests/system/CommonFunctionsTest.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class CommonFunctionsTest extends CIUnitTestCase
5050
protected function setUp(): void
5151
{
5252
unset($_ENV['foo'], $_SERVER['foo']);
53-
Services::reset();
53+
$this->resetServices();
5454

5555
parent::setUp();
5656
}
@@ -593,8 +593,6 @@ public function testTraceWithCSP()
593593

594594
public function testCspStyleNonce()
595595
{
596-
$this->resetServices();
597-
598596
$config = config('App');
599597
$config->CSPEnabled = true;
600598

@@ -603,11 +601,20 @@ public function testCspStyleNonce()
603601

604602
public function testCspScriptNonce()
605603
{
606-
$this->resetServices();
607-
608604
$config = config('App');
609605
$config->CSPEnabled = true;
610606

611607
$this->assertStringStartsWith('nonce="', csp_script_nonce());
612608
}
609+
610+
public function testLangOnCLI()
611+
{
612+
Services::createRequest(new App(), true);
613+
614+
$message = lang('CLI.generator.fileCreate', ['TestController.php']);
615+
616+
$this->assertSame('File created: TestController.php', $message);
617+
618+
$this->resetServices();
619+
}
613620
}

0 commit comments

Comments
 (0)