Create Multiple Language Using Codeigniter


Dear All I would like to share to all of you about how to create multiple languages using codeigniter. Please read description below and download by click here

1. Create your lanaguage file in folder language:

  • languag
    • khmer
      • khmer_lang.php
  • english
      • english_lang.php
  • Chinese
    • chinese_lang.php
  • In each file write text you want to translate
<![CDATA[

//example: english_lang.php

 $lang['hellow']  =  'Hello world';

.............................................................

//khmer_lang.php:

 $lang['hellow'] = 'សួស្តី';

//chinese_lang.php :

$lang['hellow'] = '你好';

]]>

2. Load language, and helper language in autoload.php
            ->application->config->autoioad.php
           $autoload['language'] = array('english'); // we use for set deduault language
           $autoload['helper'] = array('url','language','lang_helper'); // load url, language and lang_helper
3. create file in view.php

<![CDATA[

session->userdata("lang") == null ? "english" : $this->session->userdata("lang");

$this->lang->load($lang, $lang);

?>

    <title>Welcome CI multi language | Youlay</title>

    <style type="text/css">

    ::selection { background-color: #E13300; color: white; }

    ::-moz-selection { background-color: #E13300; color: white; }

    body {

       background-color: #fff;

        margin: 40px;

        font: 13px/20px normal Helvetica, Arial, sans-serif;

        color: #4F5155;

    }

    a {

        color: #003399;

        background-color: transparent;

        font-weight: normal;

    }

    h1 {

        color: #444;

        background-color: transparent;

        border-bottom: 1px solid #D0D0D0;

        font-size: 19px;

        font-weight: normal;

        margin: 0 0 14px 0;

        padding: 14px 15px 10px 15px;

    }

    code {

        font-family: Consolas, Monaco, Courier New, Courier, monospace;

        font-size: 12px;

        background-color: #f9f9f9;

        border: 1px solid #D0D0D0;

        color: #002166;

        display: block;

        margin: 14px 0 14px 0;

        padding: 12px 10px 12px 10px;

    }

    #body {

        margin: 0 15px 0 15px;

    }

    p.footer {

        text-align: right;

        font-size: 11px;

        border-top: 1px solid #D0D0D0;

        line-height: 32px;

        padding: 0 10px 0 10px;

        margin: 20px 0 0 0;

    }

    #container {

        margin: 10px;

        border: 1px solid #D0D0D0;

        box-shadow: 0 0 8px #D0D0D0;

    }

    </style>

<div id="container">

    <h1>

Welcome to CodeIgniter!</h1>

<h1>

Multiple Language</h1>

<div id="body">

<!-- Note welcome/languages/english; welcome: name of controller, languages: function name; english is the value for pass to function -->

            <a href="echo base_url();?>welcome/languages/english">English</a>

            <a href="echo base_url();?>welcome/languages/khmer">Khmer</a>

            <a href="echo base_url();?>welcome/languages/chinese">Chinese</a>

        </div>

<div id="body">

    </div>

<div class="footer">

Page rendered in <strong>0.0455</strong> seconds. ' . CI_VERSION . '</div>

</div>

]]>

 4.Create function languages() in controller Welcome.php

<![CDATA[

session->userdata("lang") == null ? "english" : $this->session->userdata("lang");

            $this->lang->load($lang, $lang);

        }

    public function index()

    {

        $this->load->view('welcome_message');

    }

        public function languages($get_lang){

            $this->session->set_userdata('lang',$get_lang);

            redirect(base_url());

        }

}

]]>

5.Create helper file in folder helper(lang_helper.php)

<![CDATA[

lang->line($label);

        if($rs){

            return $rs;

        else {

            return $label;

        }

    }

?>

]]>

Hiring Services

  • Build website
  • Develop web application
  • Graphic design
  • Maintenance and Security
  • Update website
  • Seach Egning Organization
  • Other...

My Facebook