forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageCompiler.scala
201 lines (179 loc) · 6.29 KB
/
MessageCompiler.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import _root_.java.io.File
import sbt._, Keys._
import scala.io.Source
import scala.xml.XML
object MessageCompiler {
def apply(sourceDir: File, destDir: File, dbs: List[String], compileTo: File): Seq[File] = {
compileTo.mkdirs()
val locales: List[String] = "en-GB" ::
(destDir / "site").list.toList.map { _.takeWhile('.' !=) }.sorted
val localeFiles = locales
.map { locale =>
locale -> writeLocale(locale, sourceDir, destDir, compileTo, dbs)
}
.filter(_._2.exists)
writeRegistry(compileTo, localeFiles.map(_._1)) :: localeFiles.map(_._2)
}
private def writeLocale(
locale: String,
sourceDir: File,
destDir: File,
compileTo: File,
dbs: List[String]
): File = {
val underLocale = locale.replace("-", "_")
val javaFile = compileTo / s"$underLocale.java"
val xmlFiles =
if (locale == "en-GB") dbs.map { db =>
db -> (sourceDir / s"$db.xml")
}
else
dbs.map { db =>
db -> (destDir / db / s"$locale.xml")
}
val isNew = xmlFiles.exists { case (_, file) =>
!isFileEmpty(file) && file.lastModified > javaFile.lastModified
}
if (!isNew) javaFile
else
printToFile(javaFile) {
val puts = xmlFiles flatMap { case (db, file) =>
try {
val xml = XML.loadFile(file)
xml.child.collect {
case e if e.label == "string" =>
val safe = escape(e.text)
val translation = escapeHtmlOption(safe) match {
case None => s"""new Simple(\"$safe\")"""
case Some(escaped) => s"""new Escaped(\"$safe\",\"$escaped\")"""
}
s"""m.put(${toKey(e, db)},$translation);"""
case e if e.label == "plurals" =>
val allItems: Map[String, String] = e.child
.filter(_.label == "item")
.map { i =>
ucfirst(i.\("@quantity").toString) -> s"""\"${escape(i.text)}\""""
}
.toMap
val otherValue = allItems.get("Other")
val default = allItems.head
val items = allItems.filter {
case pair if pair == default => true
case ("Other", v) => v != default._2
case (_, v) if v == default._2 => false
case (_, v) if otherValue.contains(v) => false
case _ => true
}
s"""m.put(${toKey(e, db)},new Plurals(${pluralMap(items)}));"""
}
} catch {
case _: Exception => Nil
}
}
val loadFactor = 0.75
val initialCapacity = (puts.size / loadFactor).toInt + 1
val fullMapImports =
if (puts.exists(_.contains("ScalaRunTime$")))
"""import scala.Predef$;
import scala.Tuple2;
import scala.Tuple2$;
import scala.runtime.ScalaRunTime$;"""
else ""
s"""package lila.i18n;
import java.util.HashMap;
import scala.collection.immutable.Map;
$fullMapImports
public final class $underLocale {
public static final HashMap<String, Translation> load() {
HashMap<String, Translation> m = new HashMap<String, Translation>($initialCapacity, ${loadFactor}f);
${puts mkString "\n"}
return m;
}
}
"""
}
}
private def isFileEmpty(file: File) =
!file.exists() || {
val source = Source.fromFile(file, "UTF-8")
try {
source.getLines.drop(1).next == "<resources></resources>"
} finally {
source.close()
}
}
private def packageName(db: String) = if (db == "class") "clas" else db
private def writeRegistry(destDir: File, locales: Iterable[String]) =
printToFile(destDir / "Registry.scala") {
val content = locales.map { locale =>
s"""Lang("${locale.replace("-", "\",\"")}")->${locale.replace("-", "_")}.load()"""
} mkString ",\n"
s"""package lila.i18n
import play.api.i18n.Lang
// format: OFF
private object Registry {
val all = Map[Lang, MessageMap](\n$content)
val default: MessageMap = all(defaultLang)
val langs: Set[Lang] = all.keys.toSet
}
"""
}
private def ucfirst(str: String) = str(0).toUpper + str.drop(1)
private def toKey(e: scala.xml.Node, db: String) =
if (db == "site") s""""${e.\("@name")}""""
else s""""$db:${e.\("@name")}""""
private val doubleUserBackslashRegex = """(\\.)""".r
private def doubleUserBackslash(str: String) =
doubleUserBackslashRegex.replaceAllIn(
str,
m => (if (Set("""\"""", """\n""")(m.group(1))) "\\" else "\\\\\\") + m.group(1)
)
private def escape(str: String) =
doubleUserBackslash(
str
.replace("\\\"", "\"") // remove \" escaping, which is not always present
.replace("\\'", "'")
.replace("\"", "\\\"") // escape " for sure
.replace("\n", "\\n")
)
private def pluralMap(items: Map[String, String]): String =
if (items.size > 4) {
val mapItems = items.map { case (k, v) =>
"""Tuple2$.MODULE$.apply""" + s"""(I18nQuantity$$.$k, $v)"""
} mkString ","
"""(Map)Predef$.MODULE$.Map().apply(ScalaRunTime$.MODULE$.wrapRefArray(new Tuple2[]{""" + mapItems + """}))"""
} else
s"""new Map.Map${items.size}<I18nQuantity, String>(${items.map { case (k, v) =>
s"I18nQuantity$$.$k,$v"
} mkString ","})"""
private val badChars = """[<>&"'\r\n]""".r.pattern
private def escapeHtmlOption(s: String): Option[String] =
if (badChars.matcher(s).find) Some {
val sb = new java.lang.StringBuilder(s.length + 10) // wet finger style
var i = 0
while (i < s.length) {
s.charAt(i) match {
case '<' => sb append "<"
case '>' => sb append ">"
case '&' => sb append "&"
case '"' => sb append """
case '\'' => sb append "'"
case '\r' => ()
case '\n' => sb append "<br />"
case c => sb append c
}
i += 1
}
sb.toString.replace("\\"", """)
}
else None
private def printToFile(file: File)(content: String): File = {
val p = new java.io.PrintWriter(file, "UTF-8")
try {
p.write(content)
} finally {
p.close()
}
file
}
}