diff --git a/system/Config/Services.php b/system/Config/Services.php index 69b666ff04d4..169795c77a44 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -75,6 +75,7 @@ use Config\Toolbar as ToolbarConfig; use Config\Validation as ValidationConfig; use Config\View as ViewConfig; +use Locale; /** * Services Configuration file. @@ -363,8 +364,14 @@ public static function language(?string $locale = null, bool $getShared = true) return static::getSharedInstance('language', $locale)->setLocale($locale); } + if (AppServices::request() instanceof IncomingRequest) { + $requestLocale = AppServices::request()->getLocale(); + } else { + $requestLocale = Locale::getDefault(); + } + // Use '?:' for empty string check - $locale = $locale ?: AppServices::request()->getLocale(); + $locale = $locale ?: $requestLocale; return new Language($locale); } diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index c41952fd7554..5c2b6efa7f83 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -50,7 +50,7 @@ final class CommonFunctionsTest extends CIUnitTestCase protected function setUp(): void { unset($_ENV['foo'], $_SERVER['foo']); - Services::reset(); + $this->resetServices(); parent::setUp(); } @@ -593,8 +593,6 @@ public function testTraceWithCSP() public function testCspStyleNonce() { - $this->resetServices(); - $config = config('App'); $config->CSPEnabled = true; @@ -603,11 +601,20 @@ public function testCspStyleNonce() public function testCspScriptNonce() { - $this->resetServices(); - $config = config('App'); $config->CSPEnabled = true; $this->assertStringStartsWith('nonce="', csp_script_nonce()); } + + public function testLangOnCLI() + { + Services::createRequest(new App(), true); + + $message = lang('CLI.generator.fileCreate', ['TestController.php']); + + $this->assertSame('File created: TestController.php', $message); + + $this->resetServices(); + } }