Welcome Guest, Not a member yet? Register   Sign In
Easy way to switch language
#1

[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">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Switch Language Developed By Dixanta Shrestha&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="form_lang" action="&lt;?=site_url('home/language')?&gt;"&gt;
<select name="currlang" id="currlang">
                                    <option value="en" &lt;? echo ($this->config->item('language')=='english')?'selected':'';?&gt;>English</option>
                                    <option value="fr" &lt;? echo ($this->config->item('language')=='french')?'selected':'';?&gt;>French</option>
                                    <option value="nl" &lt;? echo ($this->config->item('language')=='dutch')?'selected':'';?&gt;>Dutch</option>
                                </select>
&lt;/form&gt;
&lt;input type="submit" name="submit" value="Go"/&gt;
<ul>
                                <li><a href="&lt;?=site_url('home/set_language/frech')?&gt;">French</a></li>
                                <li><a href="&lt;?=site_url('home/set_language/english')?&gt;">English</a></li>
                                <li><a href="&lt;?=site_url('home/set_language/dutch')?&gt;">Dutch</a></li>
                          </ul>
&lt;/body&gt;
&lt;/html&gt;

You are done.


Messages In This Thread
Easy way to switch language - by El Forum - 10-23-2009, 01:03 AM
Easy way to switch language - by El Forum - 10-23-2009, 04:35 AM
Easy way to switch language - by El Forum - 10-23-2009, 10:50 AM
Easy way to switch language - by El Forum - 10-23-2009, 02:49 PM
Easy way to switch language - by El Forum - 11-18-2009, 06:32 AM
Easy way to switch language - by El Forum - 11-18-2009, 06:46 AM
Easy way to switch language - by El Forum - 03-29-2010, 10:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB