multi language in codeigniter from database

this is my short tutorial about multi languange. first you should download database here

and we are going to set up bassic instalation here is my config database

  • $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'test';
    $db['default']['dbdriver'] = 'mysql';

and copas 2 file bellow here to your application/core there is give it name MY_Config.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // CodeIgniter i18n library by Jérôme Jaglale
    // http://maestric.com/en/doc/php/codeigniter_i18n
    // version 10 - May 10, 2012
    
    class MY_Config extends CI_Config {
    
     function site_url($uri = '')
     {
     if (is_array($uri))
     {
     $uri = implode('/', $uri);
     }
    
     if (class_exists('CI_Controller'))
     {
     $CI =& get_instance();
     $uri = $CI->lang->localized($uri);
     }
    
     return parent::site_url($uri);
     }
    
    }
    
    /* End of file */
    

and this give it name MY_Config.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // CodeIgniter i18n library by Jérôme Jaglale
    // http://maestric.com/en/doc/php/codeigniter_i18n
    // version 10 - May 10, 2012
    
    class MY_Lang extends CI_Lang {
    
     /**************************************************
     configuration
     ***************************************************/
    
     // languages
     var $languages = array(
     'en' => 'english',
     'fr' => 'french'
     );
    
     // special URIs (not localized)
     var $special = array (
    
     );
    
     // where to redirect if no language in URI
     var $default_uri = ''; 
    
     /**************************************************/
    
     function __construct()
     {
     parent::__construct(); 
    
     global $CFG;
     global $URI;
     global $RTR;
    
     $segment = $URI->segment(1);
    
     if (isset($this->languages[$segment])) // URI with language -> ok
     {
     $language = $this->languages[$segment];
     $CFG->set_item('language', $language);
    
     }
     else if($this->is_special($segment)) // special URI -> no redirect
     {
     return;
     }
     else // URI without language -> redirect to default_uri
     {
     // set default language
     $CFG->set_item('language', $this->languages[$this->default_lang()]);
    
     // redirect
     header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
     exit;
     }
     }
    
     // get current language
     // ex: return 'en' if language in CI config is 'english'
     function lang()
     {
     global $CFG;
     $language = $CFG->item('language');
    
     $lang = array_search($language, $this->languages);
     if ($lang)
     {
     return $lang;
     }
    
     return NULL; // this should not happen
     }
    
     function is_special($uri)
     {
     $exploded = explode('/', $uri);
     if (in_array($exploded[0], $this->special))
     {
     return TRUE;
     }
     if(isset($this->languages[$uri]))
     {
     return TRUE;
     }
     return FALSE;
     }
    
     function switch_uri($lang)
     {
     $CI =& get_instance();
    
     $uri = $CI->uri->uri_string();
     if ($uri != "")
     {
     $exploded = explode('/', $uri);
     if($exploded[0] == $this->lang())
     {
     $exploded[0] = $lang;
     }
     $uri = implode('/',$exploded);
     }
     return $uri;
     }
    
     // is there a language segment in this $uri?
     function has_language($uri)
     {
     $first_segment = NULL;
    
     $exploded = explode('/', $uri);
     if(isset($exploded[0]))
     {
     if($exploded[0] != '')
     {
     $first_segment = $exploded[0];
     }
     else if(isset($exploded[1]) && $exploded[1] != '')
     {
     $first_segment = $exploded[1];
     }
     }
    
     if($first_segment != NULL)
     {
     return isset($this->languages[$first_segment]);
     }
    
     return FALSE;
     }
    
     // default language: first element of $this->languages
     function default_lang()
     {
     foreach ($this->languages as $lang => $language)
     {
     return $lang;
     }
     }
    
     // add language segment to $uri (if appropriate)
     function localized($uri)
     {
     if($this->has_language($uri)
     || $this->is_special($uri)
     || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
     {
     // we don't need a language segment because:
     // - there's already one or
     // - it's a special uri (set in $special) or
     // - that's a link to a file
     }
     else
     {
     $uri = $this->lang() . '/' . $uri;
     }
    
     return $uri;
     }
    
    }
    
    /* End of file */
    

let’s create controller just change welcome.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome extends CI_Controller {
    
     public function __construct() {
     parent::__construct();
    
     $this->load->model('welcome_model');
    
     $this->lang->load('welcome');
     }
    
     public function index()
     {
     $current_lang = $this->lang->lang();
    
     $data['records'] = $this->welcome_model->get_all_data($current_lang);
    
     $this->load->view('welcome_message',$data);
     }
    }
    
    /* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */

and model here give it name welcome_model.php

  • <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Welcome_model extends CI_Model {
     function get_all_data($lang)
     {
     return $this->db->get_where('language',array('lang'=>$lang))->result();
     }
    }

also view

  • <!DOCTYPE html>
    <html lang="en">
    <head>
     <meta charset="utf-8">
     <title>Welcome to CodeIgniter</title>
    
     <style type="text/css">
    
     ::selection{ background-color: #E13300; color: white; }
     ::moz-selection{ background-color: #E13300; color: white; }
     ::webkit-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;
     -webkit-box-shadow: 0 0 8px #D0D0D0;
     }
     </style>
    </head>
    <body>
    
    <div id="container">
     <h1>Welcome to CodeIgniter!</h1>
    
     <?php echo anchor('kategori', 'Kategori');?>
    
     <?php echo anchor($this->lang->switch_uri('fr'),'French'); echo ' | '.anchor($this->lang->switch_uri('en'),'English');?>
     <div id="body">
    
     <?php if(count($records)>0):?>
    
     <?php foreach($records as $record):?>
    
     <?php echo $record->title; ?>
     <br>
     <?php echo $record->text; ?>
    
     <?php endforeach; ?>
     <?php endif; ?>
     </div>
    
    </div>
    
    </body>
    </html>

in this application i just create french and also english, if you are going to change languange just open it application/core/MY_Lang.php change languange what you want to do example ‘in’=>indonesia and also open database and make sure same in database.

  • var $languages = array(
     'en' => 'english',
     'fr' => 'french'
     );

well below here is screen shot my simple application multi languange english and french
paint

well i’m sorry guys if i’m not good to tell tutorial here, whole script you can get it here

this tutorial came from here http://news.nationalgeographic.com/news/2015/02/150206-global-warming-sea-slugs-animals-ocean-science-california/

feel free to comment here bro because i’m still learn Codeigniter, if you have another way to make multi languange, would you share it here ?

  • Boomi Nathan

    Nice work !! I hope this will help people good job!

    • freddy

      no you are good bro, help other understand programming :D

      Happy to help