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.
#2

[eluser]eoinmcg[/eluser]
Hi dixcoder,

In the past I used a similar approach, though changing the language via session, rather than the url, means that the non default language versions are going to be invisible to search engines.

If you're interested take a look at
http://codeigniter.com/wiki/URI_Language_Identifier/
http://maestric.com/doc/php/codeigniter_i18n
#3

[eluser]imn.codeartist[/eluser]
thank you for your reply, well i also have provided the hyperlink to change the language where it takes different languages as parameter for example http://www.example.com/set_language/english so shouldn't be the problem for search engine ...mmm... what do you say?
#4

[eluser]eoinmcg[/eluser]
maybe i read your post too fast or misunderstood something...

is the change of language evident in the URL or simply by changing session state? what mean to say is that using sessions/ cookies to transfer such info is going to be invisible to search engines...
#5

[eluser]liri[/eluser]
Any ideas why am I getting an error of
"Fatal error: Call to a member function set_userdata() on a non-object in /var/www/..."
which is this part of code:
Code:
$this->session->set_userdata('language', strtolower($language));
in the front controller?
#6

[eluser]liri[/eluser]
Ok so,

Seems like I needed to load the session library in each controller (parent and child).
Is this how it should be?

Secondly, what is the code in the front controller's construct for about
item data? seems like it's never get read to there or written to there at all?
I'm talking about:
Code:
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');
#7

[eluser]ADujmovic[/eluser]
Hi,
I need some help with this code here. I'm trying to implement this code with my solution and it is not working. I did all the steps but when I clickc on the link i get 404 message! Any tips?




Theme © iAndrew 2016 - Forum software by © MyBB