Skip to content

Commit 425fc4d

Browse files
iwasherefirst2Marc Cámara
authored and
Marc Cámara
committed
Improve translatable url section in readme (#684)
* Updated very importat things in readme * Improved translated-routes section * Improved translated-routes section * Add youtube video to instruction * General imrpovments * Try to fix spacing issue * Try to fix spacing issue * Small improvments on language and structure
1 parent e9c5c00 commit 425fc4d

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

README.md

+52-21
Original file line numberDiff line numberDiff line change
@@ -356,55 +356,86 @@ Note that Route Model Binding is supported.
356356

357357
## Translated Routes
358358

359-
You can adapt your URLs depending on the language you want to show them. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish) or http://url/en/view/5 and http://url/es/ver/5 (view == ver in spanish) would be redirected to the same controller using the proper filter and setting up the translation files as follows:
359+
You may translate your routes. For example, http://url/en/about and http://url/es/acerca (acerca is about in spanish)
360+
or http://url/en/article/important-article and http://url/es/articulo/important-article (article is articulo in spanish) would be redirected to the same controller/view as follows:
360361

361-
It is necessary that the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#LaravelLocalizationRoutes)).
362+
It is necessary that at least the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#LaravelLocalizationRoutes)).
362363

363-
In the routes file you just have to add the `LaravelLocalizationRoutes` filter and the `LaravelLocalization::transRoute` function to every route you want to translate using the translation key.
364+
For each language, add a `routes.php` into `resources/lang/**/routes.php` folder.
365+
The file contains an array with all translatable routes. For example, like this:
364366

365-
Then you have to create the translation files and add there every key you want to translate. I suggest to create a routes.php file inside your `resources/lang/language_abbreviation` folder. For the previous example, I have created two translations files, these two files would look like:
366367
```php
368+
<?php
367369
// resources/lang/en/routes.php
368370
return [
369-
"about" => "about",
370-
"view" => "view/{id}", //we add a route parameter
371-
// other translated routes
371+
"about" => "about",
372+
"article" => "article/{article}",
372373
];
373374
```
374375
```php
376+
<?php
375377
// resources/lang/es/routes.php
376378
return [
377-
"about" => "acerca",
378-
"view" => "ver/{id}", //we add a route parameter
379-
// other translated routes
379+
"about" => "acerca",
380+
"article" => "articulo/{article}",
380381
];
381382
```
382383

383-
Once files are saved, you can access to http://url/en/about , http://url/es/acerca , http://url/en/view/5 and http://url/es/ver/5 without any problem.
384+
You may add the routes in `routes/web.php` like this:
385+
386+
```php
387+
Route::group(['prefix' => LaravelLocalization::setLocale(),
388+
'middleware' => [ 'localize' ]], function () {
389+
390+
Route::get(LaravelLocalization::transRoute('routes.about'), function () {
391+
return view('about');
392+
});
393+
394+
Route::get(LaravelLocalization::transRoute('routes.article'), function (\App\Article $article) {
395+
return $article;
396+
});
397+
398+
//,...
399+
});
400+
```
401+
402+
Once files are saved, you can access http://url/en/about , http://url/es/acerca , http://url/en/article/important-article and http://url/es/articulo/important-article without any problem.
384403

385404
### Translatable route parameters
386405

387-
You may use translatable slugs for your model, for example like this:
406+
Maybe you noticed in the previous example the English slug in the Spanish url:
407+
408+
http://url/es/articulo/important-article
409+
410+
It is possible to have translated slugs, for example like this:
411+
412+
http://url/en/article/important-change
413+
http://url/es/articulo/cambio-importante
414+
415+
However, in order to do this, each article must have many slugs (one for each locale).
416+
Its up to you how you want to implement this relation. The only requirement for translatable route parameters is, that the relevant model implements the interface `LocalizedUrlRoutable`.
417+
418+
#### Implementing LocalizedUrlRoutable
388419

389-
http://url/en/view/five
390-
http://url/es/ver/cinco
420+
To implement `\Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable`,
421+
one has to create the function `getLocalizedRouteKey($locale)`, which must return for a given locale the translated slug. In the above example, inside the model article, `getLocalizedRouteKey('en')` should return `important-change` and `getLocalizedRouteKey('es')` should return `cambio-importante`.
391422

392-
For this, your model needs to implement `\Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable`.
393-
The function `getLocalizedRouteKey($locale)` must return for a given locale the translated slug.
394-
This is necessary so that your urls will be correctly [localized](#localized-urls).
423+
#### Route Model Binding
395424

396-
Also, to use [route-model-binding](https://laravel.com/docs/routing#route-model-binding), you should overwrite the function `resolveRouteBinding($value)`
397-
in your model. The function should return the model that belongs to the translated slug `$value`.
425+
To use [route-model-binding](https://laravel.com/docs/routing#route-model-binding), one should overwrite the function `resolveRouteBinding($slug)`
426+
in the model. The function should return the model that belongs to the translated slug `$slug`.
398427
For example:
399428

400429
```php
401-
public function resolveRouteBinding($value)
430+
public function resolveRouteBinding($slug)
402431
{
403-
return static::findByLocalizedSlug($value)->first() ?? abort(404);
432+
return static::findByLocalizedSlug($slug)->first() ?? abort(404);
404433
}
405434
```
406435

436+
#### Tutorial Video
407437

438+
You may want to checkout this [video](https://youtu.be/B1AUqCdizgc) which demonstrates how one may set up translatable route parameters.
408439

409440
## Events
410441

0 commit comments

Comments
 (0)