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

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());
    dump($detected);
    $detected = explode('-', $detected[0]);
    dump($detected);

    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:

Array
(
    [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

Array
(
    [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.