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.
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
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.
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
@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.