Codeigniter Multi-Language Website

Introduction

This post is about Codeigniter multi-language website, where user will have options to choose his/her own language for reading the text or information on the website. In this Codeigniter multi-language website, I will show how to change the text of the website in different locales or languages. I will use five different languages to make the website internationalization (i18n). You may add more language or locale to display information on other languages.

The Codeigniter documentation provides some idea on how to use language file in Codeigniter application.

Related Post:

Prerequisites

Apache 2.4, PHP 7.3.5 – 7.4.3, Codeigniter 3.1.10 – 3.1.11

Project Directory

You need to first create the project’s root directory in order to implement the Codeigniter multi-language website.

It’s assumed that you have setup Apache, PHP and Codeigniter in Windows system.

Next I will create a project root directory called codeIgniter-multi-language-site under the Apache server’s htdocs folder.

Now move all the directories and files from Codeigniter framework into codeIgniter-multi-language-site directory.

I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project root directory.

Autoload Configuration

You need some configurations, such as, auto-loading for helpers to avoid loading every time I need to use.

Modify application/config/autoload.php file for auto-loading url and form.

This one time auto-loading gives flexibility to uniformly use the helpers anywhere throughout the application without loading repeatedly.

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

Language Files

You want to display information on your website in different languages. Therefore, you need to create language specific files from where the information will be loaded for a particular language when chosen by end users.

I will use here five different languages to make our website internationalization.

I am creating language file for each of the five different languages. The file must end with _lang.php, though while loading the language file you will not use this suffix.

Bengali

The Bengali language file locale_lang.php is created under application/language/bengali folder.

<?php

$lang['msg']= 'কোডইগনিটার এর আন্তর্জাতিকীকরণ উদাহরণ';
$lang['welcome']= 'বাংলা ভাষায় স্বাগতম';
$lang['chooseLang']= 'আপনার ভাষা নির্বাচন করুন';
$lang['copyright']= 'গ্রহস্বত্ব';
$lang['year']= '২০১৯';

Hindi

The Hindi language file locale_lang.php is created under application/language/hindi folder.

<?php

$lang['msg']= 'कोडइग्निटार में अंतर्राष्ट्रीयकरण उदाहरण';
$lang['welcome']= 'हिंदी भाषा में आपका स्वागत है';
$lang['chooseLang']= 'अपनी भाषा चुनिए';
$lang['copyright']= 'सत्त्वाधिकार';
$lang['year']= '२०१९';

English

The English language file locale_lang.php is created under application/language/english folder.

<?php

$lang['msg']= 'Internationalization Example in Codeigniter';
$lang['welcome']= 'Welcome to English Language';
$lang['chooseLang']= 'Choose your language';
$lang['copyright']= 'Copyright';
$lang['year']= '2019';

Dutch

The Dutch language file locale_lang.php is created under application/language/dutch folder.

<?php

$lang['msg']= 'Internationalisering Voorbeeld in Codeigniter';
$lang['welcome']= 'Welkom bij Nederlandse Taal';
$lang['chooseLang']= 'Kies uw taal';
$lang['copyright']= 'Auteursrecht';
$lang['year']= '2019';

French

The French language file locale_lang.php is created under application/language/french folder.

<?php

$lang['msg']= 'Internationalisation exemple dans Codeigniter';
$lang['welcome']= 'Bienvenue à la langue française';
$lang['chooseLang']= 'Choisissez votre langue';
$lang['copyright']= 'Droit d\'auteur';
$lang['year']= '2019';

Notice I have given the same file names and same keys with same values in different languages.

If you want, you may add more language to your application.

Controller Class

Create a controller file Multilingual.php under application/controllers with the following source code.

The below controller class handles request and response for clients.

The controller class has only one method called index() that first checks if there is any input language in the request otherwise it will set English as the default language and display the page in English.

If the language is found in request then accordingly language file is loaded.

Finally I sent the language to the view file.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Author: https://roytuts.com
*/

