2015-08-08 112 views
3

我是Codeigniter,我需要用戶的動態語言。CodeIgniter動態語言功能

我在標題處添加了下拉菜單,我想讓用戶在前端更改網站語言。

我想在一個控制器下面的代碼更改語言

$this->config->set_item('language','spanish'); 

,但它不工作的不改變語言

我也試圖與我的控制器的一個以會話使用下面的代碼

$mylanguage = $this->session->set_userdata(array('my_language',$dynamiclang)); 

並嘗試訪問此配置文件中的變量,但它也不起作用。

幫我做這個工作。

回答

3

最後我得到的成功,使多國語言下面

按照步驟

MY_Lang.php文件夾application\core

MY_Lang.php 
<?php 
(defined('BASEPATH')) OR exit('No direct script access allowed'); 

class MY_Lang extends CI_Lang 
{ 
    function __construct() { 

     global $URI, $CFG, $IN; 

     $config =& $CFG->config; 

     $index_page = $config['index_page']; 
     $lang_ignore = $config['lang_ignore']; 
     $default_abbr = $config['language_abbr']; 
     $lang_uri_abbr = $config['lang_uri_abbr']; 
     #exit('my_lang'); 
     #print_r($URI); 
     /*if($index_page=='es') 
     { 
      #$config['index_page'] = 'es'; 
      #$config['lang_uri_abbr'] = 'es'; 
      #$IN->set_cookie('user_lang', 'es', $config['sess_expiration']); 
      #$URI->uri_string = str_replace('es','en',$URI->uri_string); 
      } 
     else{ 
      #$config['index_page'] = 'en'; 
      #$config['lang_uri_abbr'] = 'en'; 
      #$IN->set_cookie('user_lang', 'en', $config['sess_expiration']); 
      } 
     /* get the language abbreviation from uri */ 
     $uri_abbr = $URI->segment(1); 
     #$uri_abbr='es';  
     /* adjust the uri string leading slash */ 
     #print $URI->uri_string; 
     $URI->uri_string = preg_replace("|^\/?|", '/', $URI->uri_string); 



     if ($lang_ignore) { 

      if (isset($lang_uri_abbr[$uri_abbr])) { 

       /* set the language_abbreviation cookie */ 
       $IN->set_cookie('user_lang', $uri_abbr, $config['sess_expiration']); 

      } else { 

       /* get the language_abbreviation from cookie */ 
       $lang_abbr = $IN->cookie($config['cookie_prefix'].'user_lang'); 

      } 

      if (strlen($uri_abbr) == 2) { 

       /* reset the uri identifier */ 
       $index_page .= empty($index_page) ? '' : '/'; 
       // exit('654'); 
       /* remove the invalid abbreviation */ 
       $URI->uri_string = preg_replace("|^\/?$uri_abbr\/?|", '', $URI->uri_string); 

       /* redirect */ 
       header('Location: '.$config['base_url'].$index_page.$URI->uri_string); 
       exit; 
      } 

     } else { 

      /* set the language abbreviation */ 
      $lang_abbr = $uri_abbr; 
     } 

     /* check validity against config array */ 
     if (isset($lang_uri_abbr[$lang_abbr])) { 


      /* reset uri segments and uri string */ 
      //$URI->_reindex_segments(array_shift($URI->segments)); # this is commented becasue this is giving error : @$hok : 09/August/2015 
      $URI->uri_string = preg_replace("|^\/?$lang_abbr|", '', $URI->uri_string); 

      /* set config language values to match the user language */ 
      $config['language'] = $lang_uri_abbr[$lang_abbr]; 
      $config['language_abbr'] = $lang_abbr; 


      /* if abbreviation is not ignored */ 
      if (! $lang_ignore) { 

        /* check and set the uri identifier */ 
        $index_page .= empty($index_page) ? $lang_abbr : "/$lang_abbr"; 

       /* reset the index_page value */ 
       $config['index_page'] = $index_page; 
      } 

      /* set the language_abbreviation cookie */    
      $IN->set_cookie('user_lang', $lang_abbr, $config['sess_expiration']); 

     } else { 

      /* if abbreviation is not ignored */ 
      if (! $lang_ignore) {     

        /* check and set the uri identifier to the default value */  
       $index_page .= empty($index_page) ? $default_abbr : "/$default_abbr"; 

       if (strlen($lang_abbr) == 2) { 

        /* remove invalid abbreviation */ 
        $URI->uri_string = preg_replace("|^\/?$lang_abbr|", '', $URI->uri_string); 
       } 
       /*echo '<pre>'; 
       print_r($_SERVER); 
       print_r($config['base_url'].$index_page.$URI->uri_string); 
       exit;*/ 
       $q = $_SERVER['QUERY_STRING']; 
       if($q) 
        $q = "/?".$q; 
       /* redirect */ 
       header('Location: '.$config['base_url'].$index_page.$URI->uri_string.$q); 
       exit; 
      } 

      /* set the language_abbreviation cookie */     
      $IN->set_cookie('user_lang', $default_abbr, $config['sess_expiration']); 
     } 

     log_message('debug', "Language_Identifier Class Initialized"); 
    } 
} 

/* translate helper */ 
function t($line) { 
    global $LANG; 
//print_r($LANG); 
// exit; 
    return ($t = $LANG->line($line)) ? $t : $line; 
} 
function _t($line,$params=array()) { 
    global $LANG; 
    if($params){ 
     echo str_replace(array_keys($params),array_values($params),($t = $LANG->line($line)) ? $t : $line); 
    } 
    else 
     echo ($t = $LANG->line($line)) ? $t : $line; 
} ?> 

,並在config.php

下面的東西加入
$config['language'] = "english"; 

/* default language abbreviation */ 
$config['language_abbr'] = "en"; 

/* set available language abbreviations */ 
$config['lang_uri_abbr'] = array("en" => "english","es" => "spanish","ca" => "catalan"); 

/* hide the language segment (use cookie) */ 
$config['lang_ignore'] = TRUE; 

添加以下代碼route.php

$route['^en/(.+)$'] = "$1"; 
$route['^es/(.+)$'] = "$1"; 
$route['^ca/(.+)$'] = "$1"; 

$route['^(\w{2})$'] = $route['default_controller']; 
$route['^(\w{2})/(.+)$'] = "$2"; 

,並添加語言文件語言文件夾像下面

language/catalan 
language/spanish 
language/english 

我希望這會有所幫助。

0

你有沒有嘗試在你的控制器中添加這個?

$this->load->helper('language');

+0

嘿@Gemmi,這個幫手已經包含在autoload.php中,所以不需要添加。 –

0

負載在cookie中的當前語言和在cookie 實施例使用的語言值加載語言文件,每當用戶選擇了語言使用下面的函數來的當前語言設置到cookie

function language($lang = false) { 
    if($this->input->get('lang')){ $lang = $this->input->get('lang'); } 
    $folder = 'application/language/'; 
    $languagefiles = scandir($folder); 

    if(in_array($lang, $languagefiles)){ 
     $cookie = array(
      'name' => 'lang', 
      'value' => $lang, 
      'expire' => '31536000', 
      'prefix' => 'my_', 
      'secure' => false 
     ); 

     $this->input->set_cookie($cookie); 
    } 

    $this->config->set_item('language', $lang); 

    redirect($_SERVER["HTTP_REFERER"]); 
} 

然後在您的主要自定義控制器的構造函數或如果你只是延長是CI_Controller然後在每個控制器的構造函數加載語言文件(S)和像

$this->lang->load('language_filename', get_cookie('my_lang')); 

你完成了