I want to detect my client country or locale which they open website from or get the browser recommended language.

For Example, if you open the browser in Japan it will give me country code or country name current user opened like "en-jp" or "japan".

After Searching I found out that "Zend Framework" has function to detect the user/environmental in Zend_locale.

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

Thank you in advanced.

有帮助吗?

解决方案

Ok I know the answers of my questions as following:

  1. How to detect the client country?

    As far as I know we need to use geoIP service to detect the client IP which can tell where the client using from (e.g. maxmind)

    But this is not solution to detect and alter my website language, if you looking for this solution in laravel 4 I will show you in next question

  2. How to check the language that client want to use? (locale in laravel4)

    In summarize I found some of ways that can get which language that client want to use by following:

    1. HTTP Header (HTTP_ACCEPT_LANGUAGE) in $_SERVER['HTTP_ACCEPT_LANGUAGE'] equal to Request::server('HTTP_ACCEPT_LANGUAGE') in laravel4. Which these header tell us the language that current client browser want to use.

    2. Direct request - In this condition we will get direct request from client which language they want to use. For easy example like we give them the

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

      And they select from it send to server via url Ex: www.Test.com/en

    3. Cookies (optional) - We can get the cookies from browser which we provide the language that last use by current user. Which is we must send the cookies after they visited the site for first times.

Before I use it I store the support languages array in app/config/app.php by following:

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

All of it I modify the code in app/filter.php to get all above data and processing with my app by following:

    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'));

    }});

And after event of app is following:

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

Hope this will help you out.

其他提示

I use this Ip2Country for Laravel 4.2 that can retrieve a users country based on a given IP address. Creates a local database that utilizes the MaxMind GeoIP data, so no run time external API calls.

https://github.com/smalldogs/ip2country

I haven't tried this package, but you can probably use this:
https://github.com/webpatser/laravel-countries

If that doesn't get you all you need, you can probably combine it with the built in App::setLocale('en');
Referenced in:
http://cheats.jesse-obrien.ca

Speaks ohm89! I used the following technique to capture the priority user linguam and show my site in your language:

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

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

2º - In 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);
});

I hope I have helped.o/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top