Welcome Guest, Not a member yet? Register   Sign In
setLocale() doesn't change the language set by browser
#5

Hello,

I know this thread is a little bit old but have faced the same problem in CI4, i'm not an expert, solved it this way

Have created a filter in App\Filters\Locale.php
PHP Code:
<?php namespace App\Filters;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;

class 
Locale implements FilterInterface
{
    public function before(RequestInterface $request$arguments null)
    {
        if (session()->has('locale'))
        {
            // Set site language to session locale value
            service('language')->setLocale(session('locale'));
        }
        else
        {
            // Save locale to session
            session()->set('locale'service('language')->getLocale());
        }
    }

    //--------------------------------------------------------------------

    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        // Do something here
    }


Activate the filter for every request in Config\Filters
PHP Code:
    // Makes reading things below nicer,
    // and simpler to change out script that's used.
    
public $aliases = [
        
'csrf'     => \CodeIgniter\Filters\CSRF::class,
        
'toolbar'  => \CodeIgniter\Filters\DebugToolbar::class,
        
'honeypot' => \CodeIgniter\Filters\Honeypot::class,
        
'locale'   => \App\Filters\Locale::class
    ];

    
// Always applied before every request
    
public $globals = [
        
'before' => [
            
'honeypot',
            
'csrf',
            
'locale',
        ],
        
'after'  => [
            
'toolbar',
            
'honeypot'
        
],
    ]; 

A controller to allow user to change language
PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

/**
 * Change site locale to requested one
 */
class Locale extends Controller
{
    // Set application wide locale
    public function setstring $locale )
    {
        
// Check requested language exist in \Config\App
        
if ( in_array($localeconfig('App')->supportedLocales) )
        {
            
// Save requested locale in session, will be set by filter
            
session()->set('locale'$locale);

            
// Reload page
            
return redirect()->back();
        }
        else
        {
            throw new \CodeIgniter\Exceptions\PageNotFoundExceptionesc($locale) ." is not a supported language" );
        }
    }


Controller route
Code:
$routes->get('locale/(:segment)', 'Locale::set/$1');


My Config\App

PHP Code:
    public $defaultLocale 'fr';

    
/*
    |--------------------------------------------------------------------------
    | Negotiate Locale
    |--------------------------------------------------------------------------
    |
    | If true, the current Request object will automatically determine the
    | language to use based on the value of the Accept-Language header.
    |
    | If false, no automatic detection will be performed.
    |
    */
    
public $negotiateLocale true;

    
/*
    |--------------------------------------------------------------------------
    | Supported Locales
    |--------------------------------------------------------------------------
    |
    | If $negotiateLocale is true, this array lists the locales supported
    | by the application in descending order of priority. If no match is
    | found, the first locale will be used.
    |
    */
    
public $supportedLocales = ['fr','en']; 

This way Codeigniter set locale to defined browser language automatically and user can use the controller to set another language.

View example to change language, with selected language flag auto update
Code:
<!-- Language dropdown -->
<div class="navbar-tool dropdown">
    <a class="topbar-link" data-toggle="dropdown" aria-expanded="false">
        <img width="20" src="/assets/img/flags/<?= session('locale') ?>.png" />
    </a>
    <ul class="dropdown-menu">
        <li>
            <a class="dropdown-item" href="/locale/en"><img width="20" src="/assets/img/flags/en.png" alt="English" />English</a>
        </li>
        <li>
            <a class="dropdown-item" href="/locale/fr"><img width="20" src="/assets/img/flags/fr.png" alt="Français" />Français</a>
        </li>
    </ul>
</div>

If anyone know a better way, any advises welcome  Wink
Reply


Messages In This Thread
RE: setLocale() doesn't change the language set by browser - by eelisland - 01-10-2021, 11:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB