xfra35 / f3-multilang

Create multilingual apps with this localization plugin for the PHP Fat-Free Framework
GNU General Public License v3.0
48 stars 13 forks source link

Dynamically change the language #8

Closed thfontaine closed 9 years ago

thfontaine commented 9 years ago

Hey, I've seen your great demo of the plug-in, but I'm wondering how you dynamically build links to change the language while staying on the same page (the flags on top of the page). What I mean is if I'm on the /en/contact/ page, how can I make a /fr/contact link on the template ? Thanks in advance for your answer =)

xfra35 commented 9 years ago

Hey Thibault, glad you like it =)

As for looping through the URLs of the current page translations, you can use the plugin alias function, which is a superset of the framework's own alias function, taking one extra $lang parameter.

In the demo, it is basically achieved like this:

$f3=Base::instance();
$ml=Multilang::instance();
foreach($ml->languages as $lang)
  echo $ml->alias($f3->ALIAS,NULL,$lang);//<-- translated page path

As you can see, the 2nd parameter is null, because the demo routes don't hold any dynamic token.

If you have dynamic tokens, you need to fill in the 2nd parameter. And the way to go depends on your app's architecture:

Hope it makes sense ^^

thfontaine commented 9 years ago

Hey Florent, Thank you for your very clear answer. It works like a charm ! :+1: Your work is very appreciated.