View Source Cldr.Route.LocalizedHelpers (Cldr Routes v0.6.2)

Generates a module that implements localised helpers.

It introspects the generated helpers module and creates a wrapper function that translates (at compile time) the path segments.

Link to this section Summary

Functions

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

Generates HTML link tags for a given map of locale => URLs

Link to this section Types

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

Link to this section Functions

Link to this function

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

View Source

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

Link to this function

helper_by_locale(routes)

View Source
@spec hreflang_links(%{required(locale_name()) => url()}) :: Phoenix.HTML.safe()

Generates HTML link tags for a given map of locale => URLs

This function generates <link ... /> tags that should be placed in the <head> section of an HTML document to indicate the different language versions of a given page.

The MyApp.Router.LocalizedHelpers.<helper>_link functions can generate the required mapping from locale to URL for a given helper. These _link helpers take the same arguments as the _path and _url helpers.

If the helper refers to a route that is not localized then an empty string will be returned since there are no alternative localizations of this route.

See https://developers.google.com/search/docs/advanced/crawling/localized-versions#http

examples

Examples

iex> links = %{
...>   "en" => "https://localhost/users/1",
...>   "fr" => "https://localhost/utilisateurs/1"
...>  }
iex> Cldr.Route.LocalizedHelpers.hreflang_links(links)
{
  :safe,
  [
    [60, "link", [32, "href", 61, 34, "https://localhost/users/1", 34, 32, "hreflang", 61, 34, "en", 34, 32, "rel", 61, 34, "alternate", 34], 62],
    10,
    [60, "link", [32, "href", 61, 34, "https://localhost/utilisateurs/1", 34, 32, "hreflang", 61, 34, "fr", 34, 32, "rel", 61, 34, "alternate", 34], 62]
  ]
}

iex> Cldr.Route.LocalizedHelpers.hreflang_links(nil)
{:safe, []}

iex> Cldr.Route.LocalizedHelpers.hreflang_links(%{})
{:safe, []}