Friday, October 23, 2009

Easy way to switch language

Create a library named MY_Controller.php and place in your system/application/libraries

and paste the code below


class Front_Controller extends Controller

{


var $current_culture='';


function Front_Controller()

{

parent::Controller();

if($this->session->userdata('language'))

{

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

$this->config->set_item('language_code',$this->session->userdata('language_code'));

}

$this->current_culture=$this->config->item('language');

$this->culture_code=$this->config->item('language_code');

}


function set_current_culture($language)

{

$this->session->set_userdata('language',strtolower($language));

}


}

}


Create your controller and extend it with Front_Controller class


For example:

class Home extends Front_Controller

{

function Home()

{

parent::Front_Controller();


}


function index()

{

$data['title']='Switch Language Example';

$this->load->view('home',$data);

}


// Function if you are doing post from FORM

function language()

{

$this->set_language($this->input->post('currlang'));

}


//Function if you are selecting from the Link


function set_language($language_code)

{

$this->set_current_culture($language_code);

redirect(site_url('home'));

}


}


create your view.


You are done.


No comments:

Post a Comment