How To Create CodeIgniter 4 Multiple Language Website

Share this Article
Reading Time: 9 minutes

Inside this article we will see the concept i.e How To Create CodeIgniter 4 Multiple Language Website. Nowadays, creating website in multilingual is very common and also it is very essential to gain more and more traffic on web applications.

By the help of this article we will see the step by step guide to create a CodeIgniter 4 website in different different language.

This article will be very interesting to learn i.e How To Create CodeIgniter 4 Multiple Language Website and easy to implement in your code.

Learn More –

Let’s get started.

What is Website Language Localization?

Simply creating an website in different different languages is termed as Website Language Localization. A website can be created in various technologies. Each technology has their own syntax to deploy web application in into multi language.

If we are talking about wordpress a PHP CMS, there are several plugins available which works for the same.

Creating site in different different languages gain users/viewers/visitors world wide. It ultimately increases site traffic. Nowadays every client wants all about, site should be multi lingual supported.

If you are aware of CodeIgniter 3 then it will help you a little bit to understand about files hierarchy and if suppose you don’t know about that, no issues at all. We will see it from scratch.

Now, let’s see what are the configuration we need to do in CodeIgniter 4 for Multi language setup.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Locale Configuration Settings

Open configuration file – App.php from /app/Config folder.

Search for defaultLocale.

You can see, you should have public $defaultLocale = ‘en’; in App.php file.

This is default locale with application i.e English.

Configuration Steps

Step 1

public $negotiateLocale = false;

Change To

public $negotiateLocale = true;

True value is for enabling auto detection mode for locale into application.

Step 2

public $supportedLocales = ['en'];

Change To

public $supportedLocales = ['en', 'es', 'fr'];

Here, we have passed our site supported locales. You can pass more than this. In this article we will see codeigniter 4 language localization in 3 languages locales as – en(English), es(Spanish), fr(French).

You can find all details about language and it’s locale code.

Add Routes

Open Routes.php from /app/Config folder. Add these routes into it.


$routes->get('/', 'Home::index'); // welcome page of application
$routes->get('lang/{locale}', 'Language::index');


Created a route which using Language Controller and it’s method called index.

Let’s create Controller.

Create Controller

Next we need to create application controller.

Loading url Helper

Open BaseController.php from /app/Controllers folder. Search for $helpers, inside this helpers array simply add this url helper.

protected $helpers = [‘url’];

This url helper will load base_url() and site_url() functions.

Also we have the option to load any helper directly to any controller using helper() function.

Open terminal and run this spark command to create controller.

$ php spark make:controller Language

It will create Language.php file at /app/Controllers folder.

Open Language.php and write this complete code into it.


namespace App\Controllers;

use App\Controllers\BaseController;

class Language extends BaseController
    public function index()
        $session = session();
        $locale = $this->request->getLocale();
        $session->set('lang', $locale);
        $url = base_url();
        return redirect()->to($url);
  • $session = session(); // Creating session service instance variable.
  • $locale = $this->request->getLocale(); // Reading requested locale and storing into $locale variable.
  • $session->remove(‘lang’); // Removing existing saved locale “lang” key from session
  • $session->set(‘lang’, $locale); // Saving requested locale into “lang” session key
  • $url = base_url(); // Getting application base URL
  • redirect()->to($url) // Redirecting to base URL

Parent Controller Settings

In CodeIgniter 4, BaseController.php is the parent controller for every controller file.

We need to add configuration lines to it’s constructor method.

BaseController.php is in /app/Controllers folder.


public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
  parent::initController($request, $response, $logger);
  // Add these lines
  $session = \Config\Services::session();
  $language = \Config\Services::language();


Open Default controller

Default created controller with Installation Setup i.e Home.php

namespace App\Controllers;

class Home extends BaseController
	public function index()
		return view('welcome_message');

Inside this file, we have also a default view called i.e welcome_message.

This view file is inside /app/Views.

Language Localization – Creating Language Files

Language files are those files which stores key value pairs of texts. As we have discussed we are going to create site in 3 different languages.

As per the language, we need to create language folders in /app/Language.

