How to detect the client country, location in laravel 4


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:

    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:


    // 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
    // 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
    // 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
        // Default Application Setting Language


And after event of app is following:

App::after(function($request, $response){
$lang = Session::get('language');
    // Send The language Cookies

Hope this will help you out.