This commit is contained in:
parent
c251e1dba3
commit
59f18e1fd2
12 changed files with 1163 additions and 6 deletions
|
|
@ -443,6 +443,7 @@ defmodule MvWeb.CoreComponents do
|
|||
Translates an error message using gettext.
|
||||
"""
|
||||
def translate_error({msg, opts}) do
|
||||
IO.puts("DEBUG: translate_error called with msg: #{inspect(msg)}, opts: #{inspect(opts)}")
|
||||
# When using gettext, we typically pass the strings we want
|
||||
# to translate as a static argument:
|
||||
#
|
||||
|
|
@ -453,11 +454,15 @@ defmodule MvWeb.CoreComponents do
|
|||
# dynamically, so we need to translate them by calling Gettext
|
||||
# with our gettext backend as first argument. Translations are
|
||||
# available in the errors.po file (as we use the "errors" domain).
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(MvWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(MvWeb.Gettext, "errors", msg, opts)
|
||||
end
|
||||
result =
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(MvWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(MvWeb.Gettext, "errors", msg, opts)
|
||||
end
|
||||
|
||||
IO.puts("DEBUG: translate_error result: #{inspect(result)}")
|
||||
result
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
@ -466,4 +471,22 @@ defmodule MvWeb.CoreComponents do
|
|||
def translate_errors(errors, field) when is_list(errors) do
|
||||
for {^field, {msg, opts}} <- errors, do: translate_error({msg, opts})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Translates Backpex strings using gettext.
|
||||
"""
|
||||
def translate_backpex({msg, opts}) do
|
||||
IO.puts("DEBUG: translate_backpex called with msg: #{inspect(msg)}, opts: #{inspect(opts)}")
|
||||
|
||||
# Use our custom translation module
|
||||
result =
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(MvWeb.Gettext, "backpex", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(MvWeb.Gettext, "backpex", msg, opts)
|
||||
end
|
||||
|
||||
IO.puts("DEBUG: translate_backpex result: #{inspect(result)}")
|
||||
result
|
||||
end
|
||||
end
|
||||
|
|
|
|||
72
lib/mv_web/plugs/set_locale.ex
Normal file
72
lib/mv_web/plugs/set_locale.ex
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
defmodule MvWeb.Plugs.SetLocale do
|
||||
@moduledoc """
|
||||
Plug to set the locale for the application.
|
||||
Defaults to German if no locale is specified.
|
||||
"""
|
||||
import Plug.Conn
|
||||
|
||||
@supported_locales ["de", "en"]
|
||||
@default_locale "de"
|
||||
|
||||
def init(_), do: nil
|
||||
|
||||
def call(conn, _) do
|
||||
case fetch_locale(conn) do
|
||||
nil ->
|
||||
# Set default locale if none found
|
||||
Gettext.put_locale(MvWeb.Gettext, @default_locale)
|
||||
persist(@default_locale, conn)
|
||||
|
||||
locale ->
|
||||
# Set and persist locale
|
||||
IO.inspect(locale, label: "locale")
|
||||
|
||||
Gettext.put_locale(MvWeb.Gettext, locale)
|
||||
|> persist(conn)
|
||||
end
|
||||
end
|
||||
|
||||
defp fetch_locale(conn) do
|
||||
conn.params["locale"] ||
|
||||
get_session(conn, :locale) ||
|
||||
parse_accept_language(conn) ||
|
||||
@default_locale
|
||||
end
|
||||
|
||||
defp parse_accept_language(conn) do
|
||||
case get_req_header(conn, "accept-language") do
|
||||
[accept_language | _] ->
|
||||
parse_accept_language_header(accept_language)
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_accept_language_header(accept_language) do
|
||||
accept_language
|
||||
|> String.split(",")
|
||||
|> Enum.map(&parse_language_tag/1)
|
||||
|> Enum.find(&(&1 in @supported_locales))
|
||||
end
|
||||
|
||||
defp parse_language_tag(tag) do
|
||||
tag
|
||||
|> String.trim()
|
||||
|> String.split(";")
|
||||
|> List.first()
|
||||
|> String.split("-")
|
||||
|> List.first()
|
||||
|> String.downcase()
|
||||
end
|
||||
|
||||
defp persist(locale, conn) do
|
||||
IO.inspect(locale, label: "persist_locale")
|
||||
|
||||
# Ensure locale is a string before setting cookie
|
||||
locale_string = to_string(locale)
|
||||
|
||||
put_session(conn, :locale, locale_string)
|
||||
|> put_resp_cookie("locale", locale_string)
|
||||
end
|
||||
end
|
||||
|
|
@ -10,6 +10,7 @@ defmodule MvWeb.Router do
|
|||
plug :protect_from_forgery
|
||||
plug :put_secure_browser_headers
|
||||
plug Backpex.ThemeSelectorPlug
|
||||
plug MvWeb.Plugs.SetLocale
|
||||
end
|
||||
|
||||
pipeline :api do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue