Elegant way to set up routes for a website that may be multi-lingual

I have seen a similar question somewhere here, but I can’t find it and I don’t think it included a suitable solution.

Let’s consider a scenario where we work with the following languages and domains

cs-CZ, en-GB, en-US, de-DE, fr-FR, it-IT
www.example.cz 
www.example.com 
www.example.fr 
www.example.it

For whatever reason en-GB, en-US, and de-DE need to share one domain, which is www.example.com

We want to show news in each and every language on the following URLs

www.example.cz/novinky 
www.example.com/news
www.example.com/en-us/news 
www.example.com/de-de/aktualiten
www.example.fr/nouvelles 
www.example.it/notizia

It would be easy if there was a domain for every language, or all languages were hosted on a single domain, or all domains started with www.domain.xx/xx-xx/news

I could put in my router.ex

Enum.each ["/:locale", ""], fn locale ->
    scope locale, PlutoWeb do
      pipe_through [:browser, :ensure_redirect]

      get "/", PageController, :index
    end
  end

but then how will I know whether :locale is an actual language or just news?

If I go with www.domain.xx/xx-xx/news for every language, the issue is that URLs will be ugly for websites, where there will just be one language.

This might help and allows different language ‘schemes’ without collision. It does what you do in the example but also

  • supports nesting
  • rewrites all links to stay in the local scope
  • allows custom assigns per locale-scope

It’s development is young, but development active. So if you need something that’s in the libs alley, please let me know.

2 Likes

Thank you for the tip! I’ve seen your library around, I still hope that there’s a simpler solution that won’t involve installing third-party libraries.

In the worst scenario, I’ll go with “ugly” URLs.