Multi-Language implementation in CodeIgniter
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.
1 2 3 |
<?php $config['language'] = 'english'; ?> |
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
ⅰ- Eglish
- application/language/english/header_lang.php
- application/language/english/content_lang.php
- application/language/english/footer_lang.php
1 2 3 4 5 6 7 8 9 10 11 |
<?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'; ?> |
1 2 3 |
<?php $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'; ?> |
1 2 3 |
<?php $lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. All Rights Reserved'; ?> |
ⅱ- French
- application/language/french/header_lang.php
- application/language/french/content_lang.php
- application/language/french/footer_lang.php
1 2 3 4 5 6 7 8 9 10 11 |
<?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'; ?> |
1 2 3 |
<?php $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.'; ?> |
1 2 3 |
<?php $lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. Tous droits réservés'; ?> |
ⅲ- German
- application/language/german/header_lang.php
- application/language/german/content_lang.php
- application/language/german/footer_lang.php
1 2 3 4 5 6 7 8 9 10 11 |
<?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'; ?> |
1 2 3 |
<?php $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'; ?> |
1 2 3 |
<?php $lang['text_copyright_footer'] = 'Copyright © TechArise '.date('Y').'. Alle Rechte vorbehalten'; ?> |
ⅳ- Hindi
- application/language/hindi/header_lang.php
- application/language/hindi/content_lang.php
- application/language/hindi/footer_lang.php
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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'] = 'संपर्क करें'; ?> |
1 2 3 |
<?php $lang['text_content'] = 'Lorem Ipsum के कई अंश उपलब्ध हैं, परन्तु अधिकांश लोगों को इंजेक्शन वाले हास्य या कुछ यादृच्छिक शब्दों से कुछ रूप में परिवर्तन हुआ है, जो कुछ भी विश्वसनीय नहीं लगते। यदि आप Lorem Ipsum के एक मार्ग का उपयोग करने जा रहे हैं, तो आपको यह सुनिश्चित करने की आवश्यकता है कि टेक्स्ट के मध्य में छिपे हुए कोई भी शर्मनाक नहीं है। इंटरनेट पर सभी लॉर्म Ipsum जनरेटर के रूप में आवश्यक पूर्वनिर्धारित विखंडन दोहराने के लिए होते हैं, जिससे इंटरनेट पर यह पहला सच्चा जनरेटर बना। यह 200 से अधिक लैटिन शब्दों के एक शब्दकोश का उपयोग करता है, जो कि कुछ बुनियादी वाक्य संरचनाओं के साथ मिलकर, Lorem Ipsum उत्पन्न करता है जो उचित लगता है। उत्पन्न Lorem Ipsum इसलिए हमेशा पुनरावृत्ति, इंजेक्शन हास्य, या गैर-विशिष्ट शब्द आदि से मुक्त है'; ?> |
1 2 3 |
<?php $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.
1 2 3 |
<?php $config['enable_hooks'] = TRUE; ?> |
Step 4: Create Hooks file
create hook file named “MultiLanguageLoader.php” inside “application/hooks folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php /** * @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.
1 2 3 4 5 6 7 8 9 |
<?php // 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
1 2 3 4 5 6 |
<?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php /** * @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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php /** * @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
1 2 3 4 5 6 |
<select class="form-control" onchange="javascript:window.location.href='<?php echo base_url(); ?>multilanguageswitcher/switch/'+this.value;"> <option value="english" <?php if($this->session->userdata('site_lang') == 'english') echo 'selected="selected"'; ?>>English</option> <option value="hindi" <?php if($this->session->userdata('site_lang') == 'hindi') echo 'selected="selected"'; ?>>Hindi</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> |
Step 10: Create a view
Create a view file named “index.php” inside “application/views/ folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title><?php echo $this->lang->line('text_title_header'); ?></title> <!-- Bootstrap core CSS --> <link href="<?php echo HTTP_VENDOR_PATH; ?>bootstrap/css/bootstrap.min.css" rel="stylesheet"> <link type="text/css" rel="stylesheet" media="all" href="<?php echo HTTP_VENDOR_PATH; ?>galleyZoom/fancybox/source/jquery.fancybox.css"/> <!-- Custom styles for this template --> <link href="<?php echo HTTP_CSS_PATH; ?>style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark fixed-top" style="background-color: #3B5998;"> <span> <select class="form-control" onchange="javascript:window.location.href='<?php echo base_url(); ?>multilanguageswitcher/switch/'+this.value;"> <option value="english" <?php if($this->session->userdata('site_lang') == 'english') echo 'selected="selected"'; ?>>English</option> <option value="hindi" <?php if($this->session->userdata('site_lang') == 'hindi') echo 'selected="selected"'; ?>>Hindi</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> </span> <div class="container"> <a class="navbar-brand" href="<?php echo site_url(); ?>"><?php echo $this->lang->line('text_name_header'); ?></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="#"><?php echo $this->lang->line('text_menu_home_header'); ?> <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="#"><?php echo $this->lang->line('text_menu_demo_header'); ?></a> </li> <li class="nav-item"> <a class="nav-link" href="#"><?php echo $this->lang->line('text_menu_tutorials_header'); ?></a> </li> <li class="nav-item"> <a class="nav-link" href="#"><?php echo $this->lang->line('text_menu_contact_header'); ?></a> </li> <li class="nav-item"> <a class="nav-link" href="#"> </a> </li> </ul> </div> </div> </nav> <div class="container"> <div class="row"> <div class="col-lg-12"> <?php echo $this->lang->line('text_content'); ?> </div> </div> </div> <!-- Footer --> <footer class="py-2" style="background-color: #3B5998;"> <div class="container"> <p class="m-0 text-center text-white"><?php echo $this->lang->line('text_copyright_footer'); ?></p> </div> <!-- /.container --> </footer> <script>var url = "<?php echo site_url(); ?>";</script> <!-- Bootstrap core JavaScript --> <script src="<?php echo HTTP_VENDOR_PATH; ?>jquery/jquery.min.js"></script> <script src="<?php echo HTTP_VENDOR_PATH; ?>popper/popper.min.js"></script> <script src="<?php echo HTTP_VENDOR_PATH; ?>bootstrap/js/bootstrap.min.js"></script> </body> </html> |