Gone are the days when English was the only language for the Internet. these days, users expect web apps to speak their language. In practical terms, this means that the UI of the web app is in the native language of the visitor. Fortunately, all full stack PHP frameworks allow developers to create multilingual interfaces for PHP applications without rewriting code for every language.
In this tutorial, I will discuss how you could easily develop multilingual PHP applications using CodeIgniter on any hosting for PHP. I will also cover several ideas for customizing the core functionality.
Specify Default Language and Language Options
Go to application/config folder and open config.php. Add the following line to specify the website’s default language.
$config['language'] = 'english';
Now to create the language options, of to application/language folder and create separate subfolders for each language. Next, in each subfolder, create a file with the suffix _lang. For instance, if you wish to offer a file named information.php in English and German languages, use the following structure:
Application→ language → English → information_lang.php
Application→ language → German → information_lang.php
You might also like: How To Install CodeIgniter
Set up Language Files
Within the language file, information_lang.php, add every line of text to a $lang array in the following format:
$lang['language_key'] = 'type your message';
For instance, the entry for the text line, “Type message in German” in the file in the German language folder would look like:
$lang['welcome_message'] = 'Type message in German';
Load Language File
Once the files are in place, it is time to load the language files.
For this, add the following code to the controller’s _construct() function:
$this->lang->load('information','english');
Next to use the hooks, add the following line to the application/config/config.php file.
$config['enable_hooks'] = TRUE;
Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:
$hook['post_controller_constructor'] = array( 'class' => 'LanguageLoader', 'function' => 'initialize', 'filename' => 'LanguageLoader.php', 'filepath' => 'hooks' );
Go to application/hooks folder and create a file named LanguageLoader.php. Create the LanguageLoader class in the file and add the following code to it:
class LanguageLoader { function initialize() { $ci =& get_instance(); $ci->load->helper('language'); $ci->lang->load('information','english'); }
This will load the appropriate language file. Now you could easily fetch individual text lines through the following code
$this->lang->line('welcome_message');
Supercharged Managed PHP Hosting – Improve Your PHP App Speed by 300%
Switch Language
At this point all the necessary ideas for multilingual web apps are in place. It is time to switch languages. But before this, open the application/config/autoload.php file and load SESSION library and the URL helper.
$autoload['libraries'] = array('session'); $autoload['helper'] = array('url');
Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:
<?php if ( ! defined('BASEPATH')) exit('Direct access allowed'); class LanguageSwitcher extends CI_Controller { public function __construct() { parent::__construct(); } function switchLang($language = "") { $language = ($language != "") ? $language : "english"; $this->session->set_userdata('site_lang', $language); redirect($_SERVER['HTTP_REFERER']); } }
Now replace the code in application/hooks/LanguageLoader.php file with the following code:
<?php class LanguageLoader { function initialize() { $ci =& get_instance(); $ci->load->helper('language'); $siteLang = $ci->session->userdata('site_lang'); if ($siteLang) { $ci->lang->load('information',$siteLang); } else { $ci->lang->load('information','english'); } } }
Notice that I have use the URl for switching language without index.php. Thus I will a .htaccess file in the root directory with the following code:
RewriteEngine on RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
You might also like: How To Create A REST API In Codeigniter With Basic Authentication
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
Wrapping Up
As you could see, creating a multilingual website in CodeIgniter is simple enough. If you need help in implementing the idea in your CodeIgniter project, just drop a line and I will get back to you.
Owais Khan
Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.