Multi-Language setup in CodeIgniter

Teguh Arief

Sep 24, 2020

Share article to

Multi-language in CodeIgniter.

The Multi-Language feature, also known as internationalization, is necessary for the modern web application. In this tutorial, we will discuss how we can enable multiple languages using CodeIgniter. You can easily make your site multi-language. This is the solution for non-editable text, but it doesn't cover any multi-language content stored and retrieved from a database. 1. Configuration default Language Open the application/config/config.php file and specify the site’s default language.


$config['language'] = 'english';


2. Create Multi-Language files Create multiple language files and insert those files into the application/language/ directory with separate sub-directories for each language (for example, English, Indonesian, etc.). Language files structure would look like following.


  • application/


    • language/


      • english/


        • welcome_lang.php

        • ……



      • indonesian/

        • welcome_lang.php

        • ……



      • chinese/

        • welcome_lang.php

        • ……



      • arabic/

        • welcome_lang.php

        • ……



      • ……







Sample language files are given below. english/welcome_lang.php file would be look like the following.


$lang['welcome_message'] = 'Welcome!';


indonesian/welcome_lang.php file would be look like the following.


$lang['welcome_message'] = 'Selamat datang!';


chinese/welcome_lang.php file would be look like the following.


$lang['welcome_message'] = '斯拉末大唐!';


arabic/welcome_lang.php file would be look like the following.


$lang['welcome_message'] = 'سلامات داتانغ!';


3. Loading Language Files Open the application/controllers/welcome.php and into the controller’s __construct() function write the following code.


public function __construct() {
parent::__construct();
$this->load->helper('language');
$this->lang->load('welcome');
}


Open the application/config/hooks.php file and define a hook.


$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);


Create LanguageLoader class in LanguageLoader.php file inside the application/hooks/ directory.


class LanguageLoader
{
public function initialize() {
$ci =& get_instance();
$ci->load->helper('language');

$site_lang = $ci->session->userdata('site_lang');
if ($site_lang) {
$ci->lang->load('message',$ci->session->userdata('site_lang'));
} else {
$ci->lang->load('message','english');
}
}
}


4. Switching Between Different Languages Create Langswitch class in langswitch.php file inside the application/controllers/ directory.



class Langswitch extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->helper('url');
}

public function switchLanguage($language = "") {
$language = $this->uri->segment(1,'english');
$this->session->set_userdata('site_lang', $language);
redirect(base_url());
}
}


5. Page needs Core system setup We need config setup for related page. Create MY_Config class in MY_Config.php file inside the application/core/ directory.


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);
}

}


Then to load the language, create MY_Lang class in MY_Lang.php file inside the application/core/ directory.



class MY_Lang extends CI_Lang {

var $languages = array(
'en' => 'english',
'id' => 'indonesian',
'cn' => 'chinese',
'sa' => 'arabic'
);

var $special = array (
""
);

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
{
$CFG->set_item('language', $this->languages[$this->default_lang()]);

header("Location: " . $CFG->site_url($this->localized($this->default_uri)), TRUE, 302);
exit;
}
}

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;
}

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;
}

function default_lang()
{
foreach ($this->languages as $lang => $language)
{
return $lang;
}
}

function localized($uri)
{
if($this->has_language($uri)
|| $this->is_special($uri)
|| preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri))
{

}
else
{
$uri = $this->lang() . '/' . $uri;
}

return $uri;
}

}

/* End of file */


6. Set default Route Controller


$route['^en$'] = $route['default_controller'];
$route['^id$'] = $route['default_controller'];
$route['^cn$'] = $route['default_controller'];
$route['^sa$'] = $route['default_controller'];


7. Define links to switch Multi-Language Then we need to define links to switch each of the available languages.


English
Indonesian
Chinese
Arabic


8. Fetching line of text Fetch the line text in welcome.php file inside the application/views/ directory.