views:

20

answers:

1

I followed this tutorial: http://codeigniter.com/wiki/Internationalization_and_the_Template_Parser_Class/

The controller that loads the language is this one:

<?php

class Example extends Controller {

    function Example() {
        parent::Controller();

        # Load libraries
        $this->load->library('parser');

        # Load language
        $this->lang->load('example', 'english');
    }

    function index() {
        # Load variables into the template parser
        $data = $this->lang->language;

        # Display view
        $this->parser->parse('example', $data);
    }

}

?>

In order to change the language I have to manually change english to say spanish in the controller.

What's the best way the user can do this from the index.php file (view)?

+2  A: 

The best thing to do is have the user select a supported language on some page, set it as a session variable and call it when ever you need to load a language

$language = $this->session->userdata("language");

$this->lang->load("example", $language);

$data = $this->lang->language;

$this->parser->parse("example", $data);

EDITED BELOW

If you're using CodeIgniter and you're new to this, I wouldn't suggest messing with the index.php file.

You want to do it inside your controller by loading a form where they can pick their language and storing it in the session. I'd also suggest autoloading your session library.

The controller:

<?php

class Home extends Controller {

    function Home()
    {
        parent::Controller();

        $this->load->library("session");
    }

    function index()
    {
        $language = $this->session->userdata("language");

        $this->lang->load("example", $language);

        $data = $this->lang->language;

        $this->parser->parse("example", $data);
    }

    function set_lang()
    {
        if( ! $this->form_validation->run())
        {
            $this->load->view("select_language_form");
        }
        else
        {
            $language = $this->input->post('language', TRUE);

            $this->session->set_userdata('language', $language);

            redirect('home' 'location');
        }
    }
}
bschaeffer
@bschaeffer Sorry, I'm a beginner. Can you give me an example of the code for the index.php file? (where the user clicks the links to change the language).
janoChen