Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

getBestLanguage for Googlebot

Hi guys!

getBestLanguage detected user-agent language if not set a header ACCEPT_LANGUAGE ? Example: googlebot, etc.

what this method return if user-agent not found and header accept_language not found? false? null? how it detect?

Thanks and nice day!



61.1k
edited Jan '16

as far as I understand ,you can return whatever you want, the thing is what does your application expect from this function ?

me personally after i check the language if it is empty I return a "default one" ( lets say English , en_US) because my app needs a language, and if there is something wrong, i simply dont care i display the default language

Izo, i understand you. But i interest: getBestLanguage use header (accept_language) and useragent, or some one ? or i need add one detect by useragent if GBL retured header langs only?

$lang = substr($request->getBestLanguage(), 0, 2);
// need it? :
if(empty($lang)) {
   $lang = $defLang;
}

and how detect language searchable bots ? google, yandex etc

Thanks!

im tested via requestmaker[dot]com without accept lang and without useragent. gBL returned false. It means necessary so:

...
if(!$lang) {
    $lang = $defLang;
}
...


61.1k
Accepted
answer
edited Jan '16

well here is source code of phalcon https://github.com/phalcon/cphalcon/blob/7cd81ef3c04f660f2d901aa285c051ec8f44c0a1/phalcon/http/request.zep#L917

it uses HTTP_ACCEPT_LANGUAGE

and if we check php.net http://www.php.net/manual/en/reserved.variables.server.php it says Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.

so my guess would be that it should be null.

regard the checking i am not an empty() user so i dont know you have to check it yourself.

but what i usually do is like :

$langs = [
    'en_US' => "US",
    'de_DE' => "DE" // or whatever
];

$lang = $request->getBestLanguage();

if (false == isset($langs[$lang])){
    return "EN"; // the default one 
}

return $langs[$lang];

so in this case i wont care what $_SERVER headers sends.

edited Jan '16

my release as service. maybe someone will need.

$di->setShared('lang', function() use ($di) {

        $langDefault = 'en';

        $request = $di->getRequest();
        $cookies = $di->getCookies();

        // Если есть кука с языком
        if($cookies->has('lang')) {
            $lang = $cookies->get('lang');
        }
        // Если нет
        else {
            // Ставим язык из браузера 
            $lang = substr($request->getBestLanguage(), 0, 2);

            // Если из браузера или заголовков определить не удалось
            if(!$lang) {
                // Ставим по умолчанию
                $lang = $langDefault;
            } 
            else {
                // Устанавливаем куку на 1 год
                $cookies->set('lang', $lang, time() + 365 * 86400);
            }
        }

        return $lang ?: $langDefault;
    });

    $di->setShared('translate', function() use ($di) {

        $lang = $di->getLang();

        // Доступные переводы
        $acceptLanguages = array(
            'en','ru',
        );

        // Языки-замены
        $friendlyLanguages = array(
            'ru' => ['ru','be','uk','kk'],
            'en' => ['en'],
        );

        // Если перевод на этот язык не готов - ищем ближайший понятный
        if(!in_array($lang, $acceptLanguages)) {
            foreach($friendlyLanguages as $from => $to) {
                if(in_array($lang, $to)) {
                    $lang = $from;
                    break;
                }
            }
        }

        // Подключаем перевод
        if (file_exists('../app/langs/'.$lang.'.php')) {
            require '../app/langs/'.$lang.'.php';
        }

        return new \Phalcon\Translate\Adapter\NativeArray(array(
            'content' => $messages
        ));

    });