You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add new Interface for translating RouteParameter
* Add instructions to use translatable route paramters
* Add route-model-binding to translatable slugs
* Correct Interface path in Readme
* Fixes PSR2 for new test method
* Update Readme
Add example for route-model-binding
Copy file name to clipboardExpand all lines: README.md
+34-48Lines changed: 34 additions & 48 deletions
Original file line number
Diff line number
Diff line change
@@ -358,53 +358,8 @@ Note that Route Model Binding is supported.
358
358
359
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:
360
360
361
-
As it is a middleware, first you have to register in on your `app/Http/Kernel.php` file like this:
361
+
It is necessary that the `localize` middleware in loaded in your `Route::group` middleware (See [installation instruction](#LaravelLocalizationRoutes)).
362
362
363
-
```php
364
-
<?php namespace App\Http;
365
-
366
-
use Illuminate\Foundation\Http\Kernel as HttpKernel;
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.
409
364
410
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:
@@ -427,6 +382,30 @@ return [
427
382
428
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.
429
384
385
+
### Translatable route parameters
386
+
387
+
You may use translatable slugs for your model, for example like this:
388
+
389
+
http://url/en/view/five
390
+
http://url/es/ver/cinco
391
+
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).
395
+
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`.
You can capture the URL parameters during translation if you wish to translate them too. To do so, just create an event listener for the `routes.translation` event like so:
@@ -444,9 +423,16 @@ Be sure to pass the locale and the attributes as parameters to the closure. You
444
423
445
424
## Caching routes
446
425
447
-
More information on support on [cached (translated) routes here](CACHING.md).
426
+
To cache your routes, use:
427
+
428
+
```bash
429
+
php artisan route:trans:cache
430
+
```
431
+
432
+
... instead of the normal `route:cache` command.
433
+
448
434
449
-
Note that the separate [czim/laravel-localization-route-cache](https://github.com/czim/laravel-localization-route-cache) package is no longer required.
0 commit comments