Multi Language in CodeIgniter Framework


In application/language folder, create new language files as below:

Create new folder name english. In this folder, create new PHP file named content_lang.php as below:

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

$lang['message'] = 'Hello World';
?>




Create new folder name french. In this folder, create new PHP file named content_lang.php as below:

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

$lang['message'] = 'Bonjour tout le monde';

?>

Create new folder name german. In this folder, create new PHP file named content_lang.php as below:

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

$lang['message'] = 'Hallo Welt';

?>

In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:

$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';

In CodeIgniter Project, Open autoload.php file in config folder. Add url helper to helper config as below:

$autoload['helper'] = array('url');




In CodeIgniter Project, Open autoload.php file in config folder. Add session library to libraries config as below:

$autoload['libraries'] = array('session');

Create new hook and config to run hook as below:

In CodeIgniter Project, Open config.php in config folder. Set value for enable_hooks config as below:

$config['enable_hooks'] = TRUE;

In CodeIgniter Project, Open hooks.php file in config folder. Define new hook as below:

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

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




Create new PHP file named LanguageLoader.php in application/hooks folder as below:

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

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

Create new PHP file named demo.php in controllers folder as below:

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

class Demo extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->session->set_userdata('site_lang', "english");
        $this->load->view('demo/index');
    }

    function switchLang($language = "")
    {
        $this->session->set_userdata('site_lang', $language);
        redirect('demo');
    }
}




In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'demo';

Create new folder named demo in views folder. In this folder, create new views as below:

Create new PHP file named index.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
	<head>
		<title>Language</title>
	</head>
	<body>

		<div style="margin:0 auto; text-align: center;">

            <a href="<?php echo site_url("demo/switchLang/english"); ?>">English</a> |
            <a href="<?php echo site_url("demo/switchLang/german"); ?>">German</a> |
            <a href="<?php echo site_url("demo/switchLang/french"); ?>">French</a>
            <br><br>
            <?php echo $this->lang->line('message'); ?>

        </div>

	</body>
</html>




Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index

Output

Select English language

Output

Select French language

Output

Select German language

Output

I recommend you refer to the books below to learn more about the knowledge in this article: