[eluser]imn.codeartist[/eluser]
Create a library named MY_Controller.php and place in your system/application/libraries
and paste the code below
Code:
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:
Code:
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'));
}
}
And create home.php and place in the views:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Switch Language Developed By Dixanta Shrestha</title>
</head>
<body>
<form name="form_lang" action="<?=site_url('home/language')?>">
<select name="currlang" id="currlang">
<option value="en" <? echo ($this->config->item('language')=='english')?'selected':'';?>>English</option>
<option value="fr" <? echo ($this->config->item('language')=='french')?'selected':'';?>>French</option>
<option value="nl" <? echo ($this->config->item('language')=='dutch')?'selected':'';?>>Dutch</option>
</select>
</form>
<input type="submit" name="submit" value="Go"/>
<ul>
<li><a href="<?=site_url('home/set_language/frech')?>">French</a></li>
<li><a href="<?=site_url('home/set_language/english')?>">English</a></li>
<li><a href="<?=site_url('home/set_language/dutch')?>">Dutch</a></li>
</ul>
</body>
</html>
You are done.