PhoenixMultilingual.Routes (phoenix_multilingual v0.1.12)

Summary

Functions

Builds a mapping of locales to paths for the current page.

Returns the equivalent localized path for the given path and locale.

Creates locale metadata for multilingual routes.

Creates locale and view metadata for multilingual routes.

Returns the locale from the metadata of the route which provides the requested path.

Functions

build_page_mapping(conn, path)

Builds a mapping of locales to paths for the current page.

Examples

In the router:

scope "/", MyAppWeb do
  get "/about", PageController, :index, metadata("en")
  get "/it/chi-siamo", PageController, :index, metadata("it")
end

> PhoenixMultilingual.Routes.build_page_mapping(Router, "/about")
{:ok, %{"en" => "/about", "it" => "/it/chi-siamo"}}

The result can be used to create a language switcher in the view.

<% locales = ["en", "it"] %>
<% locale = PhoenixMultilingual.View.fetch_key(@conn, :locale) %>
<% path = PhoenixMultilingual.View.fetch_key(@conn, :route) %>
<% {:ok, mapping} = PhoenixMultilingual.Routes.build_page_mapping(@conn, path) %>
<nav>
  <ul>
    <%= for lcl <- locales do %>
      <%= if lcl == locale do %>
        <li><%= lcl %></li>
      <% else %>
        <%= if mapping[lcl] do %>
          <li><a href={mapping[lcl]}><%= lcl %></a></li>
        <% end %>
      <% end %>
    <% end %>
  </ul>
</nav>

locale(route)

localized_path(router, path, locale)

Returns the equivalent localized path for the given path and locale.

If the path is not found, it returns nil.

Examples

In the router:

scope "/", MyAppWeb do
  get "/about", PageController, :index, metadata("en")
  get "/it/chi-siamo", PageController, :index, metadata("it")
end

> PhoenixMultilingual.Routes.localized_path(MyAppWeb.Router, "/about", "it")
"/it/chi-siamo"

metadata(locale)

Creates locale metadata for multilingual routes.

Examples

iex> PhoenixMultilingual.Routes.metadata("it")
[metadata: %{multilingual: %{locale: "it"}}]

metadata(view, locale)

Creates locale and view metadata for multilingual routes.

Examples

iex> PhoenixMultilingual.Routes.metadata(:about, "it")
[metadata: %{multilingual: %{view: :about, locale: "it"}}]

path_locale(router, path)

Returns the locale from the metadata of the route which provides the requested path.

view(route)