In this tutorial, we will learn how to use CodeIgniter’s Language Class. Using this class we can create application in any languages of our choice. CodeIgniter Language class provides various functions which will help us to retrieve various language files and the line of text present in the file. This class is basically used for the purpose of multi-language internationalization.

If you want to know more about CodeIgniter Session, you can follow our blog posts Session In CodeIgniter and CodeIgniter Simple Login With Sessions.

We will use CodeIgniter Session and CodeIgniter Language class in building the contact form through which user can view form on his choice of language. In the example given below I’ve used English and French languages.


Watch the live demo or download code from the link given below

Extract the downloaded files, save it in your local server and run it using the path given below:

http://localhost/ci_language/

Steps To Use CodeIgniter Language Class Example:

Step 1: In your CodeIgniter system folder open ‘applicationlanguage‘ folder and create two folder named as: ‘english‘ and ‘french‘. Here you can create more folders for multiple-languages.

Step 2: Now, inside english folder create a language file named as: contact_form_english_lang.php. Similarly inside french folder create a language file named as:contact_form_french_lang.php

As our language file is ready. We will load these files in controller and retrieve their text to use it in our contact form.

Step 3: Create a controller  ci_language_tutorial.php which by default loads english language file and displays contact form in english language.

Step 4: Create a View contact_form.php which displays contact form in language selected by user.

Step 5:  Create style.css file for styling html elements.

Note: Language file must contain _lang.php as file extension.


Language file:  contact_form_english_lang.php

<?php
$lang['title'] = 'CodeIgniter Language';
$lang['select_lang'] = 'Select Language ';
$lang['name'] = 'Name';
$lang['message'] = 'Message';
$lang['name_placeholder_value'] = 'Please write your name';
$lang['message_placeholder_value'] = 'Please write your message here...';
$lang['message_sent'] = 'Thank you! Your message has been sent.';
$lang['send'] = 'Send';
?>

Language file:  contact_form_french_lang.php

<?php
$lang['title'] = 'CodeIgniter Langue';
$lang['select_lang'] = 'Selectionnez la langue';
$lang['name'] = 'Votre Nom';
$lang['message'] = 'Message';
$lang['name_placeholder_value'] = 'Se il vous plait ecrivez votre nom';
$lang['message_placeholder_value'] = 'Se il vous plait ecrire votre message ici...';
$lang['message_sent'] = 'Merci! Votre message a ete envoye.';
$lang['send'] = 'Envoyer';
?>

Controller:  ci_language_tutorial.php

When a user changes the language option from dropdown box, selected language file gets loaded in controller and their values get stored in session too. Then these stored values are passed to the view page where contact form is loaded based on selected language.

<?php

ob_start();
session_start();

class CI_Language_Tutorial extends CI_Controller {

public function __construct() {
parent::__construct();
}

// Create sessions for default view page
public function index() {

// Destroy session if already set
$this->session->sess_destroy();

// Load default language
$this->lang->load('contact_form_english', 'english');

// Create session array and store default language values in array
$sess_data = array();
$sess_data['default'] = 'english';
$sess_data['title'] = $this->lang->line('title');
$sess_data['select_lang'] = $this->lang->line('select_lang');
$sess_data['name'] = $this->lang->line('name');
$sess_data['message'] = $this->lang->line('message');
$sess_data['name_placeholder_value'] = $this->lang->line('name_placeholder_value');
$sess_data['message_placeholder_value'] = $this->lang->line('message_placeholder_value');
$sess_data['success_message'] = $this->lang->line('message_sent');
$sess_data['send'] = $this->lang->line('send');

// Set values in session
$this->session->set_userdata('session_data', $sess_data);

// Retrieve session values
$set_data = $this->session->all_userdata();

// Send retrieved data to view page
$this->load->view('contact_form', $set_data);
}

// Select language and manage view page
public function select_language() {

// Destroy session data
$this->session->sess_destroy();
$language = $this->input->post('language');
if ($language == 'english') {
$data['selected'] = 'english';
} else {
$data['selected'] = 'french';
}

// Load language according to language selected
$this->lang->load('contact_form_' . $language, $language);
$sess_data['select_lang'] = $this->lang->line('select_lang');
$sess_data['title'] = $this->lang->line('title');
$sess_data['default'] = $language;
$sess_data['name'] = $this->lang->line('name');
$sess_data['message'] = $this->lang->line('message');
$sess_data['name_placeholder_value'] = $this->lang->line('name_placeholder_value');
$sess_data['message_placeholder_value'] = $this->lang->line('message_placeholder_value');
$sess_data['send'] = $this->lang->line('send');
$sess_data['success_message'] = $this->lang->line('message_sent');

// Set session values according to selected language
$this->session->set_userdata('session_data', $sess_data);
$set_data = $this->session->all_userdata();
$this->load->view('contact_form', $set_data);
}

}

