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

(This post was last modified: 08-27-2023, 08:19 AM by gosocial2.)

I noticed that the last time $request->getLocale() returned the correct locale based on the URI was with CI_VERSION 4.2.x

How I use it:
PHP Code:
class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    protected $helpers = [];

    /**
    * Constructor.
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);
        $this->viewData['currentLocale'] = $this->request->getLocale();
    }
    // ...


app/Config/App.php :
PHP Code:
class App extends BaseConfig
{
    // ...
    public $defaultLocale 'en';
    public $negotiateLocale false// true does not seem to have any effect
    public $supportedLocales = ['en''fr', ];
    // ....


The routes:

PHP Code:
$routes->get('/''Home::index');

// Additional in-file definitions

$routes->group('{locale}/cp', [], function($routes) {
  $routes->get('/''Home::index');
 
  $routes->group('countries', ['namespace' => 'App\Controllers\Cp'], function ($routes) {
    $routes->get('''CountriesController::index', ['as' => 'countryList']);
    $routes->get('index''CountriesController::index', ['as' => 'countryIndex']);
    $routes->get('add''CountriesController::add', ['as' => 'newCountry']);
    $routes->post('add''CountriesController::add', ['as' => 'createCountry']);
    $routes->get('edit/(:any)''CountriesController::edit/$1', ['as' => 'editCountry']);
 
    $routes->post('edit/(:any)''CountriesController::edit/$1', ['as' => 'updateCountry']);
    $routes->get('delete/(:any)''CountriesController::delete/$1', ['as' => 'deleteCountry']);
 
    $routes->post('menuitems''CountriesController::menuItems', ['as' => 'menuItemsOfCountries']);
  });

}); 

Expected behavior:

When in a URL like http://localhost:8080/fr/cp/countries, $currentLocale in the view equals "fr".

In CodeIgniter 4.2.5:
$currentLocale gets set as "fr"

In CodeIgniter 4.3.x:
$currentLocale gets set as "en" probably what's set as the default locale in app/Config/App.php (i.e. on a page in French, locale is returned as English)

Why? And how to get "fr" from http://localhost:8080/fr/... now in CI 4.3.x and 4.4.x?

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply
#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
#3

I checked v4.3.8, and I cannot reproduce it.
I also see "fr".
Reply
#4

I'm using IncludeBeers Create a Multilingual Website and it is working fine here with CodeIgniter 4.4.0.

Creating a multilingual website with CodeIgniter 4 (part 1)

Creating a multilingual website with CodeIgniter 4 (part 2)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 08-30-2023, 11:42 AM by gosocial2.)

Sorry, but the problem persists. I not only tried all the steps in the documentation, but also the tutorial above. Working app based on CI 4.2.x is at https://ccpdemo2.ozar.net - it can be tested by selecting a language from the dropdown in the right hand side of the top navbar. When all the code in this app is copied over to CI 4.3.x, it breaks and language selection menu always takes the user to an English page. 

May I request, those who assert no such problem exists, describe all the steps to reproduce a working solution testing also with the Safari browser?

Can't do withouts:

The URLs to be tested need to be created using routes that will reside in the below route groups.

PHP Code:
$routes->group('{locale}/cp', [], function($routes) {
....
}
  
or

PHP Code:
$routes->group('{locale}/admin', [], function($routes) {
....


CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply




Theme © iAndrew 2016 - Forum software by © MyBB