0

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:

  1. if no session variable "lang" exists, I will create one and point it to "english"
  2. 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']);
    }
}

2 Answers 2

7

make a language controller. Direct a user to relevant method when user click language link http://www.yoursite.com/language/english for English language and similar for chinese. I have used cookie instead of session variables.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Language extends CI_Controller{
    function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->english();
    }

    public function english(){
        $this->lang->load('labels', 'english');
        //$this->session->set_userdata("LANGUAGE","english");
        $cookie = array(
                    'name'   => 'LANGUAGE',
                    'value'  => 'english',
                    'expire' => 604800
                );
        $this->input->set_cookie($cookie);
        header("location: ".$_GET["ref"]);
    }

    public function chinese(){
        $this->lang->load('labels', 'chinese');
        //$this->session->set_userdata("LANGUAGE","chinese");
        $cookie = array(
                    'name'   => 'LANGUAGE',
                    'value'  => 'chinese',
                    'expire' => 604800
                );
        $this->input->set_cookie($cookie);
        header("location: ".$_GET["ref"]);
    }

}



?>
Sign up to request clarification or add additional context in comments.

Comments

0

The best way for building a Multi-Language website with CodeIgniter is using Language class, hooks and controller.

If you want to use the only controller, create a language switcher controller (LanguageSwitcher) and write the following code.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LanguageSwitcher extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->load->helper('language');
        $siteLang = $this->session->userdata('site_lang');
        if ($siteLang) {
            $this->lang->load('message',$siteLang);
        } else {
            $this->lang->load('message','english');
        }   
    }

    function switchLang($language = "") {

        $language = ($language != "") ? $language : "english";
        $this->session->set_userdata('site_lang', $language);

        redirect($_SERVER['HTTP_REFERER']);

    }
}

Complete Multi-Language tutorial can be found from here - Multi-Language implementation in CodeIgniter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.