2016-03-29 00:29:32 -06:00
|
|
|
import _root_.java.io.File
|
2017-05-25 06:57:03 -06:00
|
|
|
import _root_.java.nio.file.{Files, StandardCopyOption}
|
2016-03-29 00:29:32 -06:00
|
|
|
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] = {
|
2016-03-29 03:24:29 -06:00
|
|
|
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)
|
2016-03-29 03:24:29 -06:00
|
|
|
val res = for (entry <- registry) yield {
|
2017-05-25 06:57:03 -06:00
|
|
|
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
|
|
|
}
|
2016-03-29 03:24:29 -06:00
|
|
|
}
|
2017-05-25 07:11:42 -06:00
|
|
|
compileToFile
|
2016-03-29 00:29:32 -06:00
|
|
|
}
|
2016-03-29 03:24:29 -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"
|
2016-03-29 03:24:29 -06:00
|
|
|
printToFile(file) {
|
|
|
|
val content = registry.map {
|
2017-05-26 07:53:11 -06:00
|
|
|
case (locale, _) => s"""Lang("${locale.replace("-", "\",\"")}")->`$locale`.load"""
|
2016-03-29 03:24:29 -06:00
|
|
|
} 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 {
|
2016-03-29 03:24:29 -06:00
|
|
|
|
2017-05-27 02:02:28 -06:00
|
|
|
def load = Map[Lang, Map[MessageKey, Translation]]($content)
|
2016-03-29 03:24:29 -06:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
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")}""""
|
|
|
|
|
|
|
|
private def render(locale: String, file: File) = {
|
|
|
|
val xml = XML.loadFile(file)
|
2016-03-29 03:24:29 -06:00
|
|
|
def quote(msg: String) = s"""""\"$msg""\""""
|
2017-05-27 03:02:14 -06:00
|
|
|
val content = xml.child.collect {
|
2017-05-27 03:04:15 -06:00
|
|
|
case e if e.label == "string" => s"""${toKey(e)}->Literal(\"\"\"${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)}->\"\"\"${i.text}\"\"\""""
|
|
|
|
}
|
|
|
|
s"""${toKey(e)}->Plurals(Map(${items mkString ","}))"""
|
|
|
|
}
|
2016-03-29 03:24:29 -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
|
2017-05-25 06:57:03 -06:00
|
|
|
private object `$locale` {
|
2016-03-29 03:24:29 -06:00
|
|
|
|
2017-05-27 03:02:14 -06:00
|
|
|
def load = Map[MessageKey, Translation](\n${content mkString ",\n"})
|
2016-03-29 03:24:29 -06:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2016-03-29 03:24:29 -06:00
|
|
|
try { content.foreach(p.print) } finally { p.close() }
|
2016-03-29 00:29:32 -06:00
|
|
|
}
|
|
|
|
}
|