Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate routes in Laravel?

Based on this question I'm trying to translate routes in Laravel 4.2 in english and spanish.

This is my app/routes.php code:

<?php


/*Set up locale and locale_prefix if other language is selected*/
if (in_array(Request::segment(1), Config::get('app.web_config.alt_langs'))) {

    App::setLocale(Request::segment(1));
    Config::set('app.web_config.locale_prefix', Request::segment(1));
}


foreach(Lang::get('routes') as $k => $v) {
    Route::pattern($k, $v);
}

Route::group(array('prefix' => Config::get('app.web_config.locale_prefix')), function()
{
    Route::get('/{revelar_fotos}/',['as' => 'revelado_online', 'uses' => 'WebController@reveladoOnline'], function(){
        return 'pages.revelado_online'.App::getLocale();
    });

});

By clicking on the link I get this error in the url:

http://mywebsite.dev/%7Brevelar_fotos%7D

Instead of:

http://mywebsite.dev/photograph-development

This is my en/routes.php:

<?php

return array(

    'revelar_fotos' => 'photograph-development',
);

And this is my es/routes.php:

<?php

return array(

    'revelar_fotos' => 'revelado-online',
);

Why I returned this error?

like image 281
Funny Frontend Avatar asked Nov 10 '22 15:11

Funny Frontend


1 Answers

I understand that you problem is with generating the URL http://mywebsite.dev/%7Brevelar_fotos%7D

In the question you only described how you did set up the routing handling and this might work. However generating the link itself is done somewhere else and in there you have not done the substitution.

You must be generating the link something like this

URL::to(trans("revelar_fotos"));

trans("revelar_fotos") will get you the correct path and URL::to() generates full link.

like image 173
Margus Pala Avatar answered Nov 14 '22 23:11

Margus Pala