AnyVal is unboxed in Maps anyway. Also cache escaped translations.

This commit is contained in:
Thibault Duplessis 2017-05-28 11:44:46 +02:00
parent bcb9b0d0d1
commit a27ae18643
3 changed files with 50 additions and 50 deletions

View file

@ -14,49 +14,49 @@ object I18nDb {
val langs = all.keySet
private def playDefaultMessages: Map[MessageKey, Translation] = Map(
"constraint.required" -> Literal("Required"),
"constraint.min" -> Literal("Minimum value: {0}"),
"constraint.max" -> Literal("Maximum value: {0}"),
"constraint.minLength" -> Literal("Minimum length: {0}"),
"constraint.maxLength" -> Literal("Maximum length: {0}"),
"constraint.email" -> Literal("Email"),
"format.date" -> Literal("Date (''{0}'')"),
"format.numeric" -> Literal("Numeric"),
"format.real" -> Literal("Real"),
"format.uuid" -> Literal("UUID"),
"error.invalid" -> Literal("Invalid value"),
"error.invalid.java.util.Date" -> Literal("Invalid date value"),
"error.required" -> Literal("This field is required"),
"error.number" -> Literal("Numeric value expected"),
"error.real" -> Literal("Real number value expected"),
"error.real.precision" -> Literal("Real number value with no more than {0} digit(s) including {1} decimal(s) expected"),
"error.min" -> Literal("Must be greater or equal to {0}"),
"error.min.strict" -> Literal("Must be strictly greater than {0}"),
"error.max" -> Literal("Must be less or equal to {0}"),
"error.max.strict" -> Literal("Must be strictly less than {0}"),
"error.minLength" -> Literal("Minimum length is {0}"),
"error.maxLength" -> Literal("Maximum length is {0}"),
"error.email" -> Literal("Valid email required"),
"error.pattern" -> Literal("Must satisfy {0}"),
"error.date" -> Literal("Valid date required"),
"error.uuid" -> Literal("Valid UUID required"),
"error.expected.date" -> Literal("Date value expected"),
"error.expected.date.isoformat" -> Literal("Iso date value expected"),
"error.expected.time" -> Literal("Time value expected"),
"error.expected.jodadate.format" -> Literal("Joda date value expected"),
"error.expected.jodatime.format" -> Literal("Joda time value expected"),
"error.expected.jsarray" -> Literal("Array value expected"),
"error.expected.jsboolean" -> Literal("Boolean value expected"),
"error.expected.jsnumber" -> Literal("Number value expected"),
"error.expected.jsobject" -> Literal("Object value expected"),
"error.expected.jsstring" -> Literal("String value expected"),
"error.expected.jsnumberorjsstring" -> Literal("String or number expected"),
"error.expected.keypathnode" -> Literal("Node value expected"),
"error.expected.uuid" -> Literal("UUID value expected"),
"error.expected.validenumvalue" -> Literal("Valid enumeration value expected"),
"error.expected.enumstring" -> Literal("String value expected"),
"error.path.empty" -> Literal("Empty path"),
"error.path.missing" -> Literal("Missing path"),
"error.path.result.multiple" -> Literal("Multiple results for the given path")
"constraint.required" -> new Literal("Required"),
"constraint.min" -> new Literal("Minimum value: {0}"),
"constraint.max" -> new Literal("Maximum value: {0}"),
"constraint.minLength" -> new Literal("Minimum length: {0}"),
"constraint.maxLength" -> new Literal("Maximum length: {0}"),
"constraint.email" -> new Literal("Email"),
"format.date" -> new Literal("Date (''{0}'')"),
"format.numeric" -> new Literal("Numeric"),
"format.real" -> new Literal("Real"),
"format.uuid" -> new Literal("UUID"),
"error.invalid" -> new Literal("Invalid value"),
"error.invalid.java.util.Date" -> new Literal("Invalid date value"),
"error.required" -> new Literal("This field is required"),
"error.number" -> new Literal("Numeric value expected"),
"error.real" -> new Literal("Real number value expected"),
"error.real.precision" -> new Literal("Real number value with no more than {0} digit(s) including {1} decimal(s) expected"),
"error.min" -> new Literal("Must be greater or equal to {0}"),
"error.min.strict" -> new Literal("Must be strictly greater than {0}"),
"error.max" -> new Literal("Must be less or equal to {0}"),
"error.max.strict" -> new Literal("Must be strictly less than {0}"),
"error.minLength" -> new Literal("Minimum length is {0}"),
"error.maxLength" -> new Literal("Maximum length is {0}"),
"error.email" -> new Literal("Valid email required"),
"error.pattern" -> new Literal("Must satisfy {0}"),
"error.date" -> new Literal("Valid date required"),
"error.uuid" -> new Literal("Valid UUID required"),
"error.expected.date" -> new Literal("Date value expected"),
"error.expected.date.isoformat" -> new Literal("Iso date value expected"),
"error.expected.time" -> new Literal("Time value expected"),
"error.expected.jodadate.format" -> new Literal("Joda date value expected"),
"error.expected.jodatime.format" -> new Literal("Joda time value expected"),
"error.expected.jsarray" -> new Literal("Array value expected"),
"error.expected.jsboolean" -> new Literal("Boolean value expected"),
"error.expected.jsnumber" -> new Literal("Number value expected"),
"error.expected.jsobject" -> new Literal("Object value expected"),
"error.expected.jsstring" -> new Literal("String value expected"),
"error.expected.jsnumberorjsstring" -> new Literal("String or number expected"),
"error.expected.keypathnode" -> new Literal("Node value expected"),
"error.expected.uuid" -> new Literal("UUID value expected"),
"error.expected.validenumvalue" -> new Literal("Valid enumeration value expected"),
"error.expected.enumstring" -> new Literal("String value expected"),
"error.path.empty" -> new Literal("Empty path"),
"error.path.missing" -> new Literal("Missing path"),
"error.path.result.multiple" -> new Literal("Multiple results for the given path")
)
}

