vex icon indicating copy to clipboard operation
vex copied to clipboard

Error message internationalization

Open bruce opened this issue 10 years ago • 8 comments

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

bruce avatar Sep 17 '14 03:09 bruce

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

wafcio avatar Jan 15 '18 23:01 wafcio

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

astery avatar Jan 19 '18 12:01 astery

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

wafcio avatar Jan 19 '18 12:01 wafcio

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

astery avatar Jan 19 '18 12:01 astery

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}

wafcio avatar Jan 19 '18 12:01 wafcio

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

astery avatar Jan 19 '18 13:01 astery

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?

wafcio avatar Jan 19 '18 13:01 wafcio

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.

astery avatar Jan 19 '18 14:01 astery