Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make multilanguage website in Codeigniter

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.32k
    Comment on it

    Hello Reader's if you are looking to desing the multilaunge website then codeigniter offers you to do this with very easy steps. First learn what is multilanguage website.

     

    A multilanguage website is which can run in any langauge which ever the language are available. To make all languages available the admin of the website insert and add a new langauge to website and website will turn into that language.

    Lets us take the case of codeingiter framework in PHP. To add language in website you have to do as the steps given below:-

     

    Step1 : First create new folder in language folder. (you will find it in the location  Application->Language)

    Rename this folder with the new language you want to add in website (for arabic rename it "arabic")

     

    Step2. Now create  a new php file in it and name to "message_lang.php"

    Similarly add more folders and create files in it. ei hindi, english.

     

    Step3. Create new folder inside the folder "Hooks" (You will find it  in the location Application )

    Create a new file inside it and name it to "LanguageLoader.php"

    This file will act as a switch between different languages. And paste the code below it:-

    <?php
    class LanguageLoader
    {
        function initialize() {
            $ci =& get_instance();
            $ci->load->helper('language');
            $siteLang = $ci->session->userdata('site_lang');
            if ($siteLang) {
                $ci->lang->load('message',$siteLang);
            } else {
                $ci->lang->load('message','english');
            }
        }
    }

    This function initialize() will use ur session and set the langauge which user want it to seen on website. Note here english is the default langauge if no language is selected for first time.

     

    Now again come back to controllers and create a new controller name it to "languageSwitcher.php" and paste the code as below:-

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class LanguageSwitcher extends CI_Controller
    {
      public function __construct() {
        parent::__construct();  
      }
    
      function switchLang($language = "") {   
        $language = ($language != "") ? $language : "english";
        $this->session->set_userdata('site_lang', $language);   
        redirect($_SERVER['HTTP_REFERER']);   
      }
    }

     

    Now we are all set to make it action. Just open your header file and give a select box to user for selecting the language as code below:-

     <select onchange="javascript:window.location.href='<?php echo base_url(); ?>LanguageSwitcher/switchLang/'+this.value;">
        <option value="english" <?php if($this->session->userdata('site_lang') == 'english') echo 'selected="selected"'; ?>>English</option>
        <option value="arabic" <?php if($this->session->userdata('site_lang') == 'arabic') echo 'selected="selected"'; ?>>Arabic</option>
        
    </select>

     

    Step4. Now open the view page and replace all the static words by php variable which we will define in those files which we have already created earlier

    For example my message_lang.php  in arabic folder is as below:-

    <?php
    $lang['welcome_message'] = '   ';
    $lang['Browse'] = '';
    $lang['Sell'] = '';
    $lang['My_Tajerlee'] = ' Tajerlee';
    $lang['Help'] = '';
    $lang['Log_in_to_continue'] = '   ';
    $lang['Not_a_member?'] = ' ';
    $lang['Register_now'] = ' ';
    $lang['Email'] = ' ';
    $lang['Password'] = ' ';
    $lang['Remember_me'] = '';
    $lang['Login'] = ' ';

     

    And the same translation of it in english in mesage_lang.php in english folder  is as follows

    <?php
    $lang['welcome_message'] = 'This is english test';
    $lang['Browse'] = 'Browse';
    $lang['Sell'] = 'Sell';
    $lang['My_Tajerlee'] = 'My Tajerlee';
    $lang['Help'] = 'Help';
    $lang['Log_in_to_continue'] = 'Log in to continue';
    $lang['Not_a_member?'] = 'Not a member?';
    $lang['Register_now'] = 'Register now';
    $lang['Email'] = 'Email';
    $lang['Password'] = 'Password';
    $lang['Remember_me'] = 'Remember me';
    $lang['Login'] = 'Login';
    ?>

    Here you have to defind each and every words in all the message_lang.php files for all the language folders.

    Now last open your view files replace all the static words by keys of this $lang array.

    for example word "Login" will be replaced by <?php echo $this->session->flashdata('Login');?>

    Once you finished replacing the words load it. You will seeing on selecting the language from select box in header the page is resfreshed and all the word are fetched from that langauge file and converted in the selected languages.

 1 Comment(s)

  • You can easily enable multi lanuage in Codeigniter by adding lang load code to controller’s _construct() function and enable hook in application/config/config.php file. Source: https://www.cloudways.com/blog/multi-language-codeigniter/ After that, just create LanguageLoader class in LanguageLoader file.
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: