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

Error message internationalization #2

Open
bruce opened this issue Sep 17, 2014 · 8 comments
Open

Error message internationalization #2

bruce opened this issue Sep 17, 2014 · 8 comments

Comments

@bruce
Copy link
Contributor

bruce commented Sep 17, 2014

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

@wafcio
Copy link

wafcio commented Jan 15, 2018

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

@astery
Copy link
Contributor

astery commented Jan 19, 2018

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

@wafcio
Copy link

wafcio commented Jan 19, 2018

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

@astery
Copy link
Contributor

astery commented Jan 19, 2018

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

@wafcio
Copy link

wafcio commented Jan 19, 2018

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
Copy link
Contributor

astery commented Jan 19, 2018

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

@wafcio
Copy link

wafcio commented Jan 19, 2018

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
Copy link
Contributor

astery commented Jan 19, 2018

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.

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

No branches or pull requests

3 participants