Multi-language support or internationalization is an important feature of web applications. Most of PHP frameworks comes with multi-language support to add functionality using existing class and functions. CodeIgniter also comes with Language class to support multi-language. The Language class provides functions to retrieve language files and lines of text for purposes of internationalization. So in this tutorial you will learn how to implement multi-language support in your CodeIgniter project.
Step 1: Site Default Language
Open the application/config/config.php file and specify the site’s default language.
Step 2: 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, english, french, german, hindi, etc.).
Language files structure like
1- Eglish
application/language/english/content_lang.php
application/language/english/footer_lang.php
1- French
application/language/french/content_lang.php
application/language/french/footer_lang.php
1- German
application/language/german/content_lang.php
application/language/german/footer_lang.php
1- Hindi
application/language/hindi/content_lang.php
application/language/hindi/footer_lang.php
Step 3: enable hooks
For using the hooks option you need to enable the hooks feature in application/config/config.php file.
Step 4: Create Hooks file
create hook file named “MultiLanguageLoader.php” inside “application/hooks folder.
Step 5: Open application/config/hooks.php file and define a hook.
Step 6: Before starting to implement multilingual features, open the application/config/autoload.php
Step 7: Create a controller file for handing the language switch
Create a controller file named “MultiLanguageSwitcher.php” inside “application/controllers” folder.
Step 8: Create a controller file
Create a controller file named Home.php” inside “application/controllers” folder.
Step 9: Add dropdown your template file
Step 10: Create a view
Create a view file named “index.php” inside “application/views/ folder.
Demo [sociallocker] Download[/sociallocker]
Step 1: Site Default Language
Open the application/config/config.php file and specify the site’s default language.
Step 2: 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, english, french, german, hindi, etc.).
Language files structure like
1- Eglish
- application/language/english/header_lang.php
// title
$lang['text_title_header'] = 'Welcome to Techarise';
$lang['text_name_header'] = 'Techarise';
// Main Menu
$lang['text_menu_home_header'] = 'Home';
$lang['text_menu_demo_header'] = 'Live Demo';
$lang['text_menu_tutorials_header'] = 'Tutorials';
$lang['text_menu_contact_header'] = 'Contact';
?>
$lang['text_content'] = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don`t look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn`t anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc';
?>
$lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. All Rights Reserved';
?>
1- French
- application/language/french/header_lang.php
// title
$lang['text_title_header'] = 'Bienvenue à TechArise';
$lang['text_name_header'] = 'Techarise';
// Main Menu
$lang['text_menu_home_header'] = 'Accueil';
$lang['text_menu_demo_header'] = 'Démonstration en direct';
$lang['text_menu_tutorials_header'] = 'Tutoriels';
$lang['text_menu_contact_header'] = 'Contact';
?>
$lang['text_content'] = 'Il existe de nombreuses variantes de passages de Lorem Ipsum, mais la plupart ont subi une altération sous une forme quelconque, par l`humour injecté, ou des mots aléatoires qui ne semblent même pas légèrement crédibles. Si vous allez utiliser un passage de Lorem Ipsum, vous devez être sûr qu`il n`y a rien d`embarrassant caché au milieu du texte. Tous les générateurs Lorem Ipsum sur Internet ont tendance à répéter des morceaux prédéfinis si nécessaire, ce qui en fait le premier vrai générateur sur Internet. Il utilise un dictionnaire de plus de 200 mots latins, combiné avec une poignée de structures de phrases modèles, pour générer Lorem Ipsum qui semble raisonnable. Le Lorem Ipsum généré est donc toujours exempt de répétition, d`humour injecté ou de mots non-caractéristiques, etc.';
?>
$lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. Tous droits réservés';
?>
1- German
- application/language/german/header_lang.php
// title
$lang['text_title_header'] = 'Willkommen bei TechArise';
$lang['text_name_header'] = 'TechArise';
// Main Menu
$lang['text_menu_home_header'] = 'Zuhause';
$lang['text_menu_demo_header'] = 'Live-Demo';
$lang['text_menu_tutorials_header'] = 'Anleitungen';
$lang['text_menu_contact_header'] = 'Kontakt';
?>
$lang['text_content'] = 'Es gibt viele Variationen von Passagen von Lorem Ipsum, aber die meisten haben Veränderungen in irgendeiner Form erlitten, durch eingespritzten Humor oder randomisierte Wörter, die nicht einmal etwas glaubwürdig aussehen. Wenn Sie eine Passage von Lorem Ipsum verwenden, müssen Sie sicher sein, dass in der Mitte des Textes nichts Peinliches versteckt ist. Alle Lorem Ipsum-Generatoren im Internet tendieren dazu, vordefinierte Chunks nach Bedarf zu wiederholen und machen damit den ersten echten Generator im Internet. Es verwendet ein Wörterbuch von über 200 lateinischen Wörtern, kombiniert mit einer Handvoll von Modellsatzstrukturen, um Lorem Ipsum zu erzeugen, das vernünftig aussieht. Der erzeugte Lorem Ipsum ist daher immer frei von Wiederholungen, eingespritztem Humor oder nicht charakteristischen Wörtern etc';
?>
$lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. Alle Rechte vorbehalten';
?>
1- Hindi
- application/language/hindi/header_lang.php
// title
$lang['text_title_header'] = 'techarise में आपका स्वागत है';
$lang['text_name_header'] = 'TechArise';
// Main Menu
$lang['text_menu_home_header'] = 'घर';
$lang['text_menu_demo_header'] = 'लाइव डेमो';
$lang['text_menu_tutorials_header'] = 'ट्यूटोरियल';
$lang['text_menu_contact_header'] = 'संपर्क करें';
?>
$lang['text_content'] = 'Lorem Ipsum के कई अंश उपलब्ध हैं, परन्तु अधिकांश लोगों को इंजेक्शन वाले हास्य या कुछ यादृच्छिक शब्दों से कुछ रूप में परिवर्तन हुआ है, जो कुछ भी विश्वसनीय नहीं लगते। यदि आप Lorem Ipsum के एक मार्ग का उपयोग करने जा रहे हैं, तो आपको यह सुनिश्चित करने की आवश्यकता है कि टेक्स्ट के मध्य में छिपे हुए कोई भी शर्मनाक नहीं है। इंटरनेट पर सभी लॉर्म Ipsum जनरेटर के रूप में आवश्यक पूर्वनिर्धारित विखंडन दोहराने के लिए होते हैं, जिससे इंटरनेट पर यह पहला सच्चा जनरेटर बना। यह 200 से अधिक लैटिन शब्दों के एक शब्दकोश का उपयोग करता है, जो कि कुछ बुनियादी वाक्य संरचनाओं के साथ मिलकर, Lorem Ipsum उत्पन्न करता है जो उचित लगता है। उत्पन्न Lorem Ipsum इसलिए हमेशा पुनरावृत्ति, इंजेक्शन हास्य, या गैर-विशिष्ट शब्द आदि से मुक्त है';
?>
$lang['text_copyright_footer'] = 'कॉपीराइट © techarise '.date('Y').'. सर्वाधिकार सुरक्षित';
?>
Step 3: enable hooks
For using the hooks option you need to enable the hooks feature in application/config/config.php file.
$config['enable_hooks'] = TRUE;
?>
Step 4: Create Hooks file
create hook file named “MultiLanguageLoader.php” inside “application/hooks folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Contact : CodeIgniter Multi Language Loader
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Multi Language Loader Hook
*/
class MultiLanguageLoader
{
function initialize() {
$ci =& get_instance();
// load language helper
$ci->load->helper('language');
$siteLang = $ci->session->userdata('site_lang');
if ($siteLang) {
// difine all language files
$ci->lang->load('header',$siteLang);
$ci->lang->load('content',$siteLang);
$ci->lang->load('footer',$siteLang);
} else {
// default language files
$ci->lang->load('header','english');
$ci->lang->load('content','english');
$ci->lang->load('footer','english');
}
}
}
?>
Step 5: Open application/config/hooks.php file and define a hook.
// create hook for multi langunage
$hook['post_controller_constructor'] = array(
'class' => 'MultiLanguageLoader',
'function' => 'initialize',
'filename' => 'MultiLanguageLoader.php',
'filepath' => 'hooks'
);
?>
Step 6: Before starting to implement multilingual features, open the application/config/autoload.php
// load libraries
$autoload['libraries'] = array('session');
// load helper
$autoload['helper'] = array('url');
?>
Step 7: Create a controller file for handing the language switch
Create a controller file named “MultiLanguageSwitcher.php” inside “application/controllers” folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Contact : CodeIgniter Multi Language Switcher
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Multi Language Switcher Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class MultiLanguageSwitcher extends CI_Controller
{
public function __construct() {
parent::__construct();
}
// create language Switcher method
function switch($language = "") {
$language = ($language != "") ? $language : "english";
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
}
?>
Step 8: Create a controller file
Create a controller file named Home.php” inside “application/controllers” folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* @package Contact : CodeIgniter Home
*
* @author TechArise Team
*
* @email info@techarise.com
*
* Description of Home Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
}
//
public function index() {
$data = array();
$data['metaDescription'] = 'Multi-Language implementation in CodeIgniter';
$data['metaKeywords'] = 'Multi-Language implementation in CodeIgniter';
$data['title'] = "Multi-Language implementation in CodeIgniter - TECHARISE";
$this->load->view('home/index', $data);
}
}
?>
Step 9: Add dropdown your template file
Step 10: Create a view
Create a view file named “index.php” inside “application/views/ folder.
lang->line('text_title_header'); ?>
lang->line('text_content'); ?>
Demo [sociallocker] Download[/sociallocker]