Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with Gettext #35

Open
nikitalocalhost opened this issue Feb 24, 2024 · 1 comment
Open

Integration with Gettext #35

nikitalocalhost opened this issue Feb 24, 2024 · 1 comment

Comments

@nikitalocalhost
Copy link

It there a way to translate error messages with Gettext? I guess relevant function is AbsintheErrorPayload.ChangesetParser.interpolate_message

@andreyuhai
Copy link

Exactly what I was looking for, but I am kind of confused as to how to use it. When you have a validation message like this you can't just directly pass options from ValidationMessage into interpolate_message.

validation =
    %AbsintheErrorPayload.ValidationMessage{
      field: "headline",
      key: "headline",
      code: :max,
      options: [
        %{value: "255", key: :count},
        %{value: "string", key: :type},
        %{value: "max", key: :kind}
      ],
      template: "should be at most %{count} character(s)",
      message: "should be at most 255 character(s)"
    }

ChangesetParser.interpolate_message({validation.template, validation.options})

Also you get the template but it isn't the translated template so you'd need to do something like this currently

validation = Enum.into(error.options, [], & {&1[:key], &1[:value]})
# => [count: "255", type: "string", kind: "max"]

# Then translate the template
template = Gettext.dgettext(MyApp.Gettext, "errors", error.template)

# Finally interpolate
message = ChangesetParser.interpolate_message({template, options})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants