View Source PhxLocalizedRoutes.Helpers (Phoenix Localized Routes v0.1.2)

Helpers to be used in views and controllers.

Link to this section Summary

Functions

Marco used to wrap a Phoenix route and transform it into a localized route. The localized routes use the :scope_helper to alter the destination (alias) of the route on render.

Link to this section Functions

Link to this macro

loc_route(orig_route, loc_opts \\ nil)

View Source (macro)
@spec loc_route(
  orig_route :: Macro.t(),
  loc_opts :: PhxLocalizedRoutes.Scope.Flat.t() | nil
) ::
  Macro.output()

Marco used to wrap a Phoenix route and transform it into a localized route. The localized routes use the :scope_helper to alter the destination (alias) of the route on render.

By default it uses the scope_helper from Phoenix.LiveView.Socket or Plug.Conn, keeping the user in it's current locale / scope. A custom :scope_helper can be provided through assigns in loc_opts.

Example

Routes.product_index_path(@socket, :index)
/products

loc_opts = %{assigns: %{scope_helper: "eu_nl"}}
loc_route(Routes.product_index_path(@socket, :index), loc_opts)
/eu/nl/products

When no :scope_helper is found or when no matching helper function is exported, an error is logged and the original link will be returned.

Example: Generate links to all other localized routes of the product index

<!-- ExampleWeb.LocalizedRoutes is aliased as Loc in view_helpers() -->
<!-- loc_route is imported from PhxLocalizedRoutes.Helpers -->
<%= for {slug, opts} <- Loc.scopes(), opts.assigns.scope_helper != @loc.scope_helper do %>
    <span>
      <%= link " [#{slug}] ", to: loc_route(Routes.product_index_path(@socket, :index), opts) %></span>
<% end %>