?>

Views:  contact_form.php

Show contact form in the language selected by users.



</html>
<html>
<head>
<title>CodeIgniter Language Class</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<script type="text/javascript">
function DoSubmit()
{
var x = "<?php echo $session_data['success_message']; ?>"
alert(x);
}

</script>
</head>
<body>
<?php
echo form_open('ci_language_tutorial/select_language');
$option = array(
'english' => 'english',
'french' => 'french '
);
$onchange = 'id="lang" onChange="this.form.submit()"';
echo form_dropdown('language', $option, $session_data['default'], $onchange);
echo form_close();
?>
<div class="main">
<div id="content">
<h2 id="form_head"><?php echo $session_data['title']; ?></h2><br/>
<hr>
<div id="form_input">
<?php
echo form_open('ci_language_tutorial/');
echo form_label($session_data['name']);
$data = array(
'name' => 'name',
'class' => 'input_box',
'placeholder' => $session_data['name_placeholder_value']
);
echo form_input($data);

echo "<div class='textarea_input'>";
echo form_label($session_data['message']);
$data = array(
'rows' => 10,
'cols' => 50,
'name' => 'message',
'placeholder' => $session_data['message_placeholder_value']
);
echo form_textarea($data);
echo "</div>";
echo '<div id="form_button">';
echo form_submit('submit', $session_data['send'], 'id="submit" onclick="DoSubmit()"');
echo '</div>';
echo form_close();
?>
</div>
</div>
</body>
</html>


CSS: style.css

For Styling HTML Elements.

body {
font-family: 'Raleway', sans-serif;
}
.main{
width: 1015px;
position: absolute;
top: 10%;
left: 20%;
}
#form_head{
text-align: center;
background-color: #FEFFED;
height: 66px;
margin: 0 0 -29px 0;
padding-top: 35px;
border-radius: 8px 8px 0 0;
color: rgb(97, 94, 94);
}
#content {
position: absolute;
width: 481px;
height: 540px;
border: 2px solid gray;
border-radius: 10px;
margin-top: -40px;
margin-left: -60px;
}
#form_input{
margin-left: 30px;
margin-top: 36px;
}
label{
margin-right: 6px;
padding-bottom: 10px;
}
#form_button{
padding: 0 0px 15px 0px;
position: absolute;
bottom: 0px;
width: 481px;
background-color: #FEFFED;
border-radius: 0px 0px 8px 8px;
border-top: 1px solid #9A9A9A;
left: 0px;
}
#submit{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 300px;
border-radius: 5px;
padding: 10px 0;
outline: none;
margin-top: 20px;
margin-left: 18%;
}
.submit:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
.dropdown_box{
font-family: 'Raleway', sans-serif;
font-size: 16px;
height:40px;
width:422px;
border-radius:3px;
background-color:#FEFFED;
}
.input_box{
font-family: 'Raleway', sans-serif;
font-size: 16px;
height:40px;
width:422px;
padding:5px;
border-radius:3px;
background-color:#FEFFED;
margin-top: 5px;
border:1px solid #A9A9A9;
}
textarea{
background-color:#FEFFED;
font-family: 'Raleway', sans-serif;
font-size: 16px;
margin-top: 5px;
padding:5px;
}
#lang{
font-family: 'Raleway', sans-serif;
font-size: 16px;
height:25px;
width:100px;
}

Conclusion:

Hope you got the concept of using CodeIgniter Language Class. Hope you liked it. Keep reading our blog posts. 🙂