Here’s a step-by-step guide to adding multiple languages in CodeIgniter 4, with details on configuring, loading language files, and switching languages in the application.
Step 1: Set Up the Directory Structure for Language Files
In CodeIgniter 4, language files are stored in the app/Language
directory. To add multiple languages, create a subdirectory for each language using the ISO language codes (e.g., en
for English, fr
for French, etc.).
For instance, you might have:
app/
└── Language/
├── en/
│ └── app.php
└── fr/
└── app.php
Step 2: Create Language Files
Inside each language directory, create a PHP file that will store translations for specific strings. For this example, create app.php
in both en
and fr
folders.
app/Language/en/app.php
return [
'welcome_message' => 'Welcome to our website!',
'contact' => 'Contact Us',
'about' => 'About Us',
];
app/Language/fr/app.php
return [
'welcome_message' => 'Bienvenue sur notre site!',
'contact' => 'Contactez-nous',
'about' => 'À propos de nous',
];
Step 3: Load Language Files in the Controller
CodeIgniter 4 provides a Language service to load the language files and retrieve specific translations.
In your controller, load the language file using language()->load(), then retrieve translations with lang().
namespace App\Controllers;
use CodeIgniter\Controller;
class HomeController extends Controller
{
public function index()
{
// Load language file for the current language
$language = service('language');
$language->setLocale('en'); // Default to English
$data['welcome_message'] = lang('app.welcome_message');
$data['contact'] = lang('app.contact');
$data['about'] = lang('app.about');
return view('home', $data);
}
}
Step 4: Set Up Language Switching
To allow users to switch languages, create a route and function in the controller that changes the current language. Store the language preference in the session so that it persists across pages.
Example Route in app/Config/Routes.php
$routes->get('language/(:segment)', 'LanguageController::switchLanguage/$1');
LanguageController.php
namespace App\Controllers;
use CodeIgniter\Controller;
class LanguageController extends Controller
{
public function switchLanguage($language)
{
$session = session();
// Set the language in session
$session->set('site_lang', $language);
// Redirect back to the previous page
return redirect()->back();
}
}
Step 5: Set the Locale Based on User Preference
Modify the index function in your main controller to check for the language in the session and set it as the active locale.
namespace App\Controllers;
use CodeIgniter\Controller;
class HomeController extends Controller
{
public function index()
{
$session = session();
$language = service('language');
// Set the locale based on session preference, default to English if not set
$locale = $session->get('site_lang') ?? 'en';
$language->setLocale($locale);
$data['welcome_message'] = lang('app.welcome_message');
$data['contact'] = lang('app.contact');
$data['about'] = lang('app.about');
return view('home', $data);
}
}
Step 6: Display the Language Switcher in Views
In your view files, add links for each language that will trigger the switchLanguage
method in LanguageController
.
Example app/Views/home.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Language Switcher</title>
</head>
<body>
<h1><?= $welcome_message ?></h1>
<p><?= $contact ?></p>
<p><?= $about ?></p>
<a href="<?= site_url('language/en') ?>">English</a> |
<a href="<?= site_url('language/fr') ?>">Français</a>
</body>
</html>
Step 7 : Enable Automatic Language Detection
You can use browser language detection to set the default language, or configure it in
app/Config/App.php
.
public $defaultLocale = 'en'; // Default to English
public $supportedLocales = ['en', 'fr']; // Supported languages
Step 8: Test the Implementation
Start your CodeIgniter application and access the homepage. Use the language switcher links to change between languages. The session will remember the selected language, so when you navigate to other pages, the language should remain consistent.