Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Translated Routes (i18n) + linking in views
  • Currently looking to implement FuelPHP where I work, but just want to make sure it can do everything we currently have built in our in-house framework. Most important thing I couldn't figure out yet is to do Translated Routes and linking to those routes in a view. i.e for a English/French news details page: - /en/news/{id}/title-of-news
    - /fr/nouvelles/{id}/titre-de-la-nouvelle Both would need to get routed to a news controller, a view action, and the id as a param. Then how would you link to those in a view? Any help would be appreciated, and let me know if something isn't clear enough! Looking forward to using FuelPHP full-time ;) Guillaume
  • hey falzhobel, Could you explain if you succesfully built the system? And how?
    Im also trying to develop it but since im very very newbie on Fuel Im not able yet :( Thank you! PD: Have you consider to built in in a package? Would be cool :P
  • Nope didn't had the time to implement it yet, but I'll let you know!
  • There is no magically way to construct anchors in your views based on external parameters. You could use reverse routing, so you can fetch the public route based on it's name, and then use a custom class method that fetches the routed based on the current language, inserts the param, and calls html::anchor() to return the custom link. You define a route like so:
    'en/news/:id/title-of-news' => array('news/index', name => 'en_news'),
    'fr/nouvelles/:id/titre-de-la-nouvelle' => array('news/index', name => 'fr_news'),
    
    You can fetch the id param using
    $this->request()->param('id', 'default');
    
    You can get a route using
    $route = Router::get('fr_news');
    
    you will have to manually replace :id with your id value in $route, this has not been implemented yet.
  • Awesome, thank you for the fast reply! Reverse Routing was what I was looking for along with Router::get(). Now, would it be possible to extend the routing system so that I could do
    $route = Router::get('news')
    
    And based on a global $lang var or something similar the routing system would return the given lang's url, something along those lines (just for visualization purpose):
    'news' => array(
        'en' => 'news/:id/:title',
        'fr' => 'nouvelles/:id/:title'
    ),
    
    Hopefully that will make sense. Thanks again!
  • Just extend the Router class, and overload the get() method. Create the file router.php in app/classes, put a "class Router extends \Fuel\Core\Router" in there, define your get method, and add the class to your app bootstrap (there's a commented example in there already). I would suggest your get first prepend the language code to the parameter passed to get(), call the parent::get() to see if it exists, and if not, call parent::get() again without the language code ( to maintain API compatibility ). As to the language code, if you're smart you use the language feature provided by FuelPHP, so you don't have to maintain two systems. You can set the language using
    \Config::set('language', 'fr');
    
    and get it with
    $lang = \Config::get('language');
    
    you set the default language (and a possible fallback language) in your app config file.
  • Perfect! I'll let you know how this goes once I start implementing FuelPHP in a dev project. Thanks again !
  • anyone implemented something like this ?
  • I've implemented something like that for a previous project and I wrote an article about that, it could help you: link


Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion