How to internationalize form constraints in Play 2.5?

94 views Asked by At

I would like to internationalize the form errors returned by Play 2.5 (Scala), when for example the length of field submitted by the user is 2 and the requirement is 3, I get the following error in Firefox in English: "Minimum length is 3". (The project uses French language and other parts of the site display well in French by using the conf/messages.fr file).

import play.api.data.Form
import play.api.data.Forms.{ mapping, text }

case class NewsData(title: String, rawHTML: String)

object AllForms {
  val newsForm: Form[NewsData] = Form {
  mapping(
    "title" -> text(minLength = 3, maxLength = 255),
    "rawHTML" -> text(minLength = 3, maxLength = 19999)
  )(NewsData.apply)(NewsData.unapply)
  }
}

I have found in the source code of Play that it uses internally the following message: error.min in Play 2.5 Validation.scala but putting it in the conf/messages.fr doesn't work.

What would be the proper way to localize this?

1

There are 1 answers

0
Matthias A. Eckhart On BEST ANSWER

You are using the wrong key.

The key error.min corresponds to Must be greater or equal to {0}. You should use error.minLength instead, which corresponds to Minimum length is {0}.