How do I get the browser user’s language?

Asked

Viewed 1,905 times

5

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

But on some distinct machines when accessing the site by the browser is directing to page on English. This happens because of the code below which I developed to recover the browser user 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, obviously, that’s not the best way. Some users for not knowing how to install a browser, such as Chrome, end up installing it in English. Hence the code redirects the user to the site in English.

And it’s interfering with the site’s analytics. I see that Brazilian users are first accessing the site in English and a percentage of these click to enter in Portuguese. The problem is that others to see that it is in English reject the site, without at least looking for the option of changing language.

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

Type per IP ?

Geolocation ?

Geoip ?

  • 1

    Yes, they are usually paid Apis... You are willing to invest?

  • Bring it on. Anything I finance.

  • 2

    I’ve already used the project Laravel-geoip, perfectly attended to me.

  • This looks cool @Luciorubens, posted as answer.

  • I’m trying to install this one from Rubens, however, for version 4 of Laravel. It’s giving a package error: The "https://packagist.org/packages.json" file could not be downloaded: failed to open stream: Operation timed out
https://packagist.org could not be fully loaded, package information was loaded from the local cache and may be out of date. So when I go to publish it sucks.

  • Yeah, I tried a lot of ways here and it didn’t work out.

Show 1 more comment

2 answers

7


Translation of Response to How to get the page Visitors Country with PHP?


You can use an external API such as geoplugin.net

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

Departure from the country:

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 catch the 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 a request to an external API, latency time can be relatively large.

It still has other solutions such as PHP Geoip extension.

  • It’s risky to use this API in case this site goes off the air one day ?

  • 1

    @Diegosouza is always risky, but that response from the English site is from 2012 and I tested it before publishing the answer and it was working properly.

  • Got it. Well, I’ll use it. If I see that it’s taking too long the request I’ll give you another one. I tried to download some library for Laravel, 4 but none worked. I liked the Laravel-geoip, but did not work out to install.

  • Website hosting is with the option allow_url_fopen disabled.

  • Your lodging @Diegosouza? Try with cURL... Or contact the support and ask to enable.

  • 1

    I’ve done it. It worked!

Show 1 more comment

5

Your question does not make much sense to me the way you put it. I understand the problem you have at hand but you have already thought that today the Internet is mobility?

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

I don’t think!

For me the right way to get the language is always by 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 follow the norm a lot LOCALE which allows us, in a standardised way, to have an easy and proven solution.

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

MVC and dynamic language change

That so far I have not had any answer but gives an idea of the problem we face in any project. Be with LARAVEL or other framework.

  • The target audience is not tourists or immigrants. Thank you.

  • @Diegosouza even so. if browser is the customer par excellence then this delivers the viewing language. Try to give the user a way to modify the language and if they do update your statistics.

  • It cannot be by the browser. The idea is as follows, if not in Brazil access the site in English - international language. Then Geolocation would help me in this.

  • I tested your code here. My Chrome is on EN, but returned in EN. You see the Zica ?

  • I tested in Safari and Firefox, returned EN.

  • I use any of the browsers and it works on all of them. You have to take into account in Chrome which one you prefer. As for your choice to go to Geolocation... I think I can add nothing more.

  • In my opinion the most correct is to follow the informed by the browser; if the person installed the browser in another language will not only be your site that will look like this, until the browser menus are in the other language not?

Show 2 more comments

Browser other questions tagged

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