Welcome Guest, Not a member yet? Register   Sign In
Localization in Routes
#1
Brick 
(This post was last modified: 10-24-2018, 06:54 AM by Dmonkeyjazz.)

While trying out CI4 one of the first new features i wanted to check out was the localization and the handling of it through the routing.

We often develop multilingual websites and this is an important feature for us that we need to enable/disable based on the configuration.
In CI3 we accomplished this using our own version of MY_Lang, so we're very excited to see it natively supported in ci4.

However we notice strange behaviour when configuring 2 locales and 1 default locale.
When accessing the root of the site (without specifying a locale in the url) CI4 gives us its 404 page.
While I was expecting that a request to www.mywebsite.com would result in a redirect to www.mywebsite.com/defaultLocale

Am I missing something ?
Is this indeed the intended behaviour ?

My setup is this:

App.php:
PHP Code:
public $defaultLocale 'locale_1';
public 
$negotiateLocale true;
public 
$supportedLocales = ['locale_1''locale_2']; 

Routes.php:
PHP Code:
$routes->add('{locale}/''Home::index'); 
Reply
#2

Why u wan't push locale into url's?
Better solution is store locale in session/cookie and allow user change-language select.

In own project I make routes based on locale but like this:

PHP Code:
$routes->add(lang('Url.user_login'), 'Home::index'); 

Then in my Languages/pl/Url.php (polish) have:

PHP Code:
return [
'user_login' => '/logowanie'
]; 


or other example Languages/en/Url.php (english) have:


PHP Code:
return [
'user_login' => '/some-login-extra-link'
]; 
And this work's fine for SEO and I have "localized" links.
Reply
#3

(This post was last modified: 11-06-2018, 02:38 AM by Pertti.)

(11-06-2018, 02:01 AM)Przem4S Wrote: Why u wan't push locale into url's?

Because from search engine point of view, if you have URL www.site.com/anycontent that always defaults to lets say English, it would never index it for any other language, but www.site.com/en/samepage and www.site.com/fr/samepage are two different pages indexed, with proper language keywords etc.

More here:
https://support.google.com/webmasters/an...92?hl=en#1

I get your point that URL login in different language might be better to also have the url part translated, but it's not always viable to add that extra logic and it's easier to go - here's a locale but we still use auto-routing to specific controller.
Reply
#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
#5

we use a similar approach like @Przem4S

but we do not store the user selection -> only use the browser lang so the use can change it in a common way and we dont need to implement any features.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB