Is there a way to see what version of a depency is loaded?

Gettext released a new version (0.26) that breaks compatibiliy, the way you need to add/use Gettext has been changed.

While it isn’t a problem for my project to make the necessary changes, it also breaks compilation for Timex.

Now I was wondering how one can distinguish in library code what version of Gettext is loaded, so the correct import vs use can be compiled.

1 Like
if Version.match?(to_string(Application.spec(:app, :vsn)), ">= x.y.z") do
1 Like

you can check the version of a dependency like Gettext that’s loaded at runtime using the Application.spec/2 function. Here’s how you can do it:

defmodule VersionCheck do
  def gettext_version do
    case Application.spec(:gettext, :vsn) do
      '0.26.0' -> :new_version
      _ -> :old_version
    end
  end
end

This function retrieves the version of Gettext and allows you to distinguish between different versions, enabling you to conditionally apply the necessary changes in your code.

2 Likes

So you can do something like

if Version.match?(to_string(Application.spec(:gettet, :vsn), ">= 0.26") do
  use Gettext.Backend, otp_app: :my_app
else
  use Gettext, otp_app: :my_app
end

and then

if Version.match?(to_string(Application.spec(:gettet, :vsn), ">= 0.26") do
  use Gettext, backend: MyApp.Gettext
else
  import MyApp.Gettext
end
1 Like

FYI gettext 0.26.1 was released a few minutes ago with a release note of “Address backwards incompatible changes in previous release” and it fixes the Timex compilation issue in my (light) testing.

1 Like

Yes I saw it too.

Although I must admit that I took the opportunity to remove Timex from my project and replace it with Elixir native functions and modules.

The only thing missing was Timex.between? and that was easily resolved.

4 Likes

How does this work with ex_cldr?

defmodule MyApp.Cldr do
  use Cldr,
    default_locale: "en",
    locales: ["fr", "en", "bs", "si", "ak", "th"],
    add_fallback_locales: false,
    gettext: MyApp.Gettext,
    data_dir: "./priv/cldr",
    otp_app: :my_app,
    precompile_number_formats: ["¤¤#,##0.##"],
    precompile_transliterations: [{:latn, :arab}, {:thai, :latn}],
    providers: [Cldr.Number],
    generate_docs: true,
    force_locale_download: false
end

Taken from:

https://hexdocs.pm/ex_cldr/readme.html#backend-module-configuration

I’ve followed gettext’s instructions but then run into issues with (the amazing) ex_cldr.

Generating MyApp.Cldr for 3 locales named [:en, :sv, :und] with a default locale named :en
     error: undefined function gettext/1 (expected MyAppWeb.Layouts to define such a function or for it to be imported, but none are available)
     │
 157 │         <%= gettext("Demo") %>
     │             ^^^^^^^
     │
     └─ lib/my_app_web/components/layouts/app.html.heex:157:13: MyAppWeb.Layouts.app/1

Maybe this is apparent to the expert on the subject @kip :slightly_smiling_face:

@carlgleisner, you’ll still need to bring gettext/1 into scope by importing it - the message suggests it isn’t imported into that module. ie I don’t think the error is coming from ex_cldr in this case (but hey, wouldn’t be the first time I’ve been wrong!).

Somewhere you’ve got a module MyApp.Gettext. Then in MyAppWeb.Layouts there should be an import for MyApp.Gettext module I believe.

(thanks for the kind words)

2 Likes