Codeigniter 语言 [英] Codeigniter language

查看:24
本文介绍了Codeigniter 语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个多语言网站,但我不希望该语言出现在 URI 中,例如 example.com/fr/about(我不想要这个).我只想更改文本语言.我的问题是我做的第一个加载语言是永远的.为什么?如果我这样做:

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

$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.

感谢您的帮助.

推荐答案

我为此使用了一个钩子.

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

这不仅会为您设置正确的语言,还会为您提供一个常量 CURRENT_LANGUAGE,其中包含他们使用的语言('en'、'de' 等).

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

这将从 GET (http://somesite.com/?lang=de),然后检查会话变量(由正确匹配填充),然后检查浏览器是否有 accept-lang 标头.

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.

首先匹配的将被使用.

这篇关于Codeigniter 语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