codezero-be / laravel-localized-routes

⭐️ A convenient way to set up and use localized routes in a Laravel app.
MIT License
505 stars 46 forks source link

Default locale without prefix, is it possible? #1

Closed vladshoob closed 5 years ago

vladshoob commented 5 years ago

Hi there.

I am struggling to manage my issue. What I need is to have these routes available:

/contacts - for default english, /uk/contacts - for ukrainian, etc...

Maybe some hints?

mark1282 commented 5 years ago

I'm wondering the same. For now I've just duplicated the routes so I have the localised English and non localised default. If I get chance I'll have a look through the source code.

ivanvermeyen commented 5 years ago

Hi,

Sorry I forgot about this issue.

This is not the way this package currently works. You can define normal routes, for example a home page with language selection and then localized URL's that have a prefix. When you generate a URL, the normal routes have priority over the localized ones if the route name is the same.

However, if we add an option like omit_prefix_for_locale = 'en'; and we do some if checks against the current locale in certain places to generate the correct routes and URL's, I think it is possible.

If you have time to look into this, that would be much appreciated. If not, I will see if I can get to this any time soon.

Thanks, Ivan

vladshoob commented 5 years ago

I'm wondering the same. For now I've just duplicated the routes so I have the localised English and non localised default. If I get chance I'll have a look through the source code.

I ended up using another package that solves this problem: https://github.com/mcamara/laravel-localization .

LuizTNB commented 5 years ago

Oh, same problem here :(

@ivanvermeyen your package is good! If you solve this issue, this package will gonna be AWESOME!

Thank you

ivanvermeyen commented 5 years ago

I think this latest commit should do the trick. Might need some more edge case checking.

ivanvermeyen commented 5 years ago

One "problematic" scenario I can think of is:

Working example that registers a root homepage and an index page for each locale:

Config::set('localized-routes.supported-locales', ['en', 'nl']);
Config::set('localized-routes.omit_url_prefix_for_locale', null);

Route::localized(function () {
    // Eg. registers: '/en' and '/nl'
    Route::get('/', Controller::class.'@index')->name('index');
});

// Registers '/'
Route::get('/', Controller::class.'@index')->name('home');

If you omit the prefix for the main locale, you can not register the above. But this probably makes sense, so it's a non issue?

Config::set('localized-routes.supported-locales', ['en', 'nl']);
Config::set('localized-routes.omit_url_prefix_for_locale', 'en');

Route::localized(function () {
    // Eg. registers: '/' and '/nl'
    Route::get('/', Controller::class.'@index')->name('index');
});

// This URL is already registered...
Route::get('/', Controller::class.'@index')->name('home');
ivanvermeyen commented 5 years ago

Tagged in release 1.1.0 👍