Welcome Guest, Not a member yet? Register   Sign In
Auto select language based on browser language
#1

I have a site made in 4 languages. I need to select automatically based on browser language (better than country, I imagine). Any idea? Thanks
Reply
#2

Add this to one of your helpers.

PHP Code:
<?php
/**
 * Get browser language, given an array of avalaible languages.
 * 
 * @param  [array]   $available  Avalaible languages for the site
 * @param  [string]  $default    Default language for the site
 * @return [string]              Language code/prefix
 */

if ( ! function_exists('getBrowserLanguage'))
{
    function getBrowserLanguage($available = [], $default 'en')
    {
        if ( ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
            $langs explode(','$_SERVER['HTTP_ACCEPT_LANGUAGE']);

            if (empty($available))
            {
                return substr($lang02);
            }

            foreach ($langs as $lang)
            {
                $lang substr($lang02);

                if (in_array($lang$available))
                {
                    return $lang;
                }
            }
        }
    
        return $default;
    }


Detects the web browsers languages.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

well, you can use agent --> accept_lang to check what language is accepted
https://codeigniter.com/user_guide/libra...agent.html

Tongue
Reply




Theme © iAndrew 2016 - Forum software by © MyBB