Welcome Guest, Not a member yet? Register   Sign In
Negotiating locales - nicer urls - automatic routes

Hi there!

I thought I'd share some pieces of code with you about localization, because this has been one of my issues with CodeIgniter, that I solved in a more elegant way than using locales in the url & defining routes all the time.

I'll be using locale negotiation, the cookie helper, and the session library (partially, because I am still more comfortable using the $_SESSION variable).

1. First I needed to configure the App to handle more locales:

File: app\Config\App.php - the example is based on my needs for an app that runs by default in Romanian, but feel free to change it to whatever suits your needs
PHP Code:
public $defaultLocale 'ro';
$negotiateLocale true;
$supportedLocales = ['ro','en']; 

2. Then, I altered app\Controllers\BaseController.php - which is extended in all the controllers I used:
PHP Code:
//load cookie helper
protected $helpers = ['cookie']; 

I also changed the initController function as follows:
PHP Code:
//initialize session
$this->session = \Config\Services::session();


//check for locale cookie
if (!empty(get_cookie('locale')))
  $_SESSION['locale'] = get_cookie('locale');

//get request locale if locale session variable not yet set
isset($_SESSION['locale']) || $_SESSION['locale'] = $this->request->getLocale();

//define LOCALE constant, to use in lang() calls
defined ('LOCALE') || define ('LOCALE',$_SESSION['locale']); 

3. Then, I created the controller app\Controllers\Locale.php to change the selected locale, for users who want to change to another locale than the one negotiated by the browser:
PHP Code:

namespace App\Controllers;

Locale extends BaseController

//set a new locale, overriding the negotiated locale
public function setlocale($locale)

        if (
in_array($locale,config('App')->supportedLocales)) {
//set locale cookie



The redirect view - app\Views\redirect.php:
<!DOCTYPE html>
<html lang="ro">
    <meta charset="utf-8">
    <script type="text/javascript">
      window.location.href = "<?=$url?>";

4. Finally, the only thing you need to use now is the LOCALE constant as the third parameter, when calling the lang() function.
PHP Code:

And here's how it works.

When visiting a page that extends BaseController:

  1. If a cookie containing the locale was previously set, the BaseController defines it as the locale session variable;
  2. If there is no locale session variable set, it acts as if visiting the site for the first time, and sets the negotiated locale as the locale session variable;
  3. Sets the LOCALE constant - just so that calling the lang() function looks prettier :)
When a user wants to change the locale, they should have a link to click, to access the setlocale method of the Locale controller - e.g. <?=site_url('locale/setlocale/en')?>
  1. If the specified locale is in the $supportedLocales configuration array, it sets the locale cookie to that value
  2. Displays the redirect view, so that the cookie is saved in the browser, which redirects afterwards to the previous url and selects the new locale using the cookie that was just set.
I hope this was useful.

Happy coding!

Theme © iAndrew 2016 - Forum software by © MyBB