Cochonours
I’m a newbie so it may seem like a silly question, but I can’t find the right way to do the following…
What I want : urls with locale parameter followed by whatever path. Important: paths are localised too (I’d like to add “of course”, but it’s not often the case), so " /fr/photos" and “/en/pictures” should point to the same controller and action (the locale information should just be in the params).
If I use in my router scope "/:locale/", I get the locale in the params but I cannot specify that the path “photos” should be only when the locale is “fr”.
If I use the root scope, I can write the full paths but I won’t get the locale set in params (and it would be a pain to write different ctrlers)
What I’m thinking is the best option is to declare one scope per locale like this scope "/fr/", scope "/en/", … so every scope defines its own localised paths, and write one function per locale that would set the “locale” param that I would call via pipe_through.
I’d be glad to know the recommended way, as I do not know enough to make a good judgement!
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
pedromvieira
You can create your own plug to set locale for you, but a easy way is to use set_locale.
So in your router you define your basic locale and scopes.
router.ex
This will add locale as a params to your controller.
At this point you have a locale (fr, en, any…), so it’s up to you to use (to translate with gettext) or not (to provide photos) inside your controller.
controller.ex
OvermindDL1
As a note, there is a standard header (
Accept-Language) passed in for the browser to request a locale, why not use that instead of encoding it into the path?LostKobrakai
The header is nice as a default, but people might want so view a different language than the default one.
NobbZ
Exactly this. My browser sends a German request, and i do not bother for much resources, but when I hear some technical documentation, I prefer to read it in English as the quality of the text is usually better, as well as it’s usually more complete as well.
This is especially true when I’m browsing Microsoft documentation, which is partially translated by humans and by machines or simply leaving out complete paragraphs…
allyraza
as a general approach I usually try to avoid hard coding locale params in the urls, I use the language header or a cookie if the user need to view the page in a different language
kip
You might also find Cldr.Plug.SetLocale in the package ex_cldr useful. (I’m the author). It can:
Accept-Langugeheader, the url, the session, a cookie or a query param or any combination in any priorityCochonours
That’s good to guess the default locale, but if you are not using your own browser you will not want to have the same links open in a locale you do not know. Also, as I want the urls to be different based on the locale, this wouldn’t do (aside from “/”).
Cochonours
This looks like my first solution (I do use cldr’s setlocale in my pipeline btw).
But with
there is no way to define localised urls like
get "/photos", PageController, :indexandget "/pictures", PageController, :index, as both “/fr/pictures” and “/en/photos” would resolve and I do not want that.Cochonours
When urls are not localised that’s a good possibility, until you travel and have to use browsers with chinese locale by default
@kip (I can’t reply to your post as I have 3 consecutive posts..)
I am using it for the root url in order to guess the locale! It works like a charm.
In order to make it to work with the url, I have to use the option “:path”, with a parameter name right? So as I cannot define my routes with a scope like
(UNLESS there is a way to restrict routes depending on the value of
scope "/:locale", MyAppWeb do, I cannot set this parameter name. It worked until I decided to have localised urls:locale(e.g. defineget "/pictures", PageController, :indexonly when:locale == "en").pedromvieira
You can still have scopes / pipeplines with locale or not (/photos) in a correct order in your router. In my case I have one that dosen’t need (llike /) and other that check via plug the accepted one and redirect the user to default or available language (gettext).
example without pipeline / locale