How to re-create a URL with Accept-Language + split ?

Asked

Viewed 292 times

1

I have a code on PHP created by @Maia, it informs the browser language and returns with a redirecting.

Code

<?php
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
header("Location: http://meusite.com/$lang" ) ;
?>

It redirects to:

meusite.com/pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4

The right thing would be for him to redirect to meusite.com/pt-BR because my browser has the language pt-BR.

Did creating a split would solve?


Member @Maia did so:

$lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];

In case the URL would only go to the first comma, leaving so only: meusite.com/pt-BR. Only the code seems to be wrong, it doesn’t work.

I would like to do this with the URL, leaving up /pt-BR, or even leave it:

meusite.com/pt

The member suggested that I change the index from [0] to 1 leaving so:

$lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[1];

It just doesn’t work, so I’d like some example of how to get it redirected to /en or how to redirect to /pt

  • 1

    Is the classic Problemaxy. In my humble opinion, the original question it was much better, but it seems to me that you gave Accept in the first answer, that although good it might not have solved entirely your problem.

  • @Bacco How can I fix this? Help me again man! rs

  • It solves, now my doubt is just how to change the URL. #Simply

  • I can’t help you at the moment, because I’m working on other things, but let’s hope someone gives you more options on that issue, because there are several ways to deal with it. Even servers like Apache already have this feature internally, you may notice that the default installation (the welcome page of apache) meets the language of the sew browser automatically, even without PHP: https://httpd.apache.org/docs/2.2/content-negotiation.html

  • Whenever I ask for help from you, you say you are busy... Typical. rsrsrs More has no problem! ;)

  • 2

    Follow a solution without much optimization, just for you to stop mimimi :P

Show 1 more comment

1 answer

4


Follows a quick fix 1:

<?php

$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {

   // Cá entre nós, usar regex pra isso é forçar a amizade, mas lá vai :)
   preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);

   //Aqui vamos ordenar por preferência do usuário
   if (count($lang_parse[1])) {
      $langs = array_combine($lang_parse[1], $lang_parse[4]);
      foreach ($langs as $lang => $val) {
         if ($val === '') $langs[$lang] = 1;
      }
      arsort($langs, SORT_NUMERIC);
   }
}

// Aqui você põe apenas as linguagens que seu site REALMENTE tem:
foreach ($langs as $lang => $val) {
   if ( strpos($lang, 'pt') === 0) {
      header( "Location: http://meusite.com/pt/" ) ;
      exit();
   } else if (strpos($lang, 'en') === 0) {
      header( "Location: http://meusite.com/en/" ) ;
      exit();
   } 
}
// Se nao achar nenhuma:
header( "Location: http://meusite.com/pt/" ) ;
exit();

?>

No need to put pt-BR, nor en-US, unless you want to separate for example the pt-BR of pt-PT, and so on.

1. searched in Duck Duck Go, running.

Source:http://www.thefutureoftheweb.com/blog/use-accept-language-header

  • Thank the original author and http://ddg.gg

  • That’s very top! It’s much better than redirecting by IP.

  • 1

    This solution (as well as the apache I mentioned) is good for respecting the user’s will. Just because a Brazilian travels to the US does not mean that he will read sites in English, for example (what would happen if he were to change the language by IP or geolocation). Geolocation is good if the guy is looking for a restaurant nearby, or something like that. And IP is good for your statistics, and not for the end user.

  • Which do you think is better, by IP or by Language ? By Language is good for the fact of respecting the will of the user, only that and if the browser is with language en? like your Iron. Already by IP it will not respect the will of the user. I am in doubt. And you? By Language can be better? Says there! : D

  • @Alexandrelopes my Iron is in because I left in en, if you want to just change in configs too. Is that my windows is in English too, so got the standard language of the system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.