CargoSense / vex

Data Validation for Elixir
MIT License
595 stars 60 forks source link

Error message internationalization #2

Open bruce opened 10 years ago

bruce commented 10 years ago

Probably using: https://github.com/chrismccord/linguist

wafcio commented 6 years ago

@bruce I have started using vex, and it will be good to have internationalizatio, do you have any plans about it?

astery commented 6 years ago

@wafcio, this should be helpful - https://github.com/CargoSense/vex#error-message-renderers

wafcio commented 6 years ago

unfortunatelly it isn't because you have injected counters and different variables to error message, which make impossible to create translation for them.

astery commented 6 years ago

I don't understand the trouble. Can you write an example for me?

wafcio commented 6 years ago

when I checked length validator I received must have a length of at least 2 string instead of must have a length of at least %{count}

astery commented 6 years ago

I don't see why it can't be achieved. Can you drop the code snippet and point the current and expected behavior?

wafcio commented 6 years ago

What if you have different lenght in different places like min: 2, min: 4 or you change 2 to 3 for example, then you need to fill new translation key. Don't you think that it will be better to have %{count} like it is resolved in ecto?

astery commented 6 years ago

Sorry, I don't really get what you mean by "need to fill new translation key". Some examples of code would definitely help here.

Anyway I think this link I posted above still would be useful, because with error renderer you can create any error that you need.

If you need no translation or no interpolation, and want to receive original message like ecto changeset does: {"should be at least %{count} characters", [count: 3, validation: :length, min: 3]}, try write new error renderer.

defmodule MyRenderer do
  @behaviour Vex.ErrorRenderer

  def message(options, _default, context \\ []), do: {options[:message], context}
end

result = Vex.validate([name: "Foo"], name: [
  length: [
    min: 4,
    error_renderer: MyRenderer,
    message: "too short, min %{min} chars"
  ]
])
assert {:error, [{:error, :name, :length, {"too short, min %{min} chars", [min: 4, ...]}}]} = result

I'm not fully sure is it target your issues, and if not, please, drop some snippet of code.