We are going to create 3 folders(en, es, fr) as per the language what we have taken. You can create these folders, as per your selected languages. You can name these folders as per the code of locale. Also these codes we will use into URL redirection in few minutes.

/app/Language/en, /app/Language/es, /app/Language/fr

Next, we need to create language file which stores languages keys and respective texts. Creating Text.php into all three folders. This is user defined file name. It will use to call the keys.

/app/Language/en/Text.php – English Language


return [
    'page_title' => 'Welcome Page',
    'welcome_message' => 'Hi, Welcome to this page',
    'author_information' => 'My name is Sanjay. This blog is mine and we created this post for you to learn.',

/app/Language/es/Text.php – Spanish Language


return [
    'page_title' => 'Pagina de bienvenida',
    'welcome_message' => 'Hola bienvenido a esta pagina',
    'author_information' => 'Mi nombre es Sanjay. Este blog es mío y creamos esta publicación para que aprendas.',

/app/Language/fr/Text.php – French Language


return [
    'page_title' => "Page d'accueil",
    'welcome_message' => "Bonjour, Bienvenue sur cette page",
    'author_information' => "Mon nom est Sanjay. Ce blog est le mien et nous avons créé cet article pour que vous puissiez l'apprendre.",

Successfully, we have configured language files in their respective folders.

Create View Layout Files

View file, where we will put language change option for user. According to changed language we will pick the key from created language file and render into the output screen.

Views are created inside /app/Views folder.

Open welcome_message.php

<a class="dropdown-item" href="<?= site_url('lang/en'); ?>">English</a>
<a class="dropdown-item" href="<?= site_url('lang/es'); ?>">Spanish</a>
<a class="dropdown-item" href="<?= site_url('lang/fr'); ?>">French</a>

<h1><?= lang("Text.page_title") ?></h1>
<h2><?= lang("Text.welcome_message") ?></h2>
<h3><?= lang("Text.author_information") ?></h3>

site_url(‘lang/en’) – It will convert site locale to en(English)

site_url(‘lang/es’) – It will convert site locale to es(Spanish)

site_url(‘lang/fr’) – It will convert site locale to fr(French)

After getting site locale, this code lang(“Text.page_title”) read Text.php file of respective local folder and pick the key page_title.

CodeIgniter 4 Multi language conversion demo
CodeIgniter 4 Multi language conversion demo

We hope this article helped you to learn about CodeIgniter 4 Language Localization in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

10 thoughts on “How To Create CodeIgniter 4 Multiple Language Website”

  1. Hello .. I followed the steps in your tutorial, but I can’t get the translation done .. I think it’s because of how I’m handling the routes in groups. I’m doing the translations to Spanish and English of the backend (dashboard). If you can guide me in advance, I appreciate it.

      • It doesnt change the language.
        I got this on the routes.php files:

        $routes->get(‘/lang/{locale}’, ‘SiteLanguage::index’);

        $routes->group(‘auth’, [‘namespace’ => ‘App\Controllers\Auth’], function($routes){
        $routes->get(‘login’, ‘LoginController::loginForm’,[‘as’ => ‘login’]);
        $routes->post(‘signin’, ‘LoginController::signin’, [‘as’ => ‘signin’]);
        $routes->get(‘logout’, ‘LoginController::signout’, [‘as’ => ‘signout’]);

        $routes->group(‘/’, [‘namespace’ => ‘App\Controllers\Frontend’], function($routes){
        $routes->get(”, ‘PagesController::index’, [‘as’=> ‘home’]);

        $routes->group(‘dashboard’, [‘namespace’ => ‘App\Controllers\Backend’, ‘filter’ => ‘auth:Admin’], function($routes){

        $routes->get(”, ‘DashboardController::index’,[‘as’=>’dashboard’]);

        $routes->group(‘languages’, function($routes){
        $routes->get (”, ‘LanguageController::index’, [‘as’=> ‘languages’]);
        $routes->get (‘create’, ‘LanguageController::create’, [‘as’=> ‘create’]);
        $routes->post(‘store’, ‘LanguageController::store’, [‘as’=> ‘store’]);
        $routes->get (‘edit/(:num)’, ‘LanguageController::edit/$1’, [‘as’=> ‘edit’]);
        $routes->post(‘update’, ‘LanguageController::update’, [‘as’=> ‘update’]);
        $routes->get (‘delete/(:num)’, ‘LanguageController::delete/$1’, [‘as’=> ‘delete’]);

        $routes->group(‘usersgroups’, function($routes)
        $routes->get (”, ‘UserGroupController::index’, [‘as’=> ‘usersgroups’]);
        $routes->get (‘create’, ‘UserGroupController::create’, [‘as’=> ‘creategroup’]);
        $routes->post(‘store’, ‘UserGroupController::store’, [‘as’=> ‘storegroup’]);
        $routes->get (‘edit/(:num)’, ‘UserGroupController::edit/$1’, [‘as’=> ‘editgroup’]);
        $routes->post(‘update’, ‘UserGroupController::update’, [‘as’=> ‘updategroup’]);
        $routes->get (‘delete/(:num)’, ‘UserGroupController::delete/$1’, [‘as’=> ‘deletegroup’]);

        $routes->group(‘users’, function($routes)
        $routes->get (”, ‘UserController::index’, [‘as’=> ‘users’]);
        $routes->get (‘create’, ‘UserController::create’, [‘as’=> ‘createuser’]);
        $routes->post(‘store’, ‘UserController::store’, [‘as’=> ‘storeuser’]);
        $routes->get (‘edit/(:num)’, ‘UserController::edit/$1’, [‘as’=> ‘edituser’]);
        $routes->post(‘update’, ‘UserController::update’, [‘as’=> ‘updateuser’]);
        $routes->get (‘delete/(:num)’, ‘UserController::delete/$1’, [‘as’=> ‘deleteuser’]);

        $routes->group(‘siteadmin’, function($routes)
        $routes->get(”, ‘SiteadminController::index’, [‘as’=> ‘siteadmin’]);

        $routes->group(‘posts’, function($routes)
        $routes->get(”, ‘PostController::index’, [‘as’=> ‘posts’]);

        $routes->group(‘categories’, function($routes)
        $routes->get(”, ‘CategoryController::index’, [‘as’=> ‘categories’]);

        $routes->group(‘services’, function($routes)
        $routes->get(”, ‘ServiceController::index’, [‘as’=> ‘services’]);


        • Everytime I click to change the language it redirects to the home page.
          What am I doing wrong?

          • Hi, I think this is due to using Auth. You have implemented auth which i think checking session and redirecting to home page if there is no value.

  2. You said:
    “Hi, I think this is due to using Auth. You have implemented auth which i think checking session and redirecting to home page if there is no value.”

    auth is a filter that I use to access or not to the dashboard; How can I implement your multilanguage solution in this case? Thanks in advanced.

    • Let me tell you what I did,

      in LoginController.php :

      ‘user_id’ => $user->id,
      ‘username’ => $user->username,
      ‘real_name’ => $user->name . ‘ ‘ . $user->last_name,
      ‘group’ => $user->groupname,
      ‘is_logged’ => true,
      ‘lang’ => $this->request->getLocale(), // $this->request->getLocale()” then when I login it sends me to homepage, but I got back to dashboard it takes the language, to solve this I changed in the SiteLanguage.php Controller:

      ” return redirect()->route(‘dashboard’); ” and it works but when I’m in Form, for example, a form to create a user, and want to change the language, It get me out to the dashboard, I know that is for the redirection to ‘dasboard’.

      Is there a way to change the language while in a form o any section in the dashboard without to go out it? I mean to stay in that section.

      thanks in advanced

  3. Hi Sanjay, do you know how to implement a tree directory inside the language folder? e.g.


    siteA and siteB are 2 different directories, and I want to load header.php depending on the route

    I know I can do


    return [
    ‘SiteA.title’ => ‘Hello A!’,
    ‘SiteB.title’ => ‘Hello B!’

    But I want to organize the directories better because I have an APP that runs like 10 sites, and each site has its translations in each language.

Comments are closed.

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299