Multilingual.Routes (multilingual v0.1.5)

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 metadata for multilingual routes.

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

Functions

Link to this function

build_page_mapping(router, 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

> Multilingual.Routes.build_page_mapping(MyAppWeb.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 = Multilingual.View.fetch_key(@conn, :locale) %>
<% path = Multilingual.View.fetch_key(@conn, :path) %>
<% {:ok, mapping} = Multilingual.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>
Link to this function

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

> Multilingual.Routes.localized_path(MyAppWeb.Router, "/about", "it")
"/it/chi-siamo"
Link to this function

metadata(locale)

Creates metadata for multilingual routes.

Examples

iex> Multilingual.Routes.metadata("it")
[metadata: %{multilingual: %{locale: "it"}}]
Link to this function

path_locale(router, path)

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