View file

@ -4,11 +4,11 @@ import play.twirl.api.Html
import lila.common.String.html.{ escape => escapeHtml }
private sealed trait Translation extends Any
private sealed trait Translation
private case class Literal(message: String) extends AnyVal with Translation {
private class Literal(message: String) extends Translation {
private def escaped = escapeHtml(message)
lazy val escaped = escapeHtml(message)
def formatTxt(args: Seq[Any]): String =
if (args.isEmpty) message
@ -19,7 +19,7 @@ private case class Literal(message: String) extends AnyVal with Translation {
else Html(escaped.body.format(args.map(_.body): _*))
}
private case class Plurals(messages: Map[I18nQuantity, String]) extends AnyVal with Translation {
private class Plurals(messages: Map[I18nQuantity, String]) extends Translation {
private def messageFor(quantity: I18nQuantity): Option[String] =
messages.get(quantity)

View file

@ -61,12 +61,12 @@ private[i18n] object Registry {
val xml = XML.loadFile(file)
def quote(msg: String) = s"""""\"$msg""\""""
val content = xml.child.collect {
case e if e.label == "string" => s"""(${toKey(e)},Literal(\"\"\"${escape(e.text)}\"\"\"))"""
case e if e.label == "string" => s"""(${toKey(e)},new Literal(\"\"\"${escape(e.text)}\"\"\"))"""
case e if e.label == "plurals" =>
val items = e.child.filter(_.label == "item").map { i =>
s"""${ucfirst(i.\("@quantity").toString)}->\"\"\"${escape(i.text)}\"\"\""""
}
s"""(${toKey(e)},Plurals(Map(${items mkString ","})))"""
s"""(${toKey(e)},new Plurals(Map(${items mkString ","})))"""
}
s"""package lila.i18n
package db