codezero-be / laravel-localized-routes

⭐️ A convenient way to set up and use localized routes in a Laravel app.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's the best way to deal with more complex locales like 'ch-fr'?

gpluess opened this issue · comments

I have a requirement to include the locale AND the country in the URL like this:

/ch-fr/entreprises
/de-en/companies
/at-de/unternehmen

I am also going to have multiple countries with the same locale:

/ch-de/unternehmen
/de-de/unternehmen
/at-de/unternehmen

This basically works by setting the supported-locales config value to something like this:

'supported-locales' => [
  'ch-de', 'ch-fr', 'ch-it', 'ch-en',
  'de-de', 'de-en',
  'at-de', 'at-de',
],

However, I think this has some disadvantages, for example, now I have to have 3 versions for German in the lang directory and keep them in sync: ch-de, de-de and at-de. Having only one de directory no longer works. Other third party packages like spatie/laravel-translatable do not work well with this approach.

In order to avoid duplication, I would have proposed a syntax like this:

'supported-locales' => [
  'de' => [
    'ch-de',
    'de-de',
    'at-de',
  ],
  'en' =>  [
    'ch-en',
    'de-en',
    'at-en',
  ],
  'fr' =>  [
    'ch-fr',
  ],
],

But I guess this would break the domains/subdomains feature. So I'm wondering what's the best approach to deal with this?

Any suggestions are highly appreciated!

Hello,

That's an interesting idea.
It is like the recent PR #70 but instead of a single prefix/slug, you want multiple slugs.

Now, I've been thinking about this for a short while and found a few complications...
Mainly I think I need to figure out a new and better way to map all the locale parts together in a flexible, yet not to complicated way. Also related to #20 and #21, but those ideas are not fully worked out yet either.

A slug in the URL needs to be connected to a locale for route names and translations (a sort of internal locale).
But when you generate a route('some-route'), this also needs to know what slug to use. That is currently not possible if multiple are connected to a single internal locale. So I think the slugs might need to become the source of truth, the unique "key" when looking up locale configurations.

I still need to work this out further, but I will certainly include your suggestion in the final result.