Multi-Language switcher in CodeIgniter

Multi-Language switcher in CodeIgniter

Jun 2, 2018 | CodeIgniter, MySql, PHP

The Multi-Language feature is necessary for the modern web application for the purpose of internationalization.Using CodeIgniter’s Language class, you can easily make your site multi-language in your website.
Multi-Language switcher in CodeIgniter

Multi-Language switcher in CodeIgniter

You just have follow some steps for building a multi-language website with CodeIgniter. If you are new to learn CodeIgniter, you can read Learn CodeIgniter Tutorial for Beginners first.
It is described in easy way of learning, each step have a idea to built it.

Step 1: Config Default Language

Open the application/config/config.php file and set the site’s default language.
$config['language'] = 'english';
Using Hooks:
For using the hooks option you need to enable the hooks feature in config file
$config['enable_hooks'] = TRUE;
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.

 

$autoload['libraries'] = array('session');
$autoload['helper'] = array('url');?
Now you should need to create a .htaccess file in root directory and this file will contain the following code.
	RewriteEngine on
	RewriteCond $1 !^(index.php)
	RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]

Step 2: Creating Some Language Files

applicationlanguage
Following language diretory you have to create some language’s directory.Each language director have a language array – $lang file.
Here 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.
Multi-Language switcher in CodeIgniter

Multi-Language switcher in CodeIgniter

In language files, you would need to assign each line of text to $lang array.
For example:
english/message_lang.php
$lang['welcome_message'] = 'Welcome to CodexWorld';
$lang['receive_text'] = 'Receive recent programming blogs and much more from us.';
$lang['subscribe'] = 'Subscribe my updates via Email:';?

 

Step 3:
Now you have to load default language in controller.
Into the controller’s __construct() function write the following code.
$this->lang->load('message','english');
Step 4:
Next to open application/config/hooks.php file and define a hook.
	$hook['post_controller_constructor'] = array(
	'class' => 'LanguagesLoader',
	'function' => 'initialize',
	'filename' => 'LanguagesLoader.php',
	'filepath' => 'hooks'
	);?

 

Step 5:
Create a class with the name LanguagesLoader in LanguagesLoader.php file inside the application/hooks/ directory.
LanguagesLoader.php file contain the following code.
 class LanguagesLoader{
	function initialize() {
	$ci =& get_instance();
	$ci->load->helper('language');
	$siteLang = $ci->session->userdata('site_lang');
	if ($siteLang) {
	$ci->lang->load('message',$siteLang);
	} else { 
	$ci->lang->load('message','english');
	}
	}
}
Step 6:
We will create LanguagesSwitcher controller for handing the language switch.application/controllers/LanguagesSwitcher.php file contain the following code.
class LanguagesSwitcher extends CI_Controller{
	public function __construct() {
	parent::__construct();
	}
	function switchLang($language = "") {
	$language = ($language != "") ? $language : "english";
	$this->session->set_userdata('site_lang', $language);
	redirect($_SERVER['HTTP_REFERER']);
	}
}
Step 7:

Display The language switcher on your view page

(View/welcome_message.php).

<select onchange="javascript:window.location.href='<?php echo base_url(); ?>LanguagesSwitcher/switchLang/'+this.value;">
	<option value="english" <?php if($this->session->userdata('site_lang') == 'english') echo 'selected="selected"'; ?>>English</option>
	<option value="french" <?php if($this->session->userdata('site_lang') == 'french') echo 'selected="selected"'; ?>>French</option>
	<option value="german" <?php if($this->session->userdata('site_lang') == 'german') echo 'selected="selected"'; ?>>German</option>
</select>
In this tutorial has described the language switcher guide of CodeIgniter application. If you have any questions about this tutorial, please let us know by leaving a comment below.
Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

0 Comments

Leave a Reply