Ex_cldr - need to call Gettext.put_locale to make it work

Hi all,

my backend module looks like:

defmodule OHA.Cldr do
  @moduledoc false

  @gettext_module Application.compile_env(:oha, :gettext, OHA.Gettext)

  use Cldr,
    locales: ["en", "de"],
    default_locale: "de",
    gettext: @gettext_module,
    data_dir: "./priv/cldr",
    otp_app: :oha,
    precompile_number_formats: ["¤¤#,##0.##"],
    providers: [
      Cldr.Number,
      Cldr.List,
      Cldr.Unit,
      Cldr.Territory,
      Cldr.DateTime,
      Cldr.LocaleDisplay
    ],
    generate_docs: false,
    force_locale_download: false
end

then I have a Phoenix project that uses that backend. The conf where I set the gettext_module looks like this:

config :oha, gettext: ZdbWeb.Gettext

Then I have the plugs for both the conn and the session in my browser pipeline, as I use both dead and live views:

pipeline :browser do
  plug(:accepts, ["html"])
  plug(:fetch_session)

  plug Cldr.Plug.PutLocale,
    apps: [:cldr, :gettext],
    from: [:query, :session, :path, :body, :cookie, :accept_language],
    param: "l",
    gettext: ZdbWeb.Gettext,
    cldr: OHA.Cldr

  plug Cldr.Plug.PutSession, as: :string
  plug(:fetch_live_flash)
  plug(:protect_from_forgery)
  plug(:put_secure_browser_headers)
  plug(:fetch_current_user)
end

Then I have a mount hook where I set the locale from what I have in the params or the session:

defmodule OHAWeb.RestoreLocale do
  def on_mount(:default, %{"l" => locale}, _session, socket) do
    # Gettext.put_locale(locale)
    OHA.Cldr.put_locale(locale)
    {:cont, socket}
  end

  def on_mount(:default, _params, %{"cldr_locale" => locale}, socket) do
    # Gettext.put_locale(locale)
    OHA.Cldr.put_locale(locale)
    {:cont, socket}
  end

  def on_mount(:default, _params, _session, socket), do: {:cont, socket}
end

This code does not work, I have to manually call Gettect.put_locale(locale) to make it work.

I am doing something wrong for sure, ideas?

Thank you
Cheers!

Its a good question @carloratm and it reflects the different environments between the initial HTTP request handled by Cldr.Plug.PutLocale and what happens when the live view is mounted.

The locale set by Cldr.put_locale/1 and Gettext.put_locale/1 are stored in the Process Dictionary which, by definition, is per-process. In the initial HTTP request, Cldr.Plug.PutLocale takes care of this based upon the plug configuration. Since you have configured the plug to set both locales, it does that.

Since each live view is a separate process, Cldr.put_locale/1 and Gettext.put_locale/1 need to be called again in the mount - exactly as you are doing.

Your question might be “why doesn’t Cldr.put_locale/1 also call Gettext.put_locale/1”? My reasoning has been that it would be a little bit too “magic” and that being explicit is better than implicit.

3 Likes

Got it. Thank you for the clear and concise explanation.

While I am here I would like to ask you about one small issue I have.

In the mount handler I have this code

def on_mount(:default, _params, %{"cldr_locale" => locale}, socket) do
  Gettext.put_locale(locale)
  OHA.Cldr.put_locale(locale)
  {:cont, socket}
end

Problem is locale is de-DE which is not working with Gettext.put_locale/1
It’s coming from the cldr_locale cookie.

Do I need to save "de" in the cookie, or is there another solution you are aware of?

Thank you

Good question - I was a bit brief and inaccurate in my answer. The function Cldr.put_gettext_locale/1 takes care of using the LanguageTag to set the Gettext locale appropriately.

I think the right pattern here would be:

def on_mount(:default, _params, %{"cldr_locale" => locale}, socket) do
  Cldr.put_gettext_locale(locale)
  OHA.Cldr.put_locale(locale)
  {:cont, socket}
end

Cldr.put_gettext_locale/1 takes a CLDR locale, extracts the gettext_locale_name field and uses it with Gettext.put_locale/1. Its implementation is very simple:

@spec put_gettext_locale(LanguageTag.t()) :: {:ok, binary() | nil} | {:error, {module(), String.t()}}

def put_gettext_locale(%LanguageTag{gettext_locale_name: nil} = locale) do
  {:error,
    {Cldr.UnknownLocaleError,
      "Locale #{inspect(locale)} does not map to a known gettext locale name"}}
end

def put_gettext_locale(%LanguageTag{gettext_locale_name: gettext_locale_name} = locale) do
  gettext_backend = locale.backend.__cldr__(:gettext)
  _ = Gettext.put_locale(gettext_backend, gettext_locale_name)
  {:ok, gettext_locale_name}
end

In preparing this response I can see that generating a function OHA.Cldr.put_gettext_locale/1 in the CLDR backend would be more consistent and I have pushed a commit to do just that…

1 Like