Packages

Simplify handling localized routes in Elixir Phoenix applications, with and without LiveView

Current section

12 Versions

Jump to

Compare versions

8 files changed
+122 additions
-52 deletions
  @@ -1,4 +1,8 @@
1 1 # Used by "mix format"
2 2 [
3 - inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3 + inputs: [
4 + "{mix,.formatter}.exs",
5 + "{config,lib}/**/*.{ex,exs}",
6 + "test/multilingual/**/*.{ex,exs}"
7 + ]
4 8 ]
  @@ -1,6 +1,6 @@
1 1 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/joeyates/multilingual">>}]}.
2 2 {<<"name">>,<<"multilingual">>}.
3 - {<<"version">>,<<"0.1.8">>}.
3 + {<<"version">>,<<"0.1.9">>}.
4 4 {<<"description">>,
5 5 <<"Simplify handling localized routes in Elixir Phoenix applications, with and without LiveView">>}.
6 6 {<<"elixir">>,<<"~> 1.17">>}.
  @@ -26,11 +26,10 @@ if Code.ensure_loaded?(Phoenix.Component) do
26 26 <link rel="alternate" hreflang="it" href="https://example.com/it/chi-siamo">
27 27 """
28 28 def get_rel_links(%Plug.Conn{} = conn) do
29 - router = Phoenix.Controller.router_module(conn)
30 - path = View.fetch_key(conn, :path)
29 + path = View.fetch_key(conn, :route)
31 30 locale = View.fetch_key(conn, :locale)
32 31
33 - case Routes.build_page_mapping(router, path) do
32 + case Routes.build_page_mapping(conn, path) do
34 33 {:ok, mapping} ->
35 34 rels = build_rels(conn, locale, mapping)
36 35 rel_links(%{rels: rels})
  @@ -26,8 +26,9 @@ if Code.ensure_loaded?(Phoenix.LiveView) do
26 26 socket
27 27 |> attach_hook(:multilingual_store_view, :handle_params, fn _params, uri, socket ->
28 28 uri = URI.parse(uri)
29 - locale = Routes.path_locale(socket.router, uri.path) || default_locale
30 - view = %View{path: uri.path, locale: locale}
29 + info = Phoenix.Router.route_info(socket.router, "GET", uri.path, nil)
30 + locale = Routes.path_locale(socket.router, info.route) || default_locale
31 + view = %View{route: info.route, locale: locale}
31 32 socket = put_private(socket, :multilingual, view)
32 33 {:cont, socket}
33 34 end)
  @@ -31,7 +31,8 @@ defmodule Multilingual.Plugs.StoreView do
31 31 def call(conn, opts) do
32 32 path = conn.request_path
33 33 router = Phoenix.Controller.router_module(conn)
34 - locale = Routes.path_locale(router, path) || opts.default_locale
35 - put_private(conn, :multilingual, %View{path: path, locale: locale})
34 + info = Phoenix.Router.route_info(router, "GET", path, nil)
35 + locale = Routes.path_locale(router, info.route) || opts.default_locale
36 + put_private(conn, :multilingual, %View{route: info.route, locale: locale})
36 37 end
37 38 end
Loading more files…