Language detection with locale variations not working (en_US, en_GB)


I’m struggling around with the automatic language detection with locale variations. The website should have the following languages: de, nl, it, en_GB and en_US.
When activating language detection and setting the preferred browser-language to en_US, it will redirect me to /en and not to /us. All other languages are working like a charm.

This is how my config in a fresh langkit-installation looks like:

c::set('languages', [
'code'    => 'us',
'name'    => 'English US',
'locale'  => 'en_US',
'url'     => '/us',
  ], [
'code'    => 'en',
'name'    => 'English GB',
'locale'  => 'en_GB',
'url'     => '/en',
  ], [
'code'    => 'de',
'name'    => 'Deutsch',
'locale'  => 'de_DE',
'default' => true,
'url'     => '/',

c::set('language.detect', true);

Any idea how to solve it? Thank your for your help!

P.S.: Same problem in Kirby 3 :slight_smile:

The problem is this method (the dumps don’t belong there, I just put them in to see what it gives me:

  public static function acceptedLanguageCode() {
    if(!is_null(static::$acceptedLanguageCode)) return static::$acceptedLanguageCode;
    $detected = explode(',', static::acceptedLanguage());
    $detected = explode('-', $detected[0]);

    return static::$acceptedLanguageCode = strtolower($detected[0]);

So the first dump returns the array of the browser’s accepted languages, which might look something like this:

    [0] => en-US
    [1] => en;q=0.9
    [2] => de-DE;q=0.8
    [3] => de;q=0.7
    [4] => fr;q=0.6

Then in the next step, it explodes the string (in this case en-US) leaving us with

    [0] => en
    [1] => US

And then returns the first element, en.

And in the detect() method, the language with the code en is returned.

So much for an explanation why this is happening.

As you can also see from the array of the original accepted languages, not all entries do carry the country preference. We would probably need more sophisticated logic here to try and extract the second bit if it exists etc.