CodeIgniter Forums
Language switch with sessions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Language switch with sessions (/showthread.php?tid=55615)

Pages: 1 2 3


Language switch with sessions - El Forum - 11-03-2012

[eluser]chris1986m[/eluser]
Hi,

i have a problem with my language switch.
Its a simple language switch. Only two buttons which calls the languageSwitch Method and refresh the page.
The Problem is, that the session variable is written after the second click on the Button. On the first click, the page makes a reload and the default language is selected, not the clicked one.
After the second click, everythink works fine! I have to click twice on the first time. After this, the language is change on every click!

Thanks for all your help!

The the links:
/start/languageSwitch/de
/start/languageSwitch/en

Here my simple code
Code:
$autoload['libraries'] = array('session', 'user_agent');
$autoload['language'] = array();

$config['language'] = 'de';

Code:
class Start extends CI_Controller {
    public function index() {
        $this->load->helper('url');
        $this->load->helper('language');
        $this->lang->load('main', $this->session->userdata('language'));
  
//Check language
        if (!$this->session->userdata('language'))
            $this->session->set_userdata('language','de');
        
        //Check mobile version
        if ($this->agent->mobile())
            $this->load->view('mobile');
        else
            $this->load->view('index');        
}

    public function languageSwitch() {
     $this->load->helper('url');
$this->load->helper('language');
$this->lang->load('main', $this->session->userdata('language'));
  
$this->session->set_userdata('language',$this->uri->segment(3));
redirect('http://www.test.de/','refresh');
    }
}

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]



Language switch with sessions - El Forum - 11-03-2012

[eluser]chris1986m[/eluser]
Nobody knows?

I think there are problems with saving the session variable on the first click. But I dont know how to fix this.

Chris


Language switch with sessions - El Forum - 11-03-2012

[eluser]siptik[/eluser]
Is "Start" default controller?


Language switch with sessions - El Forum - 11-03-2012

[eluser]siptik[/eluser]
It's works fine for me
After the first click language is changed!




Language switch with sessions - El Forum - 11-03-2012

[eluser]siptik[/eluser]
Code:
class Start extends MY_Controller {
    
    public function index() {
        $this->lang->load('main', $this->session->userdata('language'));
  
        if (!$this->session->userdata('language'))
            $this->session->set_userdata('language','russian');
  
            $this->load->view('index');        
     }

    public function languageSwitch() {
       $this->session->set_userdata('language',$this->uri->segment(3));
       redirect('http://localhost/ci/start','refresh');
    }
}

veiw:
Code:
<a href="&lt;?=base_url()?&gt;start/languageSwitch/russian">ru</a>
<a href="&lt;?=base_url()?&gt;start/languageSwitch/english">english</a>
&lt;? echo $this->session->userdata('language');?&gt;



Language switch with sessions - El Forum - 11-03-2012

[eluser]chris1986m[/eluser]
Hey thanks for your response.
Yes "Start" is my default Controller.

I've checked the code again, but it doesnt work.

When I remove the redirect row and make a manual reload, then it works fine.

Is it possible, that the browser makes a reload before the session variable is saved?!


Language switch with sessions - El Forum - 11-03-2012

[eluser]siptik[/eluser]
try it:
Code:
&lt;?php

class Test extends CI_Controller {
    
    public function index() {
$this->load->helper('url');

        if (!$this->session->userdata('language'))
        $this->session->set_userdata('language','russian');

        $this->load->view('test_view');        
     }

    public function languageSwitch() {
       $this->load->helper('url');
       $this->session->set_userdata('language',$this->uri->segment(3));
       redirect('http://localhost/ci/start','refresh');
    }
}

and "test_view":
Code:
<a href="&lt;?=base_url()?&gt;start/languageSwitch/russian">ru</a>
<a href="&lt;?=base_url()?&gt;start/languageSwitch/english">english</a>
&lt;? echo $this->session->userdata('language');?&gt;







Language switch with sessions - El Forum - 11-03-2012

[eluser]siptik[/eluser]
delete cookie before a test !!!


Language switch with sessions - El Forum - 11-05-2012

[eluser]chris1986m[/eluser]
So after long testings on few systems with different browsers, can I say.... I'm going crazy!

The language switch works only after the first switch is correct. For this I have to click the button twice.

The first click on the english button reloads the site and the language is german =(
The second click on the english button reloads the site and the language is english =)

After the second click I can change the language after every click... ^^

Very strange....

Anybody have an Idea?


Language switch with sessions - El Forum - 11-05-2012

[eluser]Ewout[/eluser]
Maybe the first click doesn't seem to effect the page because the language load happens before the initialization in the index function.