Welcome Guest, Not a member yet? Register   Sign In
Localization in Routes
#4

Hi, thanks for your comments !

I think you both have valuable arguments, storing the language in a cookie is indeed a valid solution to this problem, however it is not our preferred approach.

To answer my own question, I found that I could implement the desired functionality through the creation of a Filter (see code below)

application\Filters\Localization.php :

PHP Code:
<?php namespace App\Filters;

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

class 
Localization implements FilterInterface
{
 
   public function before(RequestInterface $request)
 
   {
 
       $supportedLocales $request->config->supportedLocales;

 
       if(sizeof($supportedLocales) > 1){
 
       
        
// more than one locale...
 
       $seqments $request->uri->getSegments();

 
       if($request->uri->getTotalSegments() > && !in_array($request->uri->getSegment(1), $supportedLocales)){

 
       // TODO: check for exceptions (non-localized routes)

 
       return redirect($request->config->defaultLocale);
 
       }

 
       }

 
   }

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

 
   public function after(RequestInterface $requestResponseInterface $response)
 
   {
 
       // Do something here
 
       // var_dump($response);
 
   }



application\Config\Filters.php:

PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
Filters extends BaseConfig
{
    
// Makes reading things below nicer,
    // and simpler to change out script that's used.
    
public $aliases = [
        
'localization'    => \App\Filters\Localization::class,
        
'csrf'            => \App\Filters\CSRF::class,
        
'toolbar'        => \App\Filters\DebugToolbar::class,
        
'honeypot'        => \App\Filters\Honeypot::class
    ];

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

    
// Works on all of a particular HTTP method
    // (GET, POST, etc) as BEFORE filters only
    //     like: 'post' => ['CSRF', 'throttle'],
    
public $methods = [];

    
// List filter aliases and any before/after uri patterns
    // that they should run on, like:
    //    'isLoggedIn' => ['before' => ['account/*', 'profiles/*']],
    
public $filters = [];

Reply


Messages In This Thread
Localization in Routes - by Dmonkeyjazz - 10-24-2018, 05:24 AM
RE: Localization in Routes - by Przem4S - 11-06-2018, 02:01 AM
RE: Localization in Routes - by Pertti - 11-06-2018, 02:35 AM
RE: Localization in Routes - by Dmonkeyjazz - 11-09-2018, 04:50 AM
RE: Localization in Routes - by puschie - 11-09-2018, 07:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB