Avoiding ex_cldr backend module recompilation

The problem

Every time we run mix gettext.extract it recompiles our app to extract gettext messages (that’s normal) but it also recompiles the cldr backend module which is slow to compiles (more than 10s).

Can we do something to avoid cldr backend module recompilation? I don’t think this should be needed here.

How to reproduce

We can easily reproduce this with a new phoenix app and the last elixir version (1.16.2):

mix phx.new my_app --database sqlite3

Add these deps:

{:ex_cldr, "~> 2.38"},
{:ex_cldr_messages, "~> 1.0"}

Add a backend module:

defmodule MyApp.Cldr do
  use Cldr,
    locales: ["en", "de", "es", "fr", "it", "ja", "ko", "nl", "pt", "zh"],
    default_locale: "en",
    providers: [Cldr.Message]
end

Add this configuration in config.exs:

config :ex_cldr,
  default_backend: MyApp.Cldr

And now every time you run mix gettext.extract you will see:

Compiling 16 files (.ex)
Generating MyApp.Cldr for 11 locales named [:de, :en, :es, :fr, :it, ...] with a default locale named :en

What I’ve tested so far

I know that deps are only compiled once so I’ve made a small experiment: instead of putting our cldr backend module in our app, I’ve put it as local deps.

mix.exs:

{:my_app_cldr, path: "./my_app_cldr"}

config.exs:

config :ex_cldr,
  default_backend: MyAppCldr

Then in the my_app_cldr subfolder, generated using mix new my_app_cldr:

mix.exs:

defp deps do
  [
    {:ex_cldr, "~> 2.38"},
    {:ex_cldr_messages, "~> 1.0"},
  ]
end
defmodule MyAppCldr do
  use Cldr,
    locales: ["en", "de", "es", "fr", "it", "ja", "ko", "nl", "pt", "zh"],
    default_locale: "en",
    providers: [Cldr.Message]
end

And… it seems to work :smile:
MyAppCldr is only compiled once as a deps and then I can use this module in my app but I don’t know if it’s recommended to use cldr this way.

1 Like

Long story short: no, it’s doing a full recompilation of your app: