(ArgumentError) *gettext macros expect translation keys (msgid and msgid_plural)

Hello,

I am new to elixir and phoenix.
and I get into compilation error:

== Compilation error in file lib/ex_admin/paginate.ex ==
** (ArgumentError) *gettext macros expect translation keys (msgid and msgid_plural) and
domains to expand to strings at compile-time, but the given msgid
doesn’t.

Dynamic translations should be avoided as they limit gettext’s
ability to extract translations from your source code. If you are
sure you need dynamic lookup, you can use the functions in the Gettext
module:

when I check error lines, it fail on <> operator

text gettext "Displaying" <> " "

if I change the syntax without <> operator it compiles ok.

Please advise what could be wrong
Thanks
Serge

gettext is using the elixir AST to determine if the supplied msgid is just a string or something more involved. In your case the AST for "Displaying" <> " " is no longer just a string, but something which does result in a string when being executed. The execution of it would only happen at runtime so the compiler does not yet know the resulting string to work with and therefore returns an error. One could say that the compiler should optimize away the concat operator in such cases, but it seems that’s just not the case right now.

2 Likes

@LostKobrakai Thanks for the explanation !

@LostKobrakai

gettext is using the elixir AST to determine if the supplied msgid is just a string or something more involved. In your case the AST for "Displaying" <> " " is no longer just a string , but something which does result in a string when being executed. The execution of it would only happen at runtime so the compiler does not yet know the resulting string to work with and therefore returns an error.

How can I then use gettext to translate the text contained in a variable gettext some_variable. I run into Gettext macros expect translation keys (msgid and msgid_plural) as expected but what is the solution if the text I want to translate is inside a variable?

Use the functions in the Gettext module instead of the macros of MyApp.Gettext.

2 Likes

Thank you for the help @LostKobrakai

Can I ask you what are domain-based translations? I don’t understand:

# Simple translation
gettext("Here is one string to translate")

# Plural translation
number_of_apples = 4
ngettext("The apple is ripe", "The apples are ripe", number_of_apples)

# Domain-based translation
dgettext("errors", "Here is an error message to translate")

What is “errors” for in the last example?

1 Like

The domain makes gettext output a errors.pot, where you can have different translations to default.pot even for the same translatable string.

2 Likes