lila/project/MessageCompiler.scala

89 lines
2.6 KiB
Scala
Raw Normal View History

2016-03-29 00:29:32 -06:00
import _root_.java.io.File
import sbt._, Keys._
2017-05-27 03:02:14 -06:00
import scala.xml.XML
2016-03-29 00:29:32 -06:00
object MessageCompiler {
2017-05-25 07:11:42 -06:00
def apply(sourceFile: File, destDir: File, compileTo: File): Seq[File] = {
val startsAt = System.currentTimeMillis()
2017-05-26 06:12:04 -06:00
val registry = ("en-GB" -> sourceFile) :: destDir.list.toList.map { f =>
2017-05-25 07:11:42 -06:00
f.takeWhile('.' !=) -> (destDir / f)
2017-05-26 07:53:11 -06:00
}.sortBy(_._1)
2017-05-25 07:11:42 -06:00
compileTo.mkdirs()
val registryFile = writeRegistry(compileTo, registry)
val res = for (entry <- registry) yield {
val (locale, file) = entry
2017-05-25 07:11:42 -06:00
val compileToFile = compileTo / s"$locale.scala"
if (file.lastModified > compileToFile.lastModified) {
printToFile(compileToFile) {
2017-05-27 03:02:14 -06:00
render(locale, file)
2016-03-31 01:00:31 -06:00
}
}
2017-05-25 07:11:42 -06:00
compileToFile
2016-03-29 00:29:32 -06:00
}
println(s"MessageCompiler took ${System.currentTimeMillis() - startsAt}ms")
registryFile :: res
}
2017-05-25 07:11:42 -06:00
private def writeRegistry(compileTo: File, registry: List[(String, File)]) = {
val file = compileTo / "Registry.scala"
printToFile(file) {
val content = registry.map {
2017-05-26 07:53:11 -06:00
case (locale, _) => s"""Lang("${locale.replace("-", "\",\"")}")->`$locale`.load"""
} mkString ",\n"
s"""package lila.i18n
package db
2017-05-26 06:12:04 -06:00
import play.api.i18n.Lang
2017-02-14 08:19:01 -07:00
// format: OFF
2017-05-27 05:25:24 -06:00
private[i18n] object Registry {
2017-05-27 02:02:28 -06:00
def load = Map[Lang, Map[MessageKey, Translation]]($content)
}
"""
}
file
}
2017-05-27 03:02:14 -06:00
private def ucfirst(str: String) = str(0).toUpper + str.drop(1)
private def toKey(e: scala.xml.Node) = s""""${e.\("@name")}""""
2017-05-27 15:02:49 -06:00
private def escape(str: String) = {
// is someone trying to inject scala code?
if (str contains "\"\"\"") sys error s"Skipped translation: $str"
2017-05-27 15:02:49 -06:00
// crowdin escapes ' and " with \, and encodes &. We'll do it at runtime instead.
else str.replace("\\'", "'").replace("\\\"", "\"")
}
2017-05-27 03:02:14 -06:00
private def render(locale: String, file: File) = {
val xml = XML.loadFile(file)
def quote(msg: String) = s"""""\"$msg""\""""
2017-05-27 03:02:14 -06:00
val content = xml.child.collect {
case e if e.label == "string" => s"""(${toKey(e)},new Literal(\"\"\"${escape(e.text)}\"\"\"))"""
2017-05-27 03:02:14 -06:00
case e if e.label == "plurals" =>
val items = e.child.filter(_.label == "item").map { i =>
s"""${ucfirst(i.\("@quantity").toString)}->\"\"\"${escape(i.text)}\"\"\""""
2017-05-27 03:02:14 -06:00
}
s"""(${toKey(e)},new Plurals(Map(${items mkString ","})))"""
2017-05-27 03:02:14 -06:00
}
s"""package lila.i18n
package db
2017-05-27 03:02:14 -06:00
import I18nQuantity._
2017-02-14 08:19:01 -07:00
// format: OFF
private object `$locale` {
2017-05-27 03:02:14 -06:00
def load = Map[MessageKey, Translation](\n${content mkString ",\n"})
}
"""
}
private def printToFile(f: File)(content: String): Unit = {
2017-03-21 08:33:42 -06:00
val p = new java.io.PrintWriter(f, "UTF-8")
try { content.foreach(p.print) } finally { p.close() }
2016-03-29 00:29:32 -06:00
}
}