How to translate ( i18n ) variable value?

Hi,
I Have list of atom statuses = [:pending, :accepted, :rejected] this is used in select tag in Live view HTML. I want to translate it in my language, i am not able to do that using Gettext, is there another way?

Is your question how to provide the labels to the select input or how to handle translation in an elixir system without using gettext?

I am using the above list to set the select tag options (value and text), and i want to translate the text using some kind of library, because I did figure out how to do it using Gettext

It’s not clear to me of you did or did not figure out how to use gettext. But if you didn’t get gettext working, your best option would be to describe what didn’t work for you, using gettext (what did you observe, or which error did you encounter).
Gettext is afaik your best choice for translations, and not very difficult to set up. There are no easier libraries to set up, that I know of.

Unfortunately this did not work for me!

def build_options_list do
statuses = [:pending, :accepted, :rejected]

statuses
|> Enum.map(&Atom.to_string/1)
|> Enum.map(&{&1, dgettext("options", &1)})
end

It throws the following error:

** (ArgumentError) Gettext macros expect translation keys (msgid and msgid_plural),
domains, and comments to expand to strings at compile-time, but the given msgid
doesn't. This is what the macro received:

Could you try to replace this line:

|> Enum.map(&{&1, dgettext("options", &1)})

With something like this:

|> Enum.map(&{Gettext.dgettext(YourApp.Gettext, "options", to_string(&1)), &1})

I think with dynamic values you must explicitly specify your Gettext backend module.

def build_options_list do
  [
    pending: dgettext("options", "pending"),
    accepted: dgettext("options", "accepted"),
    rejected: dgettext("options", "rejected")
  ]
end

This way the strings are compile time known – which allows mix gettext.extract to pick up those strings as something to be translated.

2 Likes

Thank you, but did not work
@LostKobrakai solution worked for me.

@ibarch
@LostKobrakai
Thank you so much for your helps.