[SOLVED] How do I switch languages. newby!

#1
[eluser]chefnelone[/eluser]
hi.
Just starting my first multilingual site with ci.

I already have the languages folder in: system/language/
But I have no idea how to manage the language switcher using something like:
<ul>
<li><a href="#">French</a></li>
<li><a href="#">Spanish</a></li>
<li><a href="#">Dutch</a></li>
</ul>

looking for a tutorial or thread where I can start with ?

#2
[eluser]JHackamack[/eluser]
What I've researched is doing something like setting a session variable that contains the language to use. You can set it with a function in a controller and redirect back to the current page.

Code:
$this->lang->load('alerts',$this->session->userdata('language'));

#3
[eluser]chefnelone[/eluser]
[quote author="JHackamack" date="1263931127"]What I've researched is doing something like setting a session variable that contains the language to use. You can set it with a function in a controller and redirect back to the current page.

Code:
$this->lang->load('alerts',$this->session->userdata('language'));
[/quote]
I can't get your point, I need beginner's guidelines to do this.

I just need to switch the language from english to spanish. Which is the variable I need to change?

#4
[eluser]JHackamack[/eluser]
There is no variable predefined in CodeIgniter.

To maintain a users language preferences across multiple pages you'd need to use a session variable, or a cookie. I prefer session variables so thats how I wrote my example.

Since you're asking about languages I'm assuming you've read the language user guide documentation and know how to do $this->lang->load(). I'll provide you an example of what setting the language variable might look like

Code:
/**
    * Short description for the Function
    *
    * @arguments  
    * @return values    
    */
    public function index()
    {
        $this->load->helper('language');
        $this->lang->load('view',$this->session->userdata('language'));
        $this->load->view('page');
    }

  /**
    * Short description for the Function
    *
    * @arguments  
    * @return values    
    */
    public function set_language()
    {
        //Sets the language to whatever is in the 3rd segment
        //http://ellislab.com/codeigniter/user-guide/libraries/uri.html
        $this->session->set_userdata('lang',$this->uri->segment(3));
        
        //Redirects to the home controller (which defaults to index)
        redirect('home','location');
    }


//View.php
<div>
&lt;?php
echo lang('text_key');
?&gt;
</div>

#5
[eluser]chefnelone[/eluser]
this is what I've done:
in applications/controller/home.php

Code:
&lt;?php
class Home extends Controller
{


    public function index()
    {
        $this->load->helper('language');
        $this->lang->load('email',$this->session->userdata('language'));
        $this->load->view('home');
    }

   public function set_language()
    {
        //Sets the language to whatever is in the 3rd segment
        //http://ellislab.com/codeigniter/user-guide/libraries/uri.html
        
        $this->session->set_userdata('lang',$this->uri->segment(3));

        //Redirects to the home controller (which defaults to index)
        redirect('home','location');

    }

}

in application/views/home.php

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;?php
    echo lang('email_must_be_array');
    ?&gt;
    
    &lt;?php
    echo anchor('home/set_language/spanish', 'Spanish');
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;

When I first load it, it shows the the correct text:
Code:
The email validation method must be passed an array.

Then I clic on Spanish links and it still loads the text in english. Instead of the spanish version.

I already have the 'spanish' folder inside: 'system/language/'

What am I doing wrong??

#6
[eluser]JHackamack[/eluser]
I believe the language should be /Application/Language/ folder

Rather than /System/Language

#7
[eluser]chefnelone[/eluser]
[quote author="JHackamack" date="1263939151"]I believe the language should be /Application/Language/ folder

Rather than /System/Language[/quote]

I tried it. But still no working.

Should I load the spanish language in config.php?

#8
[eluser]JHackamack[/eluser]
Right before you load the language can you:
Code:
echo $this->session->userdata('language');
what does it give you?

#9
[eluser]chefnelone[/eluser]
solved!
there was a typo mistake:

$this->session->set_userdata('lang',$this->uri->segment(3));
must to be
$this->session->set_userdata('language',$this->uri->segment(3));

thanks JH.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.