F3 multilangue plugin

62 views
Skip to first unread message

Gosforth

unread,
Oct 31, 2017, 5:01:16 AM10/31/17
to Fat-Free Framework
Hi,

I use this nice piece of software:


Works fine and adds lang code to each route .

If I start application from "/" it will redirect to "/lang_code" - nice.
But if I want to start from "/something" sure, it will generate error "not found" because reall route is "/lang_code/something"

Any way to redirect automatically to  "/lang_code/something" if user typed "/something" in browser?

Regards,

G.

xfra35

unread,
Oct 31, 2017, 9:08:25 AM10/31/17
to Fat-Free Framework
Hi,

They are many ways to achieve this.

First of all, to which "lang_code" do you wish to redirect? To the browser's language? Or to the website primary language?

Gosforth

unread,
Oct 31, 2017, 10:13:11 AM10/31/17
to Fat-Free Framework
To browser language.

I use routing.ini.php file to manage routes

Currently I have something like:

GET /something =\Controllers\main->something

xfra35

unread,
Oct 31, 2017, 3:51:22 PM10/31/17
to Fat-Free Framework
The two following solutions should be working:

Solution #1

Catch 404 errors and redirect to the multilingual URL, if it exists:

// index.php

$f3
->ONERROR = function($f3) {
  $ml
= Multilang::instance();
 
if ($ml->auto && $f3->VERB=='GET' && isset($f3->ROUTES[ $new = '/'.$ml->current.$f3->PATH ])) {
   
if ($f3->QUERY)
      $new
.= '?'.$f3->QUERY;
    $f3
->reroute($new);
 
}
};


Solution #2

Add initial paths to the routing table:

// index.php

$paths
= array_keys($f3->ROUTES);// store the initial paths, before the plugin instantiates

$ml
= Multilang::instance();

if ($ml->auto)
 
foreach ($paths as $path) {
    $new
= '/'.$ml->current.$f3->PATH;
   
if ($f3->QUERY)
      $new
.= '?'.$f3->QUERY;
    $f3
->redirect('GET '.$f3->PATH,$new,FALSE);
 
}

Gosforth

unread,
Nov 2, 2017, 7:47:35 AM11/2/17
to Fat-Free Framework
Thank you very much indeed! I will try.
Reply all
Reply to author
Forward
0 new messages