How to determine client country locale in laravel 4

I want to determine my country or client locale in which they open the website, or get the browser recommended language.

For example, if you open a browser in Japan, he will give me the country code or the name of the current user, opened as "en-jp" or "japan".

After searching, I found out that the "Zend Framework" has a function to detect the user / environment in Zend_locale .

So, I wonder if I can do the same in laravel 4, or if not what solution do you offer in any method (php, javascript, ip check, etc.)?

Thank you for the advanced.

+4
source share
4 answers

Well, I know the answers to my questions as follows:

  • How to determine the country of the client?

    As far as I know, we need to use the geoIP service to find the IP address of the client, which can determine where the client is used (for example, maxmind )

    But this is not a solution to detect and change the language of my site if you are looking for this solution in laravel 4. I will show you the following question.

  • How to check the language the client wants to use? (locale in laravel4)

    In a nutshell, I found several ways in which you can get the language that the client wants to use by following:

    • HTTP (HTTP_ACCEPT_LANGUAGE) $_SERVER['HTTP_ACCEPT_LANGUAGE'] Request::server('HTTP_ACCEPT_LANGUAGE') laravel4. , .

    • . , . , ,

      <select> <option val="en">English</option> <option val="th">Thailand</option> </select>

      url Ex: www.Test.com/ru/ru/p >

    • cookie (). cookie , , . , .

, app/config/app.php :

'languages' => array('en','th','jp'),

app/filter.php, , :

    App::before(function($request){

    // 1. get the request langugage
    $url_lang = Request::segment(1);

    // 2. get Cookie langugage
    $cookie_lang = Cookie::get('language');

    // 3. Get the Browser Request language
    $browser_lang = substr(Request::server('HTTP_ACCEPT_LANGUAGE'), 0, 2);

    // 4. Start Checking the request language
    // Check that Language tha request is support or not?
    if(!empty($url_lang) AND in_array($url_lang, Config::get('app.languages')))
    {
        // Check whether the request url lang not same as remember in cookies
        if($url_lang != $cookie_lang)
        {
            // Cookie::forever('language',$url_lang);
            Session::put('language', $url_lang);
        }
        // Set the App Locale
        App::setLocale($url_lang);
    }
    // Check that has Language in Forever Cookie and is it support or not?
    else if(!empty($cookie_lang) AND in_array($cookie_lang, Config::get('app.languages')))
    {
        // Set App Locale
        App::setLocale($cookie_lang);
    }
    // Check the browser request langugae is support in app?
    else if(!empty($browser_lang) AND in_array($browser_lang, Config::get('app.languages')))
    {
        // Check whether the request url lang not same as remember in cookies
        if($browser_lang != $cookie_lang)
        {
            // Cookie::forever('language',$browser_lang);
            Session::put('language', $browser_lang);
        }

        // Set Browser Lang
        App::setLocale($browser_lang);
    }
    else
    {
        // Default Application Setting Language
        App::setLocale(Config::get('app.locale'));

    }});

:

App::after(function($request, $response){
$lang = Session::get('language');
if(!empty($lang))
{
    // Send The language Cookies
    $response->withCookie(Cookie::forever('language',$lang));
}
});

, .

+13

Ip2Country Laravel 4.2, - IP-. , MaxMind GeoIP, API .

https://github.com/smalldogs/ip2country

+3

ohm89! , linguam :

1º - I created two new arrays in my app.php, alt_langs (supported by my site) and locale_prefix (language prefix for the URL):

'locale' => 'pt',                          // Default is en.
'alt_langs' => array ('pt', 'en', 'es'),   // Supported by my site 
'locale_prefix' => '',                     // Dinamic array.

2º - In the routes.php file:

// Get the first segment url, ex.: mysite.com/pt and put this in locale_prefix array item:
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {
    App::setLocale(Request::segment(1));
    Config::set('app.locale_prefix', Request::segment(1));
}

// Here i usin the prexix to show right language site:
Route::group(array('prefix'=>Config::get('app.locale_prefix')), function()
{
    Route::get('', array('uses' => 'HomeController@index'));    
});

// And here i usin the http_accept_language to redirect him to default browser language:
Route::get('/', function(){
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    return Redirect::to($lang);
});

Hope I helped .o /

0
source

Source: https://habr.com/ru/post/1525989/


All Articles