We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

getbestlanguage return different value on each browser

Hi

I have in my app a translation service on DI, and by default it takes the language that the getBestLanguage function return. My translation files names are like this fr-FR.php.

The problem is that getBestLanguage retrun different value on each browser:

  • Chrome return fr-FR
  • Firefox return fr

Apart rename my translation files with two letters, how I can resolve this problem?

Two options:

1) Why would you even do this? Does user have option to switch to his prefered language after?

2) If you really want to do this, I would make a language "config" file/array. Somehting like:

// Sample config, where Key = locale, value = language file to load.
// Here you will define all your app languages with short and full locale.
$langConfig = [
    'fr-FR' => 'fr-FR',
    'fr' => 'fr-FR',
];

// Check for match
if (isset($langConfig[$this->request->getBestLanguage()])) {
    // Load the requested file
} else {
    // show default language here
}

Also you can check popular browser locales on internet. Example: https://www.metamodpro.com/browser-language-codes

edited Jan '18
$lang = explode('-', $this->request->getBestLanguage())[0]