Tutorial – Multi-Language implementation in CodeIgniter Framework


The Multi-Language feature is necessary for the modern web application. This feature is used for the purpose of internationalization. In this tutorial, we will discuss how to implement the multi-language feature in CodeIgniter framework. Using CodeIgniter’s Language class, you can easily make your site multi-language. CodeIgniter’s Language class provides some functions to retrieve language files and lines of text.

Site Default Language

Open the application/config/config.php file and specify the site’s default language.

Creating Language Files

Create multiple language files and insert those files into the application/language/ directory with separate sub-directories for each language (for example, french, german, etc.).

The language files must be named with _lang suffix. For example, if you want to create a file containing site’s message translation then the file name might be message_lang.php.

Language files structure would look like following.

  • application/
    • language/
      • english/
        • message_lang.php
        • ……
      • french/
        • message_lang.php
        • ……
      • german/
        • message_lang.php
        • ……
      • ……

Within the language file (message_lang.php) you would need to assign each line of text to $lang array.

Sample language files are given below.

  • english/message_lang.php file would be look like the following.

  • french/message_lang.php file would be look like the following.

  • german/message_lang.php file would be look like the following.

Loading Language Files

In order to fetch a line from a particular language file, you need to load the language file first. There are two options for loading a language file.

In Controller:
Into the controller’s __construct() function write the following code.

If you choose this option, you need to load language file in all controller of your application.

Using Hooks:
For using the hooks option you need to enable the hooks feature in application/config/config.php file.

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

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

Fetching line of text

After loading the language file you can fetch the line text using the following code.

Switch Different Languages

Now it’s time to switch different languages and make your site multilingual. We will use the session to store user’s requested language and load the respective language.

Before starting to implement multilingual features, open the application/config/autoload.php file and load SESSION library and URL helper.

We will create LanguageSwitcher controller for handing the language switch. application/controllers/LanguageSwitcher.php file contain the following code.

Open the application/hooks/LanguageLoader.php file and replace file’s code with the following code.

Display the languages dropdown into view. Once the language option is changed, the switchLang() function of LanguageSwitcher controller would be called. switchLang() function set the site language into session based on the requested language and redirected to the view page. The sample languages dropdown and multi-language welcome message code is given below.

Also, you can notice that we are used the language switcher URL without index.php. So, you should need to create a .htaccess file in root directory and this file will contain the following code.


Leave A Reply

Your email address will not be published.