Welcome Guest, Not a member yet? Register   Sign In
Why doesn't $request->getLocale() return the correct locale any more (since v.4.3)?
#2

In 4.4, I cannot reproduce.

Code:
diff --git a/app/Config/App.php b/app/Config/App.php
index 186bfa86bb..a234921df7 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -97,7 +97,7 @@ class App extends BaseConfig
      *
      * @var string[]
      */
-    public array $supportedLocales = ['en'];
+    public array $supportedLocales = ['en', 'fr'];

    /**
      * --------------------------------------------------------------------------
Code:
diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index fc4914a692..0dda555c44 100644
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -6,3 +6,7 @@ use CodeIgniter\Router\RouteCollection;
  * @var RouteCollection $routes
  */
$routes->get('/', 'Home::index');
+
+$routes->group('{locale}/cp', [], function($routes) {
+    $routes->get('/', 'Home::index');
+});
Code:
diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php
index fb44007e5c..0762386d5c 100644
--- a/app/Controllers/BaseController.php
+++ b/app/Controllers/BaseController.php
@@ -37,6 +37,8 @@ abstract class BaseController extends Controller
      */
    protected $helpers = [];

+    protected array $viewData;
+
    /**
      * Be sure to declare properties for any property fetch you initialized.
      * The creation of dynamic property is deprecated in PHP 8.2.
@@ -52,6 +54,7 @@ abstract class BaseController extends Controller
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.
+        $this->viewData['currentLocale'] = $this->request->getLocale();

        // E.g.: $this->session = \Config\Services::session();
    }
Code:
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 5934333309..81c3d174be 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,8 @@ class Home extends BaseController
{
    public function index(): string
    {
+        dd($this->viewData['currentLocale']);
+
        return view('welcome_message');
    }
}

Navigate to http://localhost:8080/fr/cp , I see "fr".
Reply


Messages In This Thread
RE: Why doesn't $request->getLocale() return the correct locale any more (since v.4.3)? - by kenjis - 08-27-2023, 04:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB