How do I get the user language of the browser?

5

I have a website in 3 languages, EN , EN and EN .

But on some separate machines when accessing the site by the browser is directing to page in English . This happens because of the code below that I developed to retrieve the user's browser language.

App::before(function($request){
    $l = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    $l = ($l == 'pt') ? null : $l;
    if(!is_null($l)){
        $l = in_array($l, Config::get('app.languages')) ? $l : 'en';
    }
});

It turns out that, obviously, this is not the best way. Some users who do not know how to install a browser, like Chrome for example, end up installing it in English. Hence the code redirects the user to the site in English.

And this is interfering with site analytics. By the graph I see that Brazilian users are accessing the site in English first and a percentage of these click to enter in Portuguese. The problem is that others because they see that it is in English reject the site, without even looking for the option of the language exchange.

Anyway, is there any way in PHP, Laravel to fetch the user language in a more global way?

Type per IP?

Geolocation?

GeoIP?

    
asked by anonymous 17.08.2015 / 14:29

2 answers

6

Translation of Answer to How to get the page visitors Country with PHP?

You can use an external API like geoplugin.net

$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=76.109.14.196");
echo $xml->geoplugin_countryName ;

Country Exit:

United States

Full XML response:

<geoPlugin>
    <geoplugin_request>76.109.14.196</geoplugin_request>
    <geoplugin_status>200</geoplugin_status>
    <geoplugin_city>West Palm Beach</geoplugin_city>
    <geoplugin_region>FL</geoplugin_region>
    <geoplugin_areaCode>561</geoplugin_areaCode>
    <geoplugin_dmaCode>548</geoplugin_dmaCode>
    <geoplugin_countryCode>US</geoplugin_countryCode>
    <geoplugin_countryName>United States</geoplugin_countryName>
    <geoplugin_continentCode>NA</geoplugin_continentCode>
    <geoplugin_latitude>26.761600494385</geoplugin_latitude>
    <geoplugin_longitude>-80.091598510742</geoplugin_longitude>
    <geoplugin_regionCode>FL</geoplugin_regionCode>
    <geoplugin_regionName>Florida</geoplugin_regionName>
    <geoplugin_currencyCode>USD</geoplugin_currencyCode>
    <geoplugin_currencySymbol>&#36;</geoplugin_currencySymbol>
    <geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>

Simple function to get IP:

function getIP() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

Some considerations you have to take is that as it would be a request to an external API, the latency time may be relatively large.

You still have other solutions like PHP's GeoIP extension .

    
17.08.2015 / 14:42
5

For me your question does not make much sense of how you put it. I realize the problem you have at hand but have you ever thought that the internet is mobility?

What happens if your user is in another country ... how do you plan to respond to this by geolocation? by ip?

I do not think so!

For me the right way to get the language is always through the browser. example:

/**
 * locale <i>Tag</i> do browser
 * @return string
 */
 private function _parse_client_language() {

        $http_accept = getenv('HTTP_ACCEPT_LANGUAGE');

        if (isset($http_accept) && strlen($http_accept) > 1) {
            # Split 
            $x = explode(",", $http_accept);
            foreach ($x as $val) {
                if (preg_match("/(.*);q=([0-1]{0,1}.d{0,4})/i", $val, $matches))
                    $lang[$matches[1]] = (float) $matches[2];
                else
                    $lang[$val] = 1.0;
            }

            #default language (highest q-value)
            $qval = 0.0;
            foreach ($lang as $key => $value) {
                if ($value > $qval) {
                    $qval = (float) $value;
                    return $key;
                }
            }
        }
        return self::$tag_default;
    }

I am very much following the standard LOCALE that allows us, in a standardized way, to have an easy and proven solution.

Then I recommend reading my question here in the OS at:

MVC and change language dynamics

So far I have not gotten any response but it gives an idea of the problem that we face in any project. Be it with LARAVEL or another framework.

    
17.08.2015 / 16:12