How to Use Language Helper in Codeigniter

In this post, I would like to show how to use language helper to make multilingual application in codeigniter. Multi language, also known as internationalization. It is a key feature of web applications. Using codeigniter’s language helper class, we can make it easily. Just we need to load language helper and to make some configuration.Let see with an example.
First of all we need to create languages directory. Language directories is the actual directory which contain messages in different languages like English,hindi and other languages. Create directories called english and hindi inside application/language and create file inside both of these directories called main_lang.php and put messages like i am showing below.
For English directory.
main_lang.php
[php] <?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’); $lang[‘welcome_techalltype’] = ‘Welcome to Techalltype’; [/php]
For hindi directory.
main_lang.php
[php] <?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’); $lang[‘welcome_techalltype’] = ‘टेक आल टाइप में आपका स्वागत है’; [/php]
2. Create a view called language.php inside application/views directory.Below is the code.
[php] <html> <head> <title>Codeigniter Language Helper Example</title> <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”> </head> <body> <style> h3{ font-family: Verdana; font-size: 18pt; font-style: normal; font-weight: bold; color:red; text-align: center; } table{ font-family: Verdana; color:black; font-size: 12pt; font-style: normal; font-weight: bold; text-align:left; border-collapse: collapse; } .error{ color:red; font-size: 11px; } </style> <h3>Codeigniter Language Helper Example</h3><br> <?php echo form_open(‘languageController’,array(‘name’=>’language’, ‘method’=>’post’)); ?> <table align=”center” cellpadding = “5”> <tr> <tdalign=”right”><selectname=”langs”> <optionvalue=”english”<?php echo set_select(‘langs’,’english’, ( !empty($lang) && $lang ==”english”?TRUE:FALSE )); ?>>English</option> <optionvalue=”hindi”<?php echo set_select(‘langs’,’hindi’, ( !empty($lang) && $lang ==”hindi”?TRUE:FALSE )); ?>>Hindi</option> </select></td> <td align=”center”><input type=”submit” class=”btn btn-success” value=”Change Language”/></td> </tr> <tr> <tdcolspan =”2″> <fontstyle=”font-size:45px”><?php echo lang(‘welcome_techalltype’); ?></font></td> </td> </tr> </table> <?php echo form_close();?> </body> </html> [/php]
3. Next, create a controller called LanguageController.php inside application/controllers. Here is how code looks.
[php] <?php defined(‘BASEPATH’) OR exit(‘No direct script access allowed’); class LanguageController extends CI_Controller { publicfunction __construct(){ parent::__construct(); } public function index() { $lang = $this->input->post(‘langs’); $data=array( ‘lang’=>$lang ); $this->load_language($lang); $this->load->view(‘language’,$data); } public function load_languages($system_lang){ $files = $this->all_files(); if (!empty($system_lang)) { foreach ($files as $file => $altpath) { $shortfile = str_replace(“_lang.php”, “”, $file); $this->lang->load($shortfile, $system_lang); } } else { foreach ($files as $file => $altpath) { $shortfile = str_replace(“_lang.php”, “”, $file); $this->lang->load($shortfile, ‘english’); } } } public function all_files(){ $language = array( “main_lang.php”=>”./application/”, ); return $language; } } [/php]
Above code contain two functions, one is load_languages and the other is all_files. load_languages is for load default language. and all_files is for load all files of the default language.
4. We need to load language helper inside config.php file.
[php] $autoload[‘helper’] = array(‘url’,’html’,’file’,’form’,’language’); [/php]
Finally, Go to the browser and run the application. You will see below interface. Change language from dropdown menu and after clicking the button you will get the desire result.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!