Skip to content

Bug:Locale::getDefault() is different from $this->request->getLocale() when $negotiateLocale is true #7668

Description

@kenjis

version: develop (4.3.x)

Locale::getDefault() is different from $this->request->getLocale() when we use content negotiation.

--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -83,7 +83,7 @@ class App extends BaseConfig
      *
      * If false, no automatic detection will be performed.
      */
-    public bool $negotiateLocale = false;
+    public bool $negotiateLocale = true;
 
     /**
      * --------------------------------------------------------------------------
@@ -98,7 +98,7 @@ class App extends BaseConfig
      *
      * @var string[]
      */
-    public array $supportedLocales = ['en'];
+    public array $supportedLocales = ['en', 'ja'];
 
     /**
      * --------------------------------------------------------------------------
<?php

namespace App\Controllers;

use Locale;

class Home extends BaseController
{
    public function index()
    {
        return 'Request:' . $this->request->getLocale()
            . ', Locale:' . Locale::getDefault();
    }
}
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -31,6 +31,8 @@ $routes->set404Override();
 // route since we don't have to scan directories.
 $routes->get('/', 'Home::index');
 
+$routes->get('{locale}', 'Home::index');
+
 /*
  * --------------------------------------------------------------------
  * Additional Routing

When we use only content negotiation:

$ curl -H "Accept-Language: ja,en-US;q=0.9,en;q=0.8" http://localhost:8080/
Request:ja, Locale:en

But If {locale} is used in a route, both locales are set to ja.

$ curl -H "Accept-Language: ja,en-US;q=0.9,en;q=0.8" http://localhost:8080/ja
Request:ja, Locale:ja

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions