Welcome Guest, Not a member yet? Register   Sign In
Language selection problem
#1

[eluser]Unknown[/eluser]
Hi,

i want make multi-lang with CI but...

if i change language ($this->config->set_item('language', 'french')Wink in MY_Controller, the language file in autoload.php is loaded with default language folder ($config['language'] in config.php) ! Need help

*and i dont find solution or good exemple to make multi-language in CI manuel


Thx
happy christmas and new year Wink
#2

[eluser]Joseph Wensley[/eluser]
Instead of autoloading you could use $this->lang->load('filename', 'lang');
#3

[eluser]pickupman[/eluser]
[quote author="Joseph Wensley" date="1292624535"]Instead of autoloading you could use $this->lang->load('filename', 'lang');[/quote]

To be a little more complete:
Code:
class MY_Controller extends Controller{

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

      $language = $this->session->userdata('language'); //set this session var using a dropdown/link
      $language = (!empty($language)) ? $language : 'english'; //set fallback to english to next line doesn't break
      $this->load->lang('filename', $language);
      
   }

}
#4

[eluser]jmadsen[/eluser]
You don't need:

<code>$language = (!empty($language)) ? $language : 'english'; //set fallback to english to next line doesn't break</code>


"If the second parameter is missing, the default language set in your application/config/config.php file will be used."

This also include the variable being empty or and invalid language code. Just set English in your config as the default.
#5

[eluser]tomcode[/eluser]
I use
Code:
$this->config->set_item('language', $my_language);

to set the app's language before loading any language files.

I usually set it in MY_Controller, a hook would do the job, too.


A solution for international sites used to be : I18n on the Wiki
Personally I use a solution very close to i18n, I do not override the helper functions and have some other tweaks


Watch out when loading files using
Code:
$this->load->lang('filename', $language);
A file is only loaded once, trying to load it (edit) later one more time (/edit) in another language will fail.


For complex projects with loads of entries :

- I organise my language idents like URI's, using a point as segment separator

- I use conventions like adding _fmt to an ident when it used with sprintf()

- I override CI's lang->line() method to have it return the identifier when the entry is not found, very nice for debugging and allows to worry for translations later :
Code:
/**
* Fetch a single line of text from the language array
*
* return the passed ident if no entry is found
*
* @access    public
* @param    string    $ident     the language line ident
* @return    string
*/
function line($ident = '')
{
    $line = ($ident === '' OR ! isset($this->language[$ident])) ? $ident : $this->language[$ident];

    return $line;
}
#6

[eluser]Unknown[/eluser]
Hello,

Ok !

Code:
/**
* My controller
*/
class my_controller extends Controller {
    public $_online                        = false;
    public $_debug                        = false;
    public $_maintenance            = false;
    public $language                    = "";
    public $page                            = false;
    public $method                        = false;
    public $url                                = false;
    public $uriParam                     = array();
    public $queryString             = array();
        
    /**
     * Constructor
     */
    function my_controller() {
        parent::Controller();
        
        //Config
        $this->_online                = $this->config->item('online');
        $this->_debug        = $this->config->item('debug');
        $this->_maintenance        = $this->config->item('maintenance');
        
        //Language
         $this->language = ($this->session->userdata('language')) ? $this->session->userdata('language') : ((get_cookie('mi_language')) ? get_cookie('mi_language') : $this->config->item('language'));
        
         //Set language session
         if ( ! $this->session->userdata('language'))
             $this->session->set_userdata($this->language);
    
    //Autoload language
    $this->lang->load('mi_metas', $this->language);
    $this->lang->load('mi_template', $this->language);
        
        //Debug
        if ($this->_debug) {
            $this->load->helper('printrn');
        
        ...


Thx all
bye




Theme © iAndrew 2016 - Forum software by © MyBB