implement i18n file fixer
This commit is contained in:
parent
c66dc2e51d
commit
3b3246e1c5
|
@ -1,15 +1,43 @@
|
|||
package lila
|
||||
package i18n
|
||||
|
||||
import play.api.i18n.Lang
|
||||
import play.api.i18n.{ MessagesApi, Lang }
|
||||
import java.io._
|
||||
import scalaz.effects._
|
||||
|
||||
final class FileFix(
|
||||
pool: I18nPool,
|
||||
path: String,
|
||||
keys: I18nKeys) {
|
||||
pool: I18nPool,
|
||||
path: String,
|
||||
keys: I18nKeys,
|
||||
api: MessagesApi) {
|
||||
|
||||
private val pathFile = new File(path)
|
||||
private val keyNames = keys.keys map (_.key)
|
||||
|
||||
val apply: IO[Unit] =
|
||||
(pool.nonDefaultLangs.toList map fix).sequence map (_ ⇒ Unit)
|
||||
|
||||
private def fix(lang: Lang): IO[Unit] = {
|
||||
val messages = sanitize((api.messages get lang.language) | Map.empty)
|
||||
write(lang, messages)
|
||||
}
|
||||
|
||||
private def sanitize(messages: Map[String, String]) =
|
||||
keyNames map { name ⇒
|
||||
messages get name map (name -> _)
|
||||
} flatten
|
||||
|
||||
private def write(lang: Lang, messages: List[(String, String)]) = io {
|
||||
val file = "%s/messages.%s".format(pathFile.getCanonicalPath, lang.language)
|
||||
printToFile(new File(file)) { writer ⇒
|
||||
messages foreach {
|
||||
case (name, trans) ⇒ writer.println(name + "=" + trans)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def printToFile(f: File)(op: PrintWriter ⇒ Unit) {
|
||||
val p = new java.io.PrintWriter(f)
|
||||
try { op(p) } finally { p.close() }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,5 +38,6 @@ final class I18nEnv(
|
|||
lazy val fileFix = new FileFix(
|
||||
path = app.path.getCanonicalPath + "/" + I18nFilePathRelative,
|
||||
pool = pool,
|
||||
keys = keys)
|
||||
keys = keys,
|
||||
api = messagesApi)
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ final class I18nPool(val langs: Set[Lang], val default: Lang) {
|
|||
|
||||
private val cache = scala.collection.mutable.Map[String, Option[Lang]]()
|
||||
|
||||
def nonDefaultLangs = langs - default
|
||||
|
||||
val names: Map[String, String] = langs map { l ⇒
|
||||
l.language -> LangList.name(l.language)
|
||||
} toMap
|
||||
|
|
|
@ -10,6 +10,11 @@ case class JsDump(
|
|||
pool: I18nPool,
|
||||
keys: I18nKeys) {
|
||||
|
||||
val apply: IO[Unit] = for {
|
||||
_ ← io(pathFile.mkdir)
|
||||
_ ← (pool.nonDefaultLangs.toList map write).sequence
|
||||
} yield ()
|
||||
|
||||
private val messages = List(
|
||||
keys.unlimited,
|
||||
keys.standard,
|
||||
|
@ -37,15 +42,8 @@ case class JsDump(
|
|||
keys.yourTurn,
|
||||
keys.waitingForOpponent)
|
||||
|
||||
private val en = Lang("en")
|
||||
|
||||
private val pathFile = new File(path)
|
||||
|
||||
def apply: IO[Unit] = for {
|
||||
_ ← io(pathFile.mkdir)
|
||||
_ ← (pool.langs.toList map write).sequence
|
||||
} yield ()
|
||||
|
||||
private def write(lang: Lang): IO[Unit] = io {
|
||||
val code = dump(lang)
|
||||
val file = new File("%s/%s.js".format(pathFile.getCanonicalPath, lang.language))
|
||||
|
@ -56,7 +54,7 @@ case class JsDump(
|
|||
|
||||
private def dump(lang: Lang): String =
|
||||
"""lichess_translations = {%s};""".format(messages map { key ⇒
|
||||
""""%s":"%s"""".format(escape(key.to(en)()), escape(key.to(lang)()))
|
||||
""""%s":"%s"""".format(escape(key.to(pool.default)()), escape(key.to(lang)()))
|
||||
} mkString ",")
|
||||
|
||||
private def escape(text: String) = text.replace(""""""", """\"""")
|
||||
|
|
|
@ -33,7 +33,7 @@ final class I18nKeys(translator: Translator) {
|
|||
|
||||
%vals%
|
||||
|
||||
def keys = List(%keys%)
|
||||
def all = List(%keys%)
|
||||
}
|
||||
"""
|
||||
|
||||
|
|
|
@ -11,4 +11,9 @@ case class I18n(i18n: I18nEnv) {
|
|||
_ ← putStrLn("Dumping JavaScript translations")
|
||||
_ ← i18n.jsDump.apply
|
||||
} yield ()
|
||||
|
||||
def fileFix: IO[Unit] = for {
|
||||
_ ← putStrLn("Fixing translation files")
|
||||
_ ← i18n.fileFix.apply
|
||||
} yield ()
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ object Main {
|
|||
val op: IO[Unit] = args.toList match {
|
||||
case "average-elo" :: Nil ⇒ infos.averageElo
|
||||
case "i18n-js-dump" :: Nil ⇒ i18n.jsDump
|
||||
case "i18n-fix" :: Nil ⇒ i18n.fileFix
|
||||
case "user-enable" :: username :: Nil ⇒ users enable username
|
||||
case "user-disable" :: username :: Nil ⇒ users disable username
|
||||
case "forum-denormalize" :: Nil ⇒ forum.denormalize
|
||||
|
|
Loading…
Reference in a new issue