Localized verified routes using the ~q sigil.
This module provides compile-time verified localized routes. Instead of configuring with use Phoenix.VerifiedRoutes, configure instead:
use Localize.VerifiedRoutes,
router: MyApp.Router,
endpoint: MyApp.Endpoint,
gettext: MyApp.GettextWhen configured, the sigil ~q is made available to express localized verified routes. Sigil ~p remains available for non-localized verified routes.
The ~q sigil generates a case statement that dispatches to the appropriate localized ~p route based on the current locale:
# ~q"/users" generates:
case Localize.get_locale().cldr_locale_id do
:de -> ~p"/benutzer"
:en -> ~p"/users"
:fr -> ~p"/utilisateurs"
endLocale Interpolation
:localeis replaced with the CLDR locale name.:languageis replaced with the CLDR language code.:territoryis replaced with the CLDR territory code.
Rendering a path or URL in a specific locale
sigil_q dispatches on the current process locale set by
Localize.put_locale/1. When you need to render a link in a different
locale without changing the process locale — for example, emitting a
language switcher that lists the same page in every configured locale —
use path_for/2 and url_for/2:
# In a template, with @locale bound from the request or session:
<.link href={path_for(@locale, "/users")}>Users</.link>
# Render every configured locale in one pass (language switcher):
for locale <- [:en, :fr, :de] do
path_for(locale, "/users")
end
url_for(:fr, "/users")
#=> "http://localhost/users_fr"
Summary
Functions
Generates a localized verified path in a specific locale.
Implements the ~q sigil for localized verified routes.
Generates the router url with localized route verification.
Generates the router url with localized route verification from the connection, socket, or URI.
Generates the router url with localized route verification from the connection, socket, or URI and router.
Generates a localized verified URL in a specific locale.
Functions
Generates a localized verified path in a specific locale.
Unlike sigil_q/2, which dispatches on the current locale
(Localize.get_locale/0), path_for/2 lets the caller force a particular
locale at the call site without changing the process-wide locale. This is
useful when rendering links in multiple locales within a single template
(for example, a language switcher).
Arguments
localeis any locale id configured in the gettext backend. May be a literal atom or a runtime expression.routeis a string literal route (with optional#{...}interpolations), as accepted bysigil_q/2.
Examples
path_for(:fr, "/users")
#=> "/utilisateurs"
for locale <- [:en, :fr] do
{locale, path_for(locale, "/users")}
end
#=> [en: "/users", fr: "/utilisateurs"]
Implements the ~q sigil for localized verified routes.
Generates a case expression that dispatches to the translated ~p route for the current locale. The route path is verified at compile time against the router.
Generates the router url with localized route verification.
Generates the router url with localized route verification from the connection, socket, or URI.
Generates the router url with localized route verification from the connection, socket, or URI and router.
Generates a localized verified URL in a specific locale.
Like path_for/2 but returns a full URL via Phoenix.VerifiedRoutes.url/1.
Arguments
localeis any locale id configured in the gettext backend.routeis a string literal route accepted bysigil_q/2.