Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

localization - Codeigniter language

I wanna make a multilanguage website, but I do not want that the language appear in URI like example.com/fr/about (I do not want this). I just want to change the text language. My problem is that the first load language that I do is for ever. why? If I do:

$this->config->set_item(‘language’,‘english’);
$this->lang->load(‘messages’);
$this->config->set_item(‘language’,‘french’);
$this->lang->load(‘messages’);

or

$this->lang->load(‘messages’,‘english’);
$this->lang->load(‘messages’,‘french’);

just the english appear. How can I fix this?

My config language autoload is empty.

Thank you for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I use a hook for this.

function pick_language() {

    require_once(APPPATH.'/config/language.php');

    session_start();

    // Lang set in URL via ?lang=something
    if(!empty($_GET['lang']))
    {
        // Turn en-gb into en
        $lang = substr($_GET['lang'], 0, 2);
        $_SESSION['lang_code'] = $lang;
    }

    // Lang has already been set and is stored in a session
    elseif( !empty($_SESSION['lang_code']) )
    {
        $lang = $_SESSION['lang_code'];
    }

    // Lang has is picked by a user.
    // Set it to a session variable so we are only checking one place most of the time
    elseif( !empty($_COOKIE['lang_code']) )
    {
        $lang = $_SESSION['lang_code'] = $_COOKIE['lang_code'];
    }

    // Still no Lang. Lets try some browser detection then
    else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
    {
        // explode languages into array
        $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

        log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));

        // Check them all, until we find a match
        foreach ($accept_langs as $lang)
        {
            // Turn en-gb into en
            $lang = substr($lang, 0, 2);

            // Check its in the array. If so, break the loop, we have one!
            if(in_array($lang, array_keys($config['supported_languages'])))
            {
                break;
            }
        }
    }

    // If no language has been worked out - or it is not supported - use the default
    if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
    {
        $lang = $config['default_language'];
    }

    // Whatever we decided the lang was, save it for next time to avoid working it out again
    $_SESSION['lang_code'] = $lang;

    // Load CI config class
    $CI_config =& load_class('Config');

    // Set the language config. Selects the folder name from its key of 'en'
    $CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);

    // Sets a constant to use throughout ALL of CI.
    define('CURRENT_LANGUAGE', $lang);
}

Not only will that set the correct language for you but it will give you a constant CURRENT_LANGUAGE which contains the language they are using ('en', 'de', etc).

The available languages for this come from a config item:

/*
|--------------------------------------------------------------------------
| Supported Languages
|--------------------------------------------------------------------------
|
| Contains all languages your site will store data in. Other languages can
| still be displayed via language files, thats totally different.
| 
| Check for HTML equivalents for characters such as ? with the URL below:
|    http://htmlhelp.com/reference/html40/entities/latin1.html
|
*/
$config['supported_languages'] = array(
    'en'=> array('name' => 'English', 'folder' => 'english'),
    'es'=> array('name' => 'Español', 'folder' => 'spanish'),
    'fr'=> array('name' => 'Fran?ais', 'folder' => 'french'),
    'de'=> array('name' => 'German', 'folder' => 'german')
);

/*
|--------------------------------------------------------------------------
| Default Language
|--------------------------------------------------------------------------
|
| If no language is specified, which one to use? Must be in the array above
|
|   en
|
*/
$config['default_language'] = 'en';

This will pick up the correct language from GET (http://somesite.com/?lang=de), then check session variable (populated by a correct match) then checks the browser for accept-lang header.

Whichever has a match first will be used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...