Localize.Routes.LocalizedHelpers (Localize Web v1.3.0)

Copy Markdown View Source

Generates a module that implements localized route helpers.

At compile time, this module introspects the Phoenix-generated helpers module and creates wrapper functions that dispatch to the correct locale-specific helper based on the current locale. It also generates *_links functions that produce a map of locale-to-URL pairs suitable for hreflang link tags.

Summary

Functions

For a given set of routes, define a LocalizedHelpers module that implements localized helpers.

Generates rel="alternate" hreflang link tags for a map of locale to URL.

Types

locale_name()

@type locale_name() :: String.t()

url()

@type url() :: String.t()

Functions

define(env, routes, opts \\ [])

For a given set of routes, define a LocalizedHelpers module that implements localized helpers.

hreflang_links(url_map, options \\ [])

@spec hreflang_links(
  %{required(locale_name()) => url()} | term(),
  keyword()
) :: {:ok, Phoenix.HTML.safe()} | {:error, Exception.t()}

Generates rel="alternate" hreflang link tags for a map of locale to URL.

Every locale is validated and rendered in canonical BCP 47 form, and every URL must be fully qualified. A malformed annotation is ignored silently by search engines and agents — the site simply appears not to declare the variant — so an error is returned rather than bad markup emitted.

Arguments

  • url_map is a map of locale to fully-qualified URL, as returned by the generated *_links helpers. The key "x-default" designates the fallback variant and is emitted as-is.

  • options is a keyword list passed to Localize.HTML.Hreflang.links/2.

Returns

  • {:ok, safe} where safe is rendered HTML, or

  • {:error, exception}.

Anything other than a map, such as nil, generates no links.