How to Create Multilingual Translated Routes in Laravel

How to create multilingual translated routes in Laravel

First step:

Go to app/lang directory and create here translations for your routes for each language. You need to create 3 routes.php files - each in separate language directory (pl/en/fr) because you want to use 3 languages

For Polish:

<?php

// app/lang/pl/routes.php

return array(

'contact' => 'kontakt',
'about' => 'o-nas'
);

For English:

<?php

// app/lang/en/routes.php

return array(
'contact' => 'contact',
'about' => 'about-us'
);

For French:

<?php

// app/lang/fr/routes.php

return array(
'contact' => 'contact-fr',
'about' => 'about-fr'
);

Second step:

Go to app/config/app.php file.

You should find line:

'locale' => 'en',

and change it into language that should be your primary site language (in your case Polish):

'locale' => 'pl',

You also need to put into this file the following lines:

/**
* List of alternative languages (not including the one specified as 'locale')
*/
'alt_langs' => array ('en', 'fr'),

/**
* Prefix of selected locale - leave empty (set in runtime)
*/
'locale_prefix' => '',

In alt_langs config you set alternative languages (in your case en and fr) - they should be the same as file names from first step where you created files with translations.

And locale_prefix is the prefix for your locale. You wanted no prefix for your default locale so it's set to empty string. This config will be modified in runtime if other language than default will be selected.

Third step

Go to your app/routes.php file and put their content (that's the whole content of app/routes.php file):

<?php

// app/routes.php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/


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

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


/*
* Set up route patterns - patterns will have to be the same as in translated route for current language
*/
foreach(Lang::get('routes') as $k => $v) {
Route::pattern($k, $v);
}


Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get(
'/',
function () {
return "main page - ".App::getLocale();
}
);


Route::get(
'/{contact}/',
function () {
return "contact page ".App::getLocale();
}
);



Route::get(
'/{about}/',
function () {
return "about page ".App::getLocale();

}
);

});

As you see first you check if the first segment of url matches name of your languages - if yes, you change locale and current language prefix.

Then in tiny loop, you set requirements for your all route names (you mentioned that you want have about and contact translated in URL) so here you set them as the same as defined in routes.php file for current language.

At last you create Route group that will have prefix as the same as your language (for default language it will be empty) and inside group you simply create paths but those parameters about and contact you treat as variables so you use {about} and {contact} syntax for them.

You need to remember that in that case {contact} in all routes will be checked if it's the same as you defined it in first step for current language. If you don't want this effect and want to set up routes manually for each route using where, there's alternative app\routes.php file without loop where you set contact and about separately for each route:

<?php

// app/routes.php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

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

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


Route::group(array('prefix' => Config::get('app.locale_prefix')), function()
{
Route::get(
'/',
function () {
return "main page - ".App::getLocale();
}
);


Route::get(
'/{contact}/',
function () {
return "contact page ".App::getLocale();
}
)->where('contact', Lang::get('routes.contact'));



Route::get(
'/{about}/',
function () {
return "about page ".App::getLocale();

}
)->where('about', Lang::get('routes.about'));


});

Fourth step:

You haven't mentioned about it, but there's one extra thing you could consider. If someone will use url /en/something where something isn't correct Route, I think the best solution to make redirection. But you should make redirection not to / because it's default language but to /en.

So now you can open app/start/global.php file and create here 301 redirection for unknown urls:

// app/start/global.php

App::missing(function()
{
return Redirect::to(Config::get('app.locale_prefix'),301);
});

Multi-language URLs in Laravel

Expanding on my comment, something like this could work

Route::get('/{welcome}', 'WaitingListController@index')
->where('welcome', 'willkommen|bienvenu|bienvenidos')
->name('waiting_list.index');

How to translate routes in Laravel?

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.

How to generate url to routes with two languages in Laravel

The best way to generate URL is:

First, give a name to yor routes:

//this route is called 'contact_route'
Route::get('/contact/', ['as' => 'contact_route', function ()
{
return view('contact');
}]);

Routes are build dynamycally using the locale and the prefix, but once a route is defined and you've give it a name, you can create a URL for the route using the route helper with: route('contact_route')

Your example will become:

<a class="block-title" href="{{ route('contact_route') }}">CONTACT</a>

You can lear more of named routes in the docs

As for the trailing slash, the default Laravel .htaccess file, removes all the slashes at the end of the urls, with this rule:

RewriteRule ^(.*)/$ $1 [L,R=301]

That captures everything (.*) from the start ^ to the end before the slash /$ and substitute it with what is captured. So, if you want to add a trailing slash, probably you should edit the .htaccess file



Related Topics



Leave a reply



Submit