Detect browser language and redirect

5

I'm using Wordpress and wanted to redirect my page to the "/ br /", "/ es /" versions when the browser language is one of those and when it does not stay on the default page, which is the ".com ".

I tried to use this code, but it did not work:

function idiomaUsuario(){
    $idioma = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
    return $idioma; 
}


    function redirecionaPaginaIdioma($idioma){
        switch($idioma){
            case 'pt': //Caso seja português
                header('Location: http://www.seusite.com.br/pt/');
                break;
            case 'es': //Caso seja espanhol
                header('Location: http://www.seusite.com.br/es/');
                break;
            default:
                header('Location: http://www.seusite.com.br/en/');
                break;
        }
    }

I do not know if anything changes to do this with PHP or JS, but any solution works.

Note: I placed this code in the beginning of the Index.php of the Wordpress theme.

    
asked by anonymous 11.11.2015 / 21:41

3 answers

6

This solution put in the question does not work very well for how HTTP_ACCEPT_LANGUAGE works. It returns a string in this format:

en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4

that indicates the order of preference of the user by the parameter q= (technically called quality factor ), and as you can see, you can have more than one language (general or regionalized) in the list .

The following line is interpreted in the example line: "I accept US English, and if not," general "English (weight 0.8). last, "general" Portuguese (weight 0.4).

The problem is that nothing guarantees that this list will come in the order of importance. I've never seen out of order, but the specification clearly states that the order is given by the quality factor and not by the textual position.

In addition, the first one on the list may be a language you do not have available, but the second or third language may be. In this case, as the question code takes only the first one from the list, it does not really have how to solve your problem.

That said, let's get to the solution.


Function to parse string

Based on what I mentioned at the beginning of the answer, I've developed a basic function that can serve as a starting point for what you need:

$acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$langs = array();
foreach( explode(',', $acceptLanguage) as $lang) {
    $lang = explode(';q=', $lang);
    $langs[$lang[0]] = count($lang)>1?floatval($lang[1]):1;
}
arsort($langs);

Basically what it does is break the original string with the commas, using explode , and saving in array in the format 'language => weight'. Once this is done, arsort sorts our list starting with the largest weight and going to the lowest.

Once parse is done, we need to see what we have available to serve the user. For this part, here is a solution:

$ourLanguages = array('pt-BR'=>'pt','pt'=>'pt','es'=>'es');

$choice = 'pt'; //Caso nenhuma outra sirva
foreach($langs as $lang=>$q) {
   if(in_array($lang,array_flip($ourLanguages))) {
      $choice=$ourLanguages[$lang];
      break;
   }
}

header("Location: http://www.example.com/$choice/");

The principle of operation is simple, we will test one of one of the customer's languages until we find one that works. To fully resolve the problem, we use the linguagem do cliente => nosso mapeamento format, so you can say that pt-BR should be understood simply as pt , which is the part you use in the URL.

You can see a working example in IDEONE .


Just to be noted, it has a PECL extension that deals with this, but I'd say it's more ingrained than our solution: link

    
12.11.2015 / 00:09
1

There is a very good translation plugin for Wordpress called Transposh .

With it you can:

  • Dynamically translate any page of the site using Google, Bing, Yandex between other
  • use an existing page as a translation of another
  • Check to not translate a particular section or even the entire page, avoiding machine translation
  • Edit the translation directly in the final HTML of the site.
  • Display language flags allowing the user to view the site in any language
  • Generate links in the domain / xx / page style where xx is the acronym for the language as it appears in HTTP_ACCEPT_LANGUAGE

More details on link

I also recommend making a donation if you like the plugin

    
12.11.2015 / 21:13
1

You can get this setting by using the Locale class of the library intl , which is available from php5.3.

<?php

echo 'Disponiveis: '. $_SERVER['HTTP_ACCEPT_LANGUAGE'] .'<br>';
$lang = substr(Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);

if($lang === 'pt'){
    echo 'Olá amigo';
}else if($lang == 'es'){
    echo 'Hola amigo';  
}else if($lang == 'en'){
        echo 'Hello friend';
}else if($lang == 'de'){
    echo 'hallo Freund';
}

You can use firefox as a test to change your language preference. Go to the Tools > Options > tab content. In languages change the language let at the top for example Spanish and see how the code works.

Example - PHPFiddle

    
18.11.2015 / 11:38