elixir-cldr / cldr_routes

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

Implement localized verified routes #13

Closed kipcole9 closed 1 year ago

kipcole9 commented 1 year ago

As of this commit, localized verified routes are implemented. In order to publish, further testing and documentation is required. However it is ready for initial testing by any interested users.

Use

  1. Add {:ex_cldr_routes, github: "elixir-cldr/cldr_routes", branch: "verified_routes"} to your mix.exs
  2. Make sure you have Cldr.Route as part of your Cldr providers configuration.
  3. Add use MyAppWeb.Router.VerifiedRoutes, router: MyApp.Router, endpoint: MyApp.Endpoint to the require modules, replacing the use Phoenix.Router.VerifiedRoutes line.
  4. Use ~q (sigil_q) instead of Phoenix's sigil_p and you are done.

Implementation

Sigil_q will generate a case expression to select the correct localized path at runtime. For example:

  # ~q"/users" generates the following code under the covers:
  case MyApp.Cldr.get_locale().cldr_locale_name do
    :de -> ~p"/users_de"
    :en -> ~p"/users"
    :fr -> ~p"/users_fr"
  end

Please add comments, suggestions and general feedback to this issue.

kipcole9 commented 1 year ago

Cc: @rubas

kipcole9 commented 1 year ago

As of this commit, sigil_q is now sensitive to the optional fragment and query string at the end of a path. Neither the fragment nor query string are translated.

This is by design since these parameters would normally be considered either part of the page markup (the fragment) or the api definition (query params).

There is room to change the decision not to translate these elements. One approach would be to add two flags to the ~q sigil. f for "translate fragment" and q for "translate query keys".

Feedback is very welcome.