I am currently building a website with codeigniter 2.1.2. Now I am encountering a problem to efficiently building a multi-language website.
I have read through the language class tutorial and helper and I have stored two language folders "Chinese" and "English".
What I am intending to do is I want a language session variable called "lang". And I want the following:
- if no session variable "lang" exists, I will create one and point it to "english"
- if the session variable is already set and user clicked for a change of language, then the session variable will change from either "chinese" to "english" or "english" to "chinese".
I am quite new to PHP and Codeigniter, All I can think of now is something like the following. Can anyone shine some light as of how I can efficiently allow users to change language on my site?
I have no idea how to change session variable upon user's click event.
// Load Language Files
if(!$this->session->userdata('lang')) {
$this->session->set_userdata('lang','english');
$lang = 'english';
} else {
$lang = 'chinese';
}
$this->lang->load('pages/header',$lang);
I have created a language controller and set the session variable, and then redirect the page back to the previous page. Is it a good way?
<?php
Class language Extends CI_Controller {
function index() {
if(!$this->session->userdata('lang')) {
$this->session->set_userdata('lang','english');
} else {
$lang = $this->session->userdata('lang');
if ($lang =="chinese") {
$this->session->set_userdata('lang','english');
} elseif ($lang == "english") {
$this->session->set_userdata('lang','chinese');
} else {
$this->session->set_userdata('lang','english');
}
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
}