This page may contain affiliate links or links from our sponsors.
CodeIgniter is an Open Source development framework, which can be used to develop the dynamic application, using PHP. It has a very rich set of hierarchical structures, which will increase the speed of the website and also support security. If you know well PHP coding structure, then CodeIgniter will make your task easier.
CodeIgniter provides a rich set of libraries and helpers. By using this framework, you will save a lot of time, if you are developing a website from scratch. CodeIgniter supports a rich set of security as well as has the ability to prevent various unwanted attacks that take place through websites. One great feature of this adds multi-language in the CodeIgniter dynamic.
CodeIgniter framework, the multi-language library allows building multiple languages with reliability. If you want to display text in many different languages on the CodeIgniter website, then you put different language files in the application/language directory. In this article, I will try to explain how to add multi-language in the CodeIgniter dynamic.
Creating Language files for multi-language in CodeIgniter
At First, you need to create a language file end it with _lang.php, to configure the key/value before we can start using language support. Add a new language on Arabic language and also store language data with key and value and store in $lang array as shown below code.
$lang[‘key’] = ‘val’;
Loading Language file
Before using any type of language in your application, first, you load the file of that particular language to retrieve various data stored in that file.
$this->lang->load(‘filename’, ‘language’);
filename – It is the name parameter of the file you want to load.
Language – It is the language set where you use.
Autoload multi-language in CodeIgniter
Do you load multi-language automatic, you need to add below code.
Path is application folder / config folder / autoload.php file
$autoload [‘ language ‘] = array();
Step by Step Guide How to Add Multi-Language In Codeigniter Dynamic With Example
First, build a controller class in Lang_controller.php file
path is application folder / controller folder / Lang_controller.php file
<?php
class Lang_controller extends CI_Controller {
public function index()
{
//Load form helper
$this->load->helper(‘form’);
//Get the selected language
$language = $this->input->post(‘language’);
//Choose language file according to selected lanaguage
if($language == “french”)
$this->lang->load(‘french_lang’,’french’);
else if($language == “german”)
$this->lang->load(‘german_lang’,’german’);
else
$this->lang->load(‘english_lang’,’english’);
//Fetch the message from language file.
$data[‘msg’] = $this->lang->line(‘msg’);
$data[‘language’] = $language;
//Load the view file
$this->load->view(‘lang_view’,$data);
}
}
?>
Add a visual file with name lang_view.php and store in this location application folder /views folder /lang_view.php file
This code add in HTML tags
<?php
echo form_open(‘/lang’);
?>
<select name=”language” onchange=”javascript:this.form.submit();”>
<?php
$lang =
array(‘english’=>”English”,’french’=>”French”,’german’=>”German”);
foreach($lang as $key=>$val)
{
if($key == $language)
echo “<option value=’”.$key.”‘
selected>”.$val.”</option>”;
else
echo “<option value=’”.$key.”‘>”.$val.”</option>”;
}
?>
</select>
<br>
<?php
form_close();
echo $msg;
?>
Add folders with name English in application folder/language folder.
<?php
$lang[‘msg’] = “Test Language.”;
?>
ADD file with name eng_lang.php and at end add route parameter in routes.php file.
$route [‘lang’] = “Lang_controller”;
Run code with example == http://testsite.com/index.php/lang
We hope this article helped you to understand what is CTE SQL and how to use it. You may also want to see How To Add Multi-Language In CodeIgniter Dynamic.