forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsDump.scala
61 lines (50 loc) · 1.51 KB
/
JsDump.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
package lila.i18n
import java.io._
import scala.concurrent.Future
import play.api.i18n.Lang
import play.api.libs.json.{ JsString, JsObject }
private[i18n] final class JsDump(
path: String,
pool: I18nPool,
keys: I18nKeys
) {
def keysToObject(keys: Seq[I18nKey], lang: Lang) = JsObject {
keys.map { k =>
k.key -> JsString(k.to(lang)())
}
}
def keysToMessageObject(keys: Seq[I18nKey], lang: Lang) = JsObject {
keys.map { k =>
k.en() -> JsString(k.to(lang)())
}
}
def apply: Funit = Future {
pathFile.mkdir
writeRefs
writeFullJson
} void
private val pathFile = new File(path)
private def dumpFromKey(messages: List[I18nKey], lang: Lang): String =
messages.map { key =>
""""%s":"%s"""".format(key.key, escape(key.to(lang)()))
}.mkString("{", ",", "}")
private def writeRefs {
val code = pool.names.toList.sortBy(_._1).map {
case (code, name) => s"""["$code","$name"]"""
}.mkString("[", ",", "]")
val file = new File("%s/refs.json".format(pathFile.getCanonicalPath))
val out = new PrintWriter(file)
try { out.print(code) }
finally { out.close }
}
private def writeFullJson {
pool.langs foreach { lang =>
val code = dumpFromKey(keys.keys, lang)
val file = new File("%s/%s.all.json".format(pathFile.getCanonicalPath, lang.language))
val out = new PrintWriter(file)
try { out.print(code) }
finally { out.close }
}
}
private def escape(text: String) = text.replace(""""""", """\"""")
}