Welcome Guest, Not a member yet? Register   Sign In
Fetching session data in language class extension
#1

[eluser]the jan[/eluser]
I'm currently stuck on a problem. I need to access session data from within MY_Language.php but I can't access get_instance().

Here is my code:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Language extends CI_Language {
    
    function MY_Language()
    {
        parent::CI_Language();        
        
        global $CFG;
                $language = $this->languageChoice();
        
        $CFG->set_item('language', $language);
        
    }
    
    function languageChoice()
    {
        $CI =& get_instance();
        
        return $CI->session->userdata('language');
    }
}

?>

But I keep getting "Fatal error: Call to undefined function get_instance() in /../app/libraries/MY_Language.php on line 40"

How can I get this solved? I need to select the language file based on the session data of a user.

I also tried tried extending the controller but even though the config item is changed ( I see that when I echo the config variable in the actual controller) it still gets the data from the wrong language file:

Code:
function My_Controller()
    {
        parent::Controller();

        $this->obj =& get_instance();
                
        if($this->obj->session->userdata('language'))
        {
        
            $this->config->set_item('language', $this->obj->session->userdata('language'));


        }
        else
        {
            $this->config->set_item('language', 'german');
        }
}


Basicly I just need to choose the language file based on the data from a session variable.

Any help would be greatly appreciated.

Jan
#2

[eluser]Cristian Gilè[/eluser]
Hi Jan,

Code:
<?php
class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
        if($user_language = $this->session->userdata('language'))
        {
             $this->lang->load('filename_1', $user_language);
             $this->lang->load('filename_2', $user_language);
             ....
             $this->lang->load('filename_N', $user_language);
        }
    }
}

Cristian Gilè
#3

[eluser]the jan[/eluser]
Thanks Cristian. Sometimes a solution is that easy. Smile




Theme © iAndrew 2016 - Forum software by © MyBB