Global default url parameters based on conn values

Hi guys!

I’m using set_locale for language handling. My client requested to have locale included in the path instead of parameters. So instead of:

example.com?locale=pl we should have: example.com/pl

Furthermore, for the default locale, the path SHOULD NOT include locale, but instead, it should be assigned via cookies. Assuming that default locale is en, instead of:

example.com/en it should be example.com

With a bit of modification in set_locale I was able to set the root path for default locale without including it in the path. There is however a problem here. I need to include @locale in every path helper (routes are scoped in /:locale) across the entire app. For default locale, I don’t want to include it in the path since it’s one of the client requests.

So the question is - Is there any way to pass default params to path generators globally? In Rails, I’m able to achieve that with the default_url_options method in Controller. Such a default URL option in my case depends on @conn assigns which is probably problematic.

I’d normalize that outside of the router, a.k.a. in the endpoint remove the leading path segment if it’s a locale and for url helpers use a function to scope them to locales like conn |> maybe_prepend_locale() |> Routes.page_path(:index). Path helpers do work when supplied a %URI{} struct, so that’s what maybe_prepend_locale would likely return.