The best way to make CodeIgniter website multi-language -- calling from lang arrays depending on lang session?

The best way to make CodeIgniter website multi-language -- calling from lang arrays depending on lang session?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Multi-Language Support in CodeIgniter Without a Database

CodeIgniter offers a built-in localization engine that handles language switching, but the default implementation often feels like it requires extra steps to get right. The core question is how to map user session data directly to specific language arrays within your view files without relying on a database for storage. The standard approach involves creating distinct PHP array files and configuring the framework to load them dynamically based on the active session value.

To start, you need to define your language files. By convention, these should live in the application/language directory. Inside this folder, create subdirectories for each supported locale, such as english and polish. Within those directories, place PHP array files that map keys to translated strings. For instance, you might have an index_lang.php file inside the english folder containing:

return [
    'welcome_message' => 'Welcome to our site',
    'button_submit' => 'Submit',
];

The corresponding polish folder would contain index_lang.php with the translated values. This structure keeps your translations organized and separate from your logic.

Once the files are in place, you must tell CodeIgniter how to load them automatically. The framework relies on a configuration file located at application/config/languages.php. You should define the language path and specify which languages are valid. A typical configuration looks like this:

<?php
return [
    'load' => TRUE,
    'path' => APPPATH . 'language',
    'default_language' => 'english',
    'supported_languages' => ['english', 'polish'],
];

Setting 'load' to TRUE ensures that CodeIgniter attempts to load the language file automatically whenever a view is rendered. The framework will look for a file named {lang}_{file_name}.php. If your session variable session('lang') returns 'english', it will seek english_index_lang.php. If you prefer manual control over when files are loaded, you can disable automatic loading and call $this->lang->load() explicitly in your controllers or models.

The actual usage in your views becomes straightforward once the configuration is set. Instead of hardcoding strings, you reference them using the lang helper function. In a Blade template or standard PHP view, you would write:

<?= lang('welcome_message'); ?>

This call resolves to the value stored in the currently active language file. To switch languages dynamically, update the session data and reload the page. You can trigger this via a dropdown menu or URL parameters. For example, clicking a link like /polish might update the session and redirect:

session(['lang' => 'polish']);
redirect(current_url());

After changing the session value, subsequent view renders will pull from the polish_index_lang.php file automatically. This mechanism avoids database lookups entirely, keeping the system lightweight and fast.

One potential pitfall is ensuring the file naming convention matches exactly what the loader expects. The loader looks for {lang}_{filename}.php. If your file is named index_lang.php, it must remain consistent across all language folders. Another consideration is pluralization; static arrays cannot handle grammatical number changes automatically. You may need to implement logic in your controller to select singular or plural keys based on the count of items before passing the string to the view.

If you find yourself managing complex translation workflows, maintaining multiple versions of these files can become cumbersome. In such cases, migrating to a dedicated internationalization library or exploring tools that support JSON-based translations with fallback mechanisms might be worth considering. However, for straightforward projects requiring just two languages, the native array approach remains efficient and requires no external dependencies.

For developers looking to streamline their localization setup further or integrate more robust translation management tools, resources like https://laravelcompany.com offer insights into modern practices that can complement traditional CodeIgniter setups.

Our network