How to Create a Multilanguage Website in Codeigniter
How to Create a Multilanguage. Creating a multilingual website in CodeIgniter involves several steps that need to be followed carefully. Firstly, it’s essential to define the language files for all the languages that the website will support. These language files should contain the translation of all the texts that appear on the website.
Next, the website’s database should support multilingual data storage, allowing for the storage of content in various languages. This is important, especially for websites that allow user-generated content.
After the database has been set up, the website’s configuration file should be updated to reflect the language preferences of the user. This involves detecting the user’s preferred language and loading the corresponding language file.
It’s also crucial to provide language switch functionality, allowing users to switch between different languages. This can be done using a drop-down menu or flags of different countries.
In addition, it’s essential to ensure that the website’s URLs are also translated, making it easier for search engines to index them correctly.
Finally, it’s crucial to thoroughly test the multilingual functionality of the website, ensuring that all the translations are accurate and all the functionality works as expected.
Overall, creating a multilingual website in CodeIgniter requires careful planning and implementation to ensure a seamless user experience across all supported languages.
Multilingual website in codeigniter
To create a basic multilingual application, you need to do following steps.
- Download Codeigniter Latest
- Basic Configurations
- Autoload libraries
- Add English, French, German, Spanish translation language files
- Add a language hook
- Make New Controller
- Create Views
- Start Development server
Download Codeigniter Latest
Now, let’s get started by downloading the latest version of CodeIgniter 3. To do this, go to this link https://codeigniter.com/download and download the fresh new setup. After downloading, extract the setup and place it in your local system’s xampp/htdocs/ directory. You can also change the folder name from “ci_multilanguage” to any name of your choice.
Basic Configurations
Next, we need to configure some basic settings in the application/config/config.php file. To do this, open the file in a text editor by navigating to the application/config/config.php directory.
Once you have opened the file, you need to set the Base URL by adding the following code:
$config['base_url']='http://localhost/ci_multilingual';
Autoload libraries
Open autoload.php in config folder and go to the line add session library.
$autoload['libraries'] = array('session');
Add English, French, German, Spanish translation language files
Open application/language directory. Let us create language files with a welcome message and content key.
Create an English translation file
Create a folder named English in application/language folder. Create a file named main_lang.php with the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$lang['welcome'] = 'Welcome to our website';
$lang['message'] = 'Our mission is to provide professional and highly creative web design
services and other related services to a wide range of potential customers all over the globe.';
Create a Spanish translation file
Create a folder named Spanish in application/language folder. Create a file named main_lang.php with the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$lang['welcome'] = 'Bienvenido a nuestro sitio web';
$lang['message'] = 'Nuestra misión es proporcionar servicios de diseño web profesionales y altamente creativos y otros
servicios relacionados a una amplia gama de clientes potenciales en todo el mundo.';
Create a German translation file
Create a folder named German in application/language folder. Create a file named main_lang.php with the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$lang['welcome'] = 'Willkommen auf unserer Webseite';
$lang['message'] = 'Unsere Mission ist es, einem breiten Spektrum potenzieller Kunden auf der ganzen
Welt professionelle und äußerst kreative Webdesign-Services und damit verbundene Dienstleistungen anzubieten.';
Create a French translation file
Create a folder named French in application/language folder. Create a file named main_lang.php with the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$lang['welcome'] = 'Bienvenue sur notre site';
$lang['message'] = 'Notre mission est de fournir des services de conception Web professionnels et très créatifs,
ainsi que d’autres services connexes, à un large éventail de clients potentiels dans le monde entier.';
Add a language hook
Enable Hook in cofig.php
Open config.php file in config directory. Set value for enable_hooks to TRUE.
$config['enable_hooks'] = TRUE
Define language hook configuration
Open hooks.php file in config directory. Define the new hook.
$hook['post_controller_constructor'] = array(
'class' => 'LanguageLoader',
'function' => 'initialize',
'filename' => 'LanguageLoader.php',
'filepath' => 'hooks'
);
Create a LangaugeLoader class for multilingual website in codeigniter
Create LangaugeLoader.php file in application/hooks directory. A LanguageLoader class is created. Initialize function loads the content based on selected language by user.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class LanguageLoader
{
function initialize() {
$ci =& get_instance();
$ci->load->helper('language');
$siteLang = $ci->session->userdata('site_lang');
if ($siteLang) {
$ci->lang->load('content',$siteLang);
} else {
$ci->lang->load('content','english');
}
}
}
Create Controller
Open Welcome.php file in controllers directory. Remove the existing code and add the code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index()
{
$this->session->set_userdata('site_lang', "english");
$this->load->view('welcome_message');
}
public function switchLang($language = "") {
$this->session->set_userdata('site_lang', $language);
header('Location: http://localhost/ci_multilingual/');
}
}
The index action, session’s set_userdata method sets default_lang to default value to English and loads the view template welcome_message.
Create Views
<div id="container">
<div style ='margin:0 auto; text-align:center'>
<a href="<?php echo base_url("welcome/switchLang/english"); ?>">English</a> |
<a href="<?php echo base_url("welcome/switchLang/french"); ?>">French</a> |
<a href="<?php echo base_url("welcome/switchLang/german"); ?>">German</a> |
<a href="<?php echo base_url("welcome/switchLang/spanish"); ?>">Spanish</a>
</div>
<h1><?php echo $this->lang->line('welcome') ?></h1>
<div id="body">
<p><?php echo $this->lang->line('message') ?></p>
</div>
</div>
Start Development Server
Open your favrioute browser and run this url
http://localhost/ci_multilingual/
You May Also Like

