Categories: Learn CodeIgniter Tutorials, Tips And GuidesLearn PHP Tutorials, Tips And Guides

Enable Multi Language Capability in CodeIgniter

Reading Time: 3 minutes

Gone are the days when English was the only language for the Internet. these days, users expect web apps to speak their language. In practical terms, this means that the UI of the web app is in the native language of the visitor. Fortunately, all full stack PHP frameworks allow developers to create multilingual interfaces for PHP applications without rewriting code for every language.

In this tutorial, I will discuss how you could easily develop multilingual PHP applications using CodeIgniter on any hosting for PHP. I will also cover several ideas for customizing the core functionality.

Specify Default Language and Language Options

Go to application/config folder and open config.php. Add the following line to specify the website’s default language.

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

Now to create the language options, of to application/language folder and create separate subfolders for each language. Next, in each subfolder, create a file with the suffix _lang. For instance, if you wish to offer a file named information.php in English and German languages, use the following structure:

Application→ language → English → information_lang.php

Application→ language → German → information_lang.php

You might also like: How To Host CodeIgniter On Cloud

Set up Language Files

Within the language file, information_lang.php, add every line of text to a $lang array in the following format:

$lang['language_key'] = 'type your message';

For instance, the entry for the text line, “Type message in German” in the file in the German language folder would look like:

$lang['welcome_message'] = 'Type message in German';

Load Language File

Once the files are in place, it is time to load the language files.

For this, add the following code to the controller’s _construct() function:


Next to use the hooks, add the following line to the application/config/config.php file.

$config['enable_hooks'] = TRUE;

Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:

$hook['post_controller_constructor'] = array(

   'class'    => 'LanguageLoader',

   'function' => 'initialize',

   'filename' => 'LanguageLoader.php',

   'filepath' => 'hooks'


Go to application/hooks folder and create a file named LanguageLoader.php. Create the LanguageLoader class in the file and add the following code to it:

class LanguageLoader


   function initialize() {

       $ci =& get_instance();




This will load the appropriate language file. Now you could easily fetch individual text lines through the following code


Switch Language

At this point all the necessary ideas for multilingual web apps are in place. It is time to switch languages. But before this, open the application/config/autoload.php file and load SESSION library and the URL helper.

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

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

Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:

<?php if ( ! defined('BASEPATH')) exit('Direct access allowed');

class LanguageSwitcher extends CI_Controller


   public function __construct() {



   function switchLang($language = "") {

       $language = ($language != "") ? $language : "english";

       $this->session->set_userdata('site_lang', $language);




Now replace the code in application/hooks/LanguageLoader.php file with the following code:


class LanguageLoader


   function initialize() {

       $ci =& get_instance();


       $siteLang = $ci->session->userdata('site_lang');

       if ($siteLang) {


       } else {





Notice that I have use the URl for switching language without index.php. Thus I will a .htaccess file in the root directory with the following code:

RewriteEngine on

RewriteCond $1 !^(index\.php)

RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]

You might also like: How To Create A REST API In Codeigniter With Basic Authentication

Wrapping Up

As you could see, creating a multilingual website in CodeIgniter is simple enough. If you need help in implementing the idea in your CodeIgniter project, just drop a line and I will get back to you.

Owais Alam :is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com