Codeigniter切换语言具有多段

时间:2015-07-06 09:02:44

标签: codeigniter internationalization

我在mydomain.com/en/controller中使用i18n没问题 如果我使用像mydomain / foldername / en / controller这样的东西,我的问题就开始了 当我切换语言时,它就像mydomain / foldername / en / ar / controller一样 因为ar是另一种语言 我的base_url是mydomain / foldername / 我相信它来自一个索引,我试图在MY_LANG中改变它:     

// Originaly CodeIgniter i18n library by Jérome Jaglale
// http://maestric.com/en/doc/php/codeigniter_i18n
//modified by Tobin Thomas

class MY_Lang extends CI_Lang {


/**************************************************
configuration
***************************************************/

// Add your languages here
private $languages = array(
'en' => 'english',
'ar' => 'arabic'
);

// special URIs (not localized)
    private $special = array (
'admin', 
'assets',
'editor'
);

// where to redirect if no language in URI
private $uri;
private $default_uri;
private $lang_code;

/**************************************************/


function MY_Lang()
{
    parent::__construct();

    global $CFG;
    global $URI;
    global $RTR;

    $this->uri = $URI->uri_string();
    $this->default_uri = $RTR->default_controller;

    $uri_segment = $this->get_uri_lang($this->uri);
    $this->lang_code = $uri_segment['lang'] ;

    $url_ok = false;
    if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages)))
    {
        $language = $this->languages[$this->lang_code];
        $CFG->set_item('language', $language);
        $url_ok = true;
    }

    if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
    {
        // set default language
        $CFG->set_item('language', $this->languages[$this->default_lang()]);

        $uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
        $uri = ($uri[0] != '/') ? '/'.$uri : $uri;
        $new_url = $CFG->config['base_url'].$this->default_lang().$uri;

        header("Location: " . $new_url, TRUE, 302);
        exit;
    }
}



// get current language
// ex: return 'en' if language in CI config is 'english'

function lang()
{
    global $CFG;        
    $language = $CFG->item('language');

    $lang = array_search($language, $this->languages);
    if ($lang)
    {
        return $lang;
    }

    return NULL;    // this should not happen
}


function is_special($lang_code)
{
    if ((!empty($lang_code)) && (in_array($lang_code, $this->special)))
        return TRUE;
    else
    return FALSE;
}


function switch_uri($lang)
{
    if ((!empty($this->uri)) && (array_key_exists($lang, $this->languages)))
    {

        if ($uri_segment = $this->get_uri_lang($this->uri))
        {
            $uri_segment['parts'][0] = $lang;
            $uri = implode('/',$uri_segment['parts']);
        }
        else
        {
            $uri = $lang.'/'.$this->uri;
        }
    }

    return $uri;
}

//check if the language exists
//when true returns an array with lang abbreviation + rest
function get_uri_lang($uri = '')
{
    if (!empty($uri))
    {
        $uri = ($uri[0] == '/') ? substr($uri, 0): $uri;

        $uri_expl = explode('/', $uri, 2);
        $uri_segment['lang'] = NULL;
        $uri_segment['parts'] = $uri_expl;      

        if (array_key_exists($uri_expl[0], $this->languages))
        {
            $uri_segment['lang'] = $uri_expl[0];
        }
        return $uri_segment;
    }
    else
    return FALSE;
}


// default language: first element of $this->languages
function default_lang()
{
    $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
    $browser_lang = substr($browser_lang, 0,2);
    return (!empty($browser_lang) && array_key_exists($browser_lang, $this->languages)) ? $browser_lang: 'en';
}


// add language segment to $uri (if appropriate)
    function localized($uri)
{
    if (!empty($uri))
    {
        $uri_segment = $this->get_uri_lang($uri);
        if (!$uri_segment['lang'])
        {

            if ((!$this->is_special($uri_segment['parts'][0])) && (!preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri)))
            {
                $uri = $this->lang() . '/' . $uri;
            }
        }
    }
    return $uri;
}
} 

// END MY_Lang Class

/* End of file MY_Lang.php */
/* Location: ./application/core/MY_Lang.php */

任何想法?

1 个答案:

答案 0 :(得分:0)

我面临同样的行为。问题不在于MY_Lang。这是视图中$ this-> lang-> switch_uri('lang')的调用。我没有使用像anchor,site_url这样的codeigniter函数来生成正确的链接。我使用普通的HTML。小错误,分钟/小时的代码审查。

<a href="<?php echo $this->lang->switch_uri('en'); ?>"><img ...

不起作用。它将lang段添加两次到uri,而是使用anchor或site_url

<a href="<?php echo site_url($this->lang->switch_uri('en')); ?>"><img ...