This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Join our live AMA on the Future of Page Builders with Brizy's CEO! Register Now →

Enable Multi Language Capability in CodeIgniter

Updated on June 3, 2021

3 Min Read

Gone are the days when English was the only language for the Internet. these days, users expect web apps to speak their language. In practical terms, this means that the UI of the web app is in the native language of the visitor. Fortunately, all full stack PHP frameworks allow developers to create multilingual interfaces for PHP applications without rewriting code for every language.

In this tutorial, I will discuss how you could easily develop multilingual PHP applications using CodeIgniter on any hosting for PHP. I will also cover several ideas for customizing the core functionality.

Specify Default Language and Language Options

Go to application/config folder and open config.php. Add the following line to specify the website’s default language.

$config['language']  = 'english';

Now to create the language options, of to application/language folder and create separate subfolders for each language. Next, in each subfolder, create a file with the suffix _lang. For instance, if you wish to offer a file named information.php in English and German languages, use the following structure:

Application→ language → English → information_lang.php

Application→ language → German → information_lang.php

You might also like: How To Install CodeIgniter

Set up Language Files

Within the language file, information_lang.php, add every line of text to a $lang array in the following format:

$lang['language_key'] = 'type your message';

For instance, the entry for the text line, “Type message in German” in the file in the German language folder would look like:

$lang['welcome_message'] = 'Type message in German';

Load Language File

Once the files are in place, it is time to load the language files.

For this, add the following code to the controller’s _construct() function:

$this->lang->load('information','english');

Next to use the hooks, add the following line to the application/config/config.php file.

$config['enable_hooks'] = TRUE;

Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:

$hook['post_controller_constructor'] = array(

   'class'    => 'LanguageLoader',

   'function' => 'initialize',

   'filename' => 'LanguageLoader.php',

   'filepath' => 'hooks'

);

Go to application/hooks folder and create a file named LanguageLoader.php. Create the LanguageLoader class in the file and add the following code to it:

class LanguageLoader

{

   function initialize() {

       $ci =& get_instance();

       $ci->load->helper('language');

       $ci->lang->load('information','english');

   }

This will load the appropriate language file. Now you could easily fetch individual text lines through the following code

$this->lang->line('welcome_message');

Supercharged Managed PHP Hosting – Improve Your PHP App Speed by 300%

Switch Language

At this point all the necessary ideas for multilingual web apps are in place. It is time to switch languages. But before this, open the application/config/autoload.php file and load SESSION library and the URL helper.

$autoload['libraries'] = array('session');

$autoload['helper'] = array('url');

Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:

<?php if ( ! defined('BASEPATH')) exit('Direct access allowed');

class LanguageSwitcher 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']);

   }

}

Now replace the code in application/hooks/LanguageLoader.php file with the following code:

<?php

class LanguageLoader

{

   function initialize() {

       $ci =& get_instance();

       $ci->load->helper('language');

       $siteLang = $ci->session->userdata('site_lang');

       if ($siteLang) {

           $ci->lang->load('information',$siteLang);

       } else {

           $ci->lang->load('information','english');

       }

   }

}

Notice that I have use the URl for switching language without index.php. Thus I will a .htaccess file in the root directory with the following code:

RewriteEngine on

RewriteCond $1 !^(index\.php)

RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]

You might also like: How To Create A REST API In Codeigniter With Basic Authentication

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Wrapping Up

As you could see, creating a multilingual website in CodeIgniter is simple enough. If you need help in implementing the idea in your CodeIgniter project, just drop a line and I will get back to you.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now