chinleung / laravel-multilingual-routes

A package to handle multilingual routes in your Laravel application.
https://github.com/chinleung/laravel-multilingual-routes-demo
MIT License
394 stars 26 forks source link

Multi-segment route translation #44

Closed mvneobux closed 3 years ago

mvneobux commented 3 years ago

I can only translate the first segment of each URL. For example:

return [
  'sonhos' => 'dreams',
  'contato' => 'contact',
  'sobre-nos' => 'about-us',
  'perguntas-frequentes' => 'common-questions',
  'termos-e-condicoes' => 'terms-and-conditions',
  'politica-de-privacidade' => 'privacy-policy',
  'idiomas' => 'languages',
];

So far it works normal. But when I need to translate this url for example: https://www.meempi.com/perfil/preferencias

I can't translate, how should I proceed in that case when the routes have more than one segment?

chinleung commented 3 years ago

@mvneobux You can include segments in your translation file. For instance:

return [
    // ...
    'preferencias' => 'perfil/preferencias'
];

And in your route you will have:

Route::multilingual('preferencias', PreferenciasController::class);
mvneobux commented 3 years ago

@mvneobux You can include segments in your translation file. For instance:

return [
    // ...
    'preferencias' => 'perfil/preferencias'
];

And in your route you will have:

Route::multilingual('preferencias', PreferenciasController::class);

does not work. My route:

Route::multilingual('perfil/preferencias', function () {
    return view('preferencias');
})->middleware('auth')->name('preferencias');

I'll give another example, of routes that have parameters. How could I translate the following routes:

Route::multilingual('sonhos', 'GruposController@sonhos')->name('sonhos');

Route::multilingual('sonhos/{grupo}', 'GruposController@grupoPerfil')->name('grupoPerfil');

Route::multilingual('sonhos/{grupo}/significado', 'GruposController@significadoGrupo')->name('significadoGrupo');

Route::multilingual('sonhos/{grupo}/novo', 'GruposController@novoRelato')->middleware('auth')->name('novoRelato');

How can I translate these routes?

chinleung commented 3 years ago

@mvneobux You are not using the key correctly in your function. It should be Route::multilingual('preferencias', ...); instead of Route::multilingual('perfil/preferencias', ...);. Put the segments in your routes.php translation file instead of the web.php.

As for the routes with parameters, you can use a meaningful key name such as single-grupo.

Route::multilingual('single-groupo', ...);`

Then in your routes.php:

return [
    // ...
    'single-groupo' => 'sonhos/{group}',
];

In the demo repository, I use show-user to register a utilisateurs/{user} route.

mvneobux commented 3 years ago

I did not understand. So the way my routes are, I will not be able to translate?

My route is "perfil/preferencias" and not "preferencias"

As I understand it, I can only translate when the route has only one segment. Look at this example again, how can I translate this:

`Route::multilingual('sonhos', 'GruposController@sonhos')->name('sonhos');

Route::multilingual('sonhos/{grupo}', 'GruposController@grupoPerfil')->name('grupoPerfil');

Route::multilingual('sonhos/{grupo}/significado', 'GruposController@significadoGrupo')->name('significadoGrupo');

Route::multilingual('sonhos/{grupo}/novo', 'GruposController@novoRelato')->middleware('auth')->name('novoRelato');`

chinleung commented 3 years ago

@mvneobux The preferencias keyword would be the key in the translation file, not the path of the route itself.

Here's an example of how you can achieve what you want 2 ways.

Using a translation key

// routes/web.php
Route::multilingual('preferencias')->view('welcome');

// resources/lang/es/routes.php
return [
    'preferencias' => 'perfil/preferencias',
];

// resources/lang/en/routes.php
return [
    'preferencias' => 'profile/preferences',
];

Using the full path as the key

// routes/web.php
Route::multilingual('perfil/preferencias')->view('welcome');

// resources/lang/en/routes.php
return [
    'perfil/preferencias' => 'profile/preferences',
];

In both cases, it will generate the following routes:

https://example.test/perfil/preferencias
https://example.test/en/profile/preferences
mvneobux commented 3 years ago

@mvneobux The preferencias keyword would be the key in the translation file, not the path of the route itself.

Here's an example of how you can achieve what you want 2 ways.

Using a translation key

// routes/web.php
Route::multilingual('preferencias')->view('welcome');

// resources/lang/es/routes.php
return [
    'preferencias' => 'perfil/preferencias',
];

// resources/lang/en/routes.php
return [
    'preferencias' => 'profile/preferences',
];

Using the full path as the key

// routes/web.php
Route::multilingual('perfil/preferencias')->view('welcome');

// resources/lang/en/routes.php
return [
    'perfil/preferencias' => 'profile/preferences',
];

In both cases, it will generate the following routes:

https://example.test/perfil/preferencias
https://example.test/en/profile/preferences

this worked perfectly:

// routes/web.php
Route::multilingual('perfil/preferencias')->view('welcome');

// resources/lang/en/routes.php
return [
    'perfil/preferencias' => 'profile/preferences',
];

But I still haven't been able to translate routes with parameters, like this:

Route::multilingual('sonhos', 'GruposController@sonhos')->name('sonhos');

Route::multilingual('sonhos/{grupo}', 'GruposController@grupoPerfil');

Route::multilingual('sonhos/{grupo}/significado', 'GruposController@significadoGrupo');

Route::multilingual('sonhos/{grupo}/novo', 'GruposController@novoRelato');`

I have this that works well only on the first route:

return [
    'sonhos' => 'dreams',
];

But what about the rest? I tried everything but I can't, I thought I could solve it like this:

return [
    'sonhos/{grupo}' => 'dreams/{grupo}',
    'sonhos/{grupo}/significado' => 'dreams/{grupo}/means',
    'sonhos/{grupo}/novo' => 'dreams/{grupo}/new',
];

How do I solve this?

chinleung commented 3 years ago

@mvneobux What version of the package are you using? I just tested it on the latest version and it works properly with:

// resources/lang/en/routes.php
return [
    'sonhos/{grupo}' => 'dreams/{grupo}',
];
mvneobux commented 3 years ago

@mvneobux What version of the package are you using? I just tested it on the latest version and it works properly with:

// resources/lang/en/routes.php
return [
    'sonhos/{grupo}' => 'dreams/{grupo}',
];

"chinleung/laravel-multilingual-routes": "^2.6",

Laravel 7

chinleung commented 3 years ago

@mvneobux Can you try to update to ^2.7? If it's still not working, could you provide a demo repository so I could check what's not working?

Also, when you say that it's not working, what is the result you are getting? Can you show me the output of php artisan route:list --columns=uri,name please?

mvneobux commented 3 years ago

Friend worked :)

Thank you very much for your attention and responsiveness.

Sorted out!

thank you so much

chinleung commented 3 years ago

@mvneobux Alright! Glad it's working. 😄