Trying to get the translated text from a dependency app

I have a local app A which is a dependency of a Phoenix project B.

A has translations, from A iex:

iex(1)> import A.Gettext
A.Gettext
iex(2)> Gettext.put_locale(A.Gettext, "de")
nil
iex(3)> dgettext("a", "Log in")
"Einloggen"

If I try to get the same translation from B, I got the wrong locale:

iex(4)> import A.Gettext
A.Gettext
iex(5)> Gettext.put_locale(A.Gettext, "de")
nil
iex(6)> dgettext("a", "Log in")
"Log in"

What am I missing here?
Thank you

Two ideas to investigate come to my mind

  1. Your priv/ translations directory is maybe being compiled to a non-existent path when you load the local dependency from B, or not being compiled at all. Try Gettext.known_locales(A.Gettext) from B and if it returns [] then it can’t find your translations directory.

  2. If you have any gettext config in a/config/*.exs then it is not available in B and you’ll have to set that config in b/config/*.exs as well.

I had a little time to test this out. If you are pulling in A as a local path dependency then your priv translations directory should be compiled along with the dependency into b/_build/dev/lib/a/priv/path_to_locales/. If it’s not there, then my theory is that you compiled your PO files in A (mix compile.gettext) after the last compilation of A as a dependency in B. Running mix do deps.clean a, deps.update a, deps.compile from the B directory should fix that, and then it all should work.

You probably need to use put_locale("de") rather than put_locale(A.Gettext, "de"), which will set the locale for all Gettext backends in the current process, rather than just the one from A: Gettext — gettext v0.19.1

1 Like

That is indeed the reason it was not working.
Thank you very much