Skip to content

Commit d1f1f6b

Browse files
committed
fix: $this->request->setLocale() does not work for lang() in 404 controller
Fixes #5261
1 parent 4671341 commit d1f1f6b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

system/Exceptions/PageNotFoundException.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Exceptions;
1313

14+
use Config\Services;
1415
use OutOfBoundsException;
1516

1617
class PageNotFoundException extends OutOfBoundsException implements ExceptionInterface
@@ -26,21 +27,37 @@ class PageNotFoundException extends OutOfBoundsException implements ExceptionInt
2627

2728
public static function forPageNotFound(?string $message = null)
2829
{
29-
return new static($message ?? lang('HTTP.pageNotFound'));
30+
return new static($message ?? self::lang('HTTP.pageNotFound'));
3031
}
3132

3233
public static function forEmptyController()
3334
{
34-
return new static(lang('HTTP.emptyController'));
35+
return new static(self::lang('HTTP.emptyController'));
3536
}
3637

3738
public static function forControllerNotFound(string $controller, string $method)
3839
{
39-
return new static(lang('HTTP.controllerNotFound', [$controller, $method]));
40+
return new static(self::lang('HTTP.controllerNotFound', [$controller, $method]));
4041
}
4142

4243
public static function forMethodNotFound(string $method)
4344
{
44-
return new static(lang('HTTP.methodNotFound', [$method]));
45+
return new static(self::lang('HTTP.methodNotFound', [$method]));
46+
}
47+
48+
/**
49+
* Get translated system message
50+
*
51+
* Use a non-shared Language instance in the Services.
52+
* If a shared instance is created, the Language will
53+
* have the current locale, so even if users call
54+
* `$this->request->setLocale()` in the controller afterwards,
55+
* the Language locale will not be changed.
56+
*/
57+
private static function lang(string $line, array $args = []): string
58+
{
59+
$lang = Services::language(null, false);
60+
61+
return $lang->getLine($line, $args);
4562
}
4663
}

0 commit comments

Comments
 (0)