This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Enable Multi Language Capability in CodeIgniter

Updated on  17th January

3 Min Read
Reading Time: 3 minutes

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. 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.

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 Host CodeIgniter On Cloud

Set up Language Files

Within the language file, information_lang.php, add every line of text to a $lang array in the following format:

For instance, the entry for the text line, “Type message in German” in the file in the German language folder would look like:

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:

Next to use the hooks, add the following line to the application/config/config.php file.

Once the hooks are enabled, open application/config/hooks.php file and define a hook through the following code:

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:

This will load the appropriate language file. Now you could easily fetch individual text lines through the following code

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.

Next, create LanguageSwitcher.php controller for actually implementing the language switch.Add the following code to it:

Now replace the code in application/hooks/LanguageLoader.php file with the following code:

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:

You might also like: How To Create A REST API In Codeigniter With Basic Authentication

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.

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at owais.alam@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

THERE’S MORE TO READ.