elixir-cldr / cldr_routes

Localised routes and path helpers for Phoenix applications
Other
11 stars 6 forks source link

Looking for a way to force the generation of a localized path in a specific locale #18

Open nicolasblanco opened 2 months ago

nicolasblanco commented 2 months ago

Hello,

thank you for the library, I'm just discovering it and trying to see if I could integrate it in my project.

This is more a question than issue, but I'll be happy to provide a PR to improve the documentation. I've searched the doc and README and something is not clear to me : is it possible to force the generation of a path in a specific locale (if it was defined correctly) without the need to change the locale in the Cldr module ?

In the README there is this example:

iex> MyApp.Cldr.put_locale("en")
iex> MyAppWeb.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
"/pages/1"
iex> MyApp.Cldr.put_locale("fr")
iex> MyAppWeb.Router.LocalizedHelpers.page_path %Plug.Conn{}, :show, 1
"/pages_fr/1"

It seems fine but what about generating paths in different locales directly in my HTML template, changing globally the locale in Cldr multiple times to render a single HTML seems too heavy.

Thanks!

Nicolas.

kipcole9 commented 2 months ago

It's a very reasonable request, but I'm actually not quite sure how to go about it. Let me explain:

  1. The route helpers are specifically implemented to be a drop-in replacement for the Phoenix helpers - and they don't accomodate parameters (excepting parameters that are expected to be merged into the URL).
  2. Route helpers are really deprecated functionality in Phoenix in favour of verified routes. I'd prefer to invest time in improving those via sigil_q than trying to add this to route helpers.
  3. In the whole of the ex_cldr code base and libraries, the code I dislike the most is route helpers because that required some seriously horrible coding to grab parts of Phoenix route helpers and modify them. I'm a bit terrified to revisit them (ok, so perhaps thats not a good reason to avoid doing the work).

The call to Cldr.get_locale/0 is just a call to Process.get/1 so thankfully that is not big overhead - but I agree that the ergonomics aren't great when you just want to generate a route for a specific locale. Let me think on this and revert after some more coffee.

nicolasblanco commented 2 months ago

Thanks for your fast answer.

I also agree that because Phoenix went to deprecate helpers, it's maybe not wise to integrate more right back...

Because the new way of generating routes is by using a sigil, what do you think of a special modifier of the sigil q you implemented?

Basically the locale could be passed as a modifier to the sigil q to force the generation of the path to a specific locale.

I'm not sure of what's allowed by the syntax but it could be something like:

~q"/users"fr
# or
~q"/users"/fr
# or even
~q"/users"/loc_fr
kipcole9 commented 2 months ago

Thats a good suggestion. I'm hesitating just a little because there is a proposal to add HTTP verbs as flags to sigil_p which might make this tricky. Nevertheless I'll work up an experiment to see what I can do for this. It might take until the weekend though.