Hello there, I’m trying to localize my app using Gettext. I extracted and merged the POT files and it works fine for the page.index.html.ex
however when I navigate to any other route in my app the locale returns to English again, or I have to append ?locale=de
and the end of the url to work fine.
Here is my config:
# Config Locales
config :lean, LeanWeb.Gettext, locales: ~w(en de es), default_locale: "en"
and I wrote this plug:
defmodule LeanWeb.Plugs.SetLocale do
import Plug.Conn
@supported_locales Gettext.known_locales(LeanWeb.Gettext)
def init(_options), do: nil
def call(%Plug.Conn{params: %{"locale" => locale}} = conn, _options) when locale in @supported_locales do
case fetch_locale_from(conn) do
nil ->
conn
locale ->
LeanWeb.Gettext |> Gettext.put_locale(locale)
conn |> put_resp_cookie "locale", locale, max_age: 365*24*60*60
end
end
defp fetch_locale_from(conn) do
(conn.params["locale"] || conn.cookies["locale"]) |>
check_locale
end
defp check_locale(locale) when locale in @supported_locales, do: locale
defp check_locale(_), do: nil
def call(conn, _options), do: conn
end
and added it to the :browser
pipeline
pipeline :browser do
plug :accepts, ["html"]
plug LeanWeb.Plugs.SetLocale
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
Can you help me with this?
Thanks in advance!