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.


4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Its not working for me.

    Error came like this :-

    Fatal error: Class 'Front_Controller' not found in C:\wamp\www\website\application\controllers\home.php on line 3

    ReplyDelete