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
* 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
Copy file name to clipboardExpand all lines: README.md
+52-21
Original file line number
Diff line number
Diff line change
@@ -356,55 +356,86 @@ Note that Route Model Binding is supported.
356
356
357
357
## Translated Routes
358
358
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:
360
361
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)).
362
363
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:
364
366
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:
366
367
```php
368
+
<?php
367
369
// resources/lang/en/routes.php
368
370
return [
369
-
"about" => "about",
370
-
"view" => "view/{id}", //we add a route parameter
371
-
// other translated routes
371
+
"about" => "about",
372
+
"article" => "article/{article}",
372
373
];
373
374
```
374
375
```php
376
+
<?php
375
377
// resources/lang/es/routes.php
376
378
return [
377
-
"about" => "acerca",
378
-
"view" => "ver/{id}", //we add a route parameter
379
-
// other translated routes
379
+
"about" => "acerca",
380
+
"article" => "articulo/{article}",
380
381
];
381
382
```
382
383
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:
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.
384
403
385
404
### Translatable route parameters
386
405
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
388
419
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`.
391
422
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
395
424
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`.
0 commit comments