class Multilingual extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
	}

	public function index() {
		$language = '';
		if ($this->input->post('locale')) {
			$language = $this->input->post('locale');
			if($language == 'bn') {
				$this->lang->load('locale', 'bengali');
			} else if($language == 'hi') {
				$this->lang->load('locale', 'hindi');
			} else if($language == 'nl') {
				$this->lang->load('locale', 'dutch');
			} else if($language == 'fr') {
				$this->lang->load('locale', 'french');
			} else {
				$this->lang->load('locale', 'english');
			}
		} else {
			$this->lang->load('locale', 'english');
		}
		
		$data['language'] = $language == '' ? 'en' : $language;
		
		$this->load->view('multilingual', $data);
	}
}

View File

The view file is responsible for displaying information on the web page.

So I will create a view file multilingual.php under application/views folder.

In the view file we have a form that contains dropdown or combo box values for selecting a language.

When user chooses a language then information on the site is display in that particular language. By default information gets displayed in English language.

The form gets submitted on JavaScript’s onchange event and immediately information gets updated on the page on the chosen language.

Notice how do I access a particular line from the language file and display on the view file.

<?php
	defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Welcome to CodeIgniter Multilingual Site</title>

	<style type="text/css">

		::selection { background-color: #E13300; color: white; }
		::-moz-selection { background-color: #E13300; color: white; }

		body {
			background-color: #fff;
			margin: 40px;
			font: 13px/20px normal Helvetica, Arial, sans-serif;
			color: #4F5155;
		}

		h1 {
			color: #444;
			background-color: transparent;
			border-bottom: 1px solid #D0D0D0;
			font-size: 19px;
			font-weight: normal;
			margin: 0 0 14px 0;
			padding: 14px 15px 10px 15px;
		}

		#body {
			margin: 0 15px 0 15px;
		}

		p.footer {
			text-align: right;
			font-size: 11px;
			border-top: 1px solid #D0D0D0;
			line-height: 32px;
			padding: 0 10px 0 10px;
			margin: 20px 0 0 0;
		}

		#container {
			margin: 10px;
			border: 1px solid #D0D0D0;
			box-shadow: 0 0 8px #D0D0D0;
		}
	</style>
</head>
<body>

<div id="container">
	<h1><?php echo $this->lang->line('msg'); ?></h1>

	<div id="body">
		<fieldset>
			<legend>
				<?php echo $this->lang->line('msg'); ?>
			</legend>
			<p>
				<?php
					echo form_open($this->uri->uri_string());
				?>
				<label><?php echo $this->lang->line('chooseLang'); ?></label>
				<select	id="locale" name="locale" onchange = "javascript:this.form.submit();">
					<option value="bn" <?php if($language == 'bn'){ echo 'selected';}?>>বাংলা</option>
					<option value="hi" <?php if($language == 'hi'){ echo 'selected';}?>>हिंदी</option>
					<option value="en" <?php if($language == 'en' || $language == ''){ echo 'selected';}?>>English</option>
					<option value="fr" <?php if($language == 'fr'){ echo 'selected';}?>>Français</option>
					<option value="nl" <?php if($language == 'nl'){ echo 'selected';}?>>Nederlands</option>
				</select>
				<?php
					echo form_close();
				?>
			</p>
		</fieldset>
		<div style="clear: both"></div>
		<div>
			<?php echo $this->lang->line('copyright'); ?> © <?php echo $this->lang->line('year'); ?>
		</div>
	</div>

	<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

Configuring Route

You need to tell the application what is your default controller class that will be loaded by default on index URL hit.

$route['default_controller'] = 'multilingual';

Testing the Application

English Language

Now hit the URL http://localhost/codeIgniter-multi-language-site/  in the browser to run the application. You will get similar page on the browser as shown in the below image.

codeigniter multi-language website

Bengali Language

When Bengali language is chosen from the dropdown.

codeigniter multi-language website

Hindi Language

When you choose Hindi language.

codeigniter multi-language website

Dutch Language

When you choose Dutch language.

codeigniter multi language site

French Language

When user chooses French language.

codeigniter multilingual website

That’s all how to work with multi language website in CodeIgniter framework.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *