What are the i18n message ids for field infos and

2019-03-02 23:57发布

I'd like to translate the default constraints and error messages for markup generated by the views.html.helpers._ inputs. I couldn't find it in the documentation, and have already started browsing the code, but if anyone is faster than me please answer and be awarded.

Here is the sample template code:

@inputText(regForm("Login"), 'id-> "username", 'placeholder -> "Login", 'required -> "yes", 'autofocus -> "yes")

And here is the generated HTML:

<div id="username_field" class="error clearfix">
<div class="input">

<input type="text" id="username" name="Login" value="" id="username" placeholder="Login" required="yes" autofocus="yes">

  <span class="help-inline">This field is required</span>
  <span class="help-block">Required</span>
</div>

I'd like to translate the texts appearing in the two last spans.

Edit: I already know how the translation works from the documentation. What is not stated there are the default message IDs for the messages shown where a field constraint isn't fulfilled (the error messages) and for general info.

2条回答
Fickle 薄情
2楼-- · 2019-03-03 00:41

Browsing the code I found where it is documented. The keys are shortly mentioned in the Constraints trait documentation. You have to unfold a definition of a constraint generator to read it. How to use constraints is covered in the forms handling documentation. But there is another way not covered there - by using an input attribute like here:

@inputText(regForm("Login"), 'id-> "username", 'placeholder -> "Login", 'required -> "yes", 'autofocus -> "yes")

Here are the IDs implemented in the /Play20/framework/src/play/src/main/scala/play/api/data/validation/Validation.scala (master branch):

  • nonEmpty (i.e. a field with 'required attribute)
    • infos: constraint.required
    • errors: error.required
  • min (i.e. a field with 'min attribute; a message with one parameter)
    • infos: constraint.min(minValue)
    • errors: error.min(minValue)
  • max (i.e. a field with 'max attribute;a message with one parameter)
    • infos: constraint.max(maxValue)
    • errors: error.max(maxValue)
  • minLength (i.e. a field with 'minLength attribute)
    • infos: constraint.minLength(length)
    • errors: error.minLength(length)
  • maxLength (i.e. a field with 'maxLength attribute)
    • infos: constraint.maxLength(length)
    • errors: error.maxLength(length)
  • pattern (i.e. a field with 'regex attribute)
    • infos: constraint.pattern(regex)
    • errors: error.pattern(regex)

The attributes mentioned above are introduced in HTML5, so they won't be handled by all browsers, but the framework validation will handle it.

查看更多
别忘想泡老子
3楼-- · 2019-03-03 00:50

You have to override labels from the source messages file in your own messages.xy files.

Also take a look at the other answer some time ago there was a problem if file for default language hasn't the lang extension. AFAIK it was fixed after that answer, however it would be cool if you'll check it and confirm the current state in the comment.

查看更多
登录 后发表回答