n4thyra
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.
Most Liked
BartOtten
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









