forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsDump.scala
99 lines (87 loc) · 2.63 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
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
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 apply: Funit = Future {
pathFile.mkdir
pool.nonDefaultLangs foreach write(jsMessages)
writeRefs
writeFullJson
} void
private val jsMessages = List(
keys.standard,
keys.rated,
keys.casual,
keys.thisGameIsRated,
keys.whiteCreatesTheGame,
keys.blackCreatesTheGame,
keys.whiteJoinsTheGame,
keys.blackJoinsTheGame,
keys.drawOfferSent,
keys.drawOfferDeclined,
keys.drawOfferAccepted,
keys.drawOfferCanceled,
keys.rematchOfferSent,
keys.rematchOfferAccepted,
keys.rematchOfferCanceled,
keys.rematchOfferDeclined,
keys.takebackPropositionSent,
keys.takebackPropositionDeclined,
keys.takebackPropositionAccepted,
keys.takebackPropositionCanceled,
keys.gameOver,
keys.yourTurn,
keys.waitingForOpponent,
keys.accept,
keys.decline,
keys.challengeToPlay,
keys.youNeedAnAccountToDoThat,
keys.createANewTournament,
keys.join,
keys.joinTheGame,
keys.cancel,
keys.withdraw,
keys.tournamentIsStarting,
keys.xDays)
private val pathFile = new File(path)
private def write(messages: List[I18nKey])(lang: Lang) {
val code = s"""lichess_translations = ${dump(messages, lang)};"""
val file = new File("%s/%s.js".format(pathFile.getCanonicalPath, lang.language))
val out = new PrintWriter(file)
try { out.print(code) }
finally { out.close }
}
private def dump(messages: List[I18nKey], lang: Lang): String =
messages.map { key =>
""""%s":"%s"""".format(escape(key.to(pool.default)()), 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 = dump(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(""""""", """\"""")
}