|
| 1 | +defmodule TeslaMateWeb.Plugs.Locale do |
| 2 | + @moduledoc """ |
| 3 | + Strict locale sources for `Localize.Plug.PutLocale`, plus the plug that |
| 4 | + applies the negotiated locale to Gettext and the session. |
| 5 | +
|
| 6 | + `Localize.validate_locale/1` matches at the default CLDR distance (80), |
| 7 | + which never fails for a syntactically valid tag: an unrelated language |
| 8 | + best-matches onto the *first* supported locale as a last resort, so |
| 9 | + `Accept-Language: ru` would come back Catalan instead of falling |
| 10 | + through to the default. The sources here match strictly — distance 79 |
| 11 | + keeps CLDR's genuine related-language fallbacks (such as `nn` -> `nb`) |
| 12 | + but rejects the unrelated-language bucket — and return `nil` on no |
| 13 | + match, so the plug moves on to the next source and ultimately the |
| 14 | + configured default. Accept-Language entries are tried per tag in |
| 15 | + quality order, so an unsupported primary language still falls through |
| 16 | + to a supported secondary preference. |
| 17 | +
|
| 18 | + The session stores the Gettext locale name under `"gettext_locale"`, |
| 19 | + the same contract the pre-Localize `PutSession` plug used: sessions |
| 20 | + written before the migration stay valid, and the LiveView `on_mount` |
| 21 | + hook remains a plain `Gettext.put_locale/2` without re-running the |
| 22 | + negotiation. |
| 23 | + """ |
| 24 | + |
| 25 | + import Plug.Conn |
| 26 | + |
| 27 | + @behaviour Plug |
| 28 | + |
| 29 | + @session_key "gettext_locale" |
| 30 | + @strict_distance Localize.LanguageTag.default_distance() - 1 |
| 31 | + |
| 32 | + # The supported CLDR ids map 1:1 onto the Gettext locale names |
| 33 | + # (`priv/gettext/*`); `locale_test.exs` asserts the two sets stay |
| 34 | + # in sync. |
| 35 | + @gettext_locales Map.new( |
| 36 | + Application.compile_env!(:localize, :supported_locales), |
| 37 | + &{&1, &1 |> Atom.to_string() |> String.replace("-", "_")} |
| 38 | + ) |
| 39 | + @cldr_locales Map.new(@gettext_locales, fn {cldr, gettext} -> {gettext, cldr} end) |
| 40 | + |
| 41 | + @doc false |
| 42 | + def session_key, do: @session_key |
| 43 | + |
| 44 | + @doc false |
| 45 | + def gettext_locales, do: Map.values(@gettext_locales) |
| 46 | + |
| 47 | + ## Locale sources for Localize.Plug.PutLocale |
| 48 | + |
| 49 | + @doc false |
| 50 | + def from_query(%Plug.Conn{query_params: %Plug.Conn.Unfetched{}} = conn, options) do |
| 51 | + from_query(fetch_query_params(conn), options) |
| 52 | + end |
| 53 | + |
| 54 | + def from_query(conn, _options) do |
| 55 | + strict_match(conn.query_params["locale"]) |
| 56 | + end |
| 57 | + |
| 58 | + @doc false |
| 59 | + def from_session(conn, _options) do |
| 60 | + strict_match(get_session(conn, @session_key)) |
| 61 | + end |
| 62 | + |
| 63 | + @doc false |
| 64 | + def from_accept_language(conn, _options) do |
| 65 | + case get_req_header(conn, "accept-language") do |
| 66 | + [header | _] -> |
| 67 | + header |
| 68 | + |> Localize.AcceptLanguage.tokenize() |
| 69 | + |> Enum.find_value(fn {_quality, tag} -> strict_match(tag) end) |
| 70 | + |
| 71 | + [] -> |
| 72 | + nil |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # Exact Gettext locale names (session values, `?locale=` from the |
| 77 | + # settings UI) resolve through the compile-time map without any tag |
| 78 | + # matching; everything else pays one strict `best_match` against the |
| 79 | + # supported list. |
| 80 | + defp strict_match(nil), do: nil |
| 81 | + |
| 82 | + defp strict_match(locale) when is_binary(locale) do |
| 83 | + case Map.fetch(@cldr_locales, locale) do |
| 84 | + {:ok, cldr_id} -> |
| 85 | + validated(cldr_id) |
| 86 | + |
| 87 | + :error -> |
| 88 | + case Localize.LanguageTag.best_match( |
| 89 | + locale, |
| 90 | + Localize.supported_locales(), |
| 91 | + @strict_distance |
| 92 | + ) do |
| 93 | + {:ok, cldr_id, _score} -> validated(cldr_id) |
| 94 | + {:error, _reason} -> nil |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + defp validated(cldr_id) do |
| 100 | + case Localize.validate_locale(cldr_id) do |
| 101 | + {:ok, _language_tag} = ok -> ok |
| 102 | + {:error, _reason} -> nil |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + ## Plug: apply the locale Localize.Plug.PutLocale resolved |
| 107 | + |
| 108 | + @impl Plug |
| 109 | + def init(options), do: options |
| 110 | + |
| 111 | + @impl Plug |
| 112 | + def call(conn, _options) do |
| 113 | + case Localize.Plug.PutLocale.get_locale(conn) do |
| 114 | + %Localize.LanguageTag{cldr_locale_id: cldr_id} when is_map_key(@gettext_locales, cldr_id) -> |
| 115 | + locale = Map.fetch!(@gettext_locales, cldr_id) |
| 116 | + Gettext.put_locale(TeslaMateWeb.Gettext, locale) |
| 117 | + |
| 118 | + # :html_lang, not :locale — LiveView merges its assigns (where |
| 119 | + # :locale is the Gettext name, e.g. "zh_Hans") into the conn |
| 120 | + # assigns on the dead render and would shadow this one. |
| 121 | + conn |
| 122 | + |> put_session(@session_key, locale) |
| 123 | + |> assign(:html_lang, Atom.to_string(cldr_id)) |
| 124 | + |
| 125 | + _other -> |
| 126 | + conn |
| 127 | + end |
| 128 | + end |
| 129 | +end |
0 commit comments