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, we will show how to change the text of the website in different locales. We will use five different languages to make our website internationalization. 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.

Prerequisites

Apache 2.4, PHP 7.3.5, Codeigniter 3.1.10

Example with Source Code

Creating Project Directory

We need to first create our root project directory in order to implement the Codeigniter multi-language website.

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

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

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

We may not mention the project root directory in subsequent sections and we will assume that we are talking with respect to the project root directory.

Autoload Configuration

We need some configurations, such as, auto-loading for helpers to avoid loading every time we 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');

Creating Language Files

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

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

We are creating language file for each of the five different languages. The file must end with _lang.php, though while loading the language file we 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 we have the same file names and same keys with same values in different languages.

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

Creating 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 we sent the language to the view file.

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

/**
* Author: https://www.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);
	}
}

Creating View File

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

So we 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

We need to tell the application what is our 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://[::1]/codeIgniter-3.1.10-multi-language-site/index.php  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

Source Code

You can download source code.

Thanks for reading.

Leave a Reply

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