Skip to content

Commit

Permalink
play 2.4 migration WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jul 14, 2015
1 parent 7cd7f9b commit 2b47e23
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 30 deletions.
21 changes: 21 additions & 0 deletions modules/common/src/main/PlayApp.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package lila.common

import com.typesafe.config.Config
import play.api.i18n.{ Lang, Messages }
import play.api.{ Play, Application, Mode }
import scala.collection.JavaConversions._

object PlayApp {

Expand All @@ -16,6 +18,25 @@ object PlayApp {
play.api.libs.concurrent.Akka.system
}

lazy val langs = loadConfig.getStringList("play.i18n.langs").toList map Lang.apply

protected def loadMessages(file: String): Map[String, String] = withApp { app =>
import scala.collection.JavaConverters._
import play.utils.Resources
app.classloader.getResources(file).asScala.toList
.filterNot(url => Resources.isDirectory(app.classloader, url)).reverse
.map { messageFile =>
Messages.parse(Messages.UrlMessageSource(messageFile), messageFile.toString).fold(e => throw e, identity)
}.foldLeft(Map.empty[String, String]) { _ ++ _ }
}

lazy val messages: Map[String, Map[String, String]] =
langs.map(_.code).map { lang =>
(lang, loadMessages("messages." + lang))
}.toMap
.+("default" -> loadMessages("messages"))
.+("default.play" -> loadMessages("messages.default"))

private def enableScheduler = !(loadConfig getBoolean "app.scheduler.disabled")

def scheduler = new Scheduler(system.scheduler,
Expand Down
2 changes: 1 addition & 1 deletion modules/db/src/main/Tube.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ object JsTube {
) andThen (__ \ from).json.prune

def readDate(field: Symbol) =
(__ \ field).json.update(of[JsObject] map (_ \ "$date"))
(__ \ field).json.update(of[JsObject] map { o => (o \ "$date").get })

def readDateOpt(field: Symbol) = readDate(field) orElse json.reader

Expand Down
4 changes: 2 additions & 2 deletions modules/db/src/main/api/primitive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object $primitive {
.query(query)
.projection(Json.obj(field -> true))
} toList[BSONDocument] max map2 { (obj: BSONDocument) =>
extract(JsObjectReader.read(obj) \ field)
extract(JsObjectReader.read(obj) \ field get)
} map (_.flatten)

def one[A: InColl, B](
Expand All @@ -32,6 +32,6 @@ object $primitive {
.query(query)
.projection(Json.obj(field -> true))
}.one[BSONDocument] map2 { (obj: BSONDocument) =>
extract(JsObjectReader.read(obj) \ field)
extract(JsObjectReader.read(obj) \ field get)
} map (_.flatten)
}
13 changes: 5 additions & 8 deletions modules/i18n/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package lila.i18n

import com.typesafe.config.Config
import play.api.i18n.Lang
import play.api.i18n.{ MessagesApi, MessagesPlugin }
import play.api.libs.json._

final class Env(
config: Config,
db: lila.db.Env,
system: akka.actor.ActorSystem,
val messagesApi: MessagesApi,
messages: Messages,
captcher: akka.actor.ActorSelection,
appPath: String) {

Expand Down Expand Up @@ -37,7 +36,7 @@ final class Env(
default = I18nKey.en)

lazy val translator = new Translator(
api = messagesApi,
messages = messages,
pool = pool)

lazy val keys = new I18nKeys(translator)
Expand All @@ -56,10 +55,10 @@ final class Env(
path = appPath + "/" + FilePathRelative,
pool = pool,
keys = keys,
api = messagesApi)
messages = messages)

lazy val transInfos = TransInfos(
api = messagesApi,
messages = messages,
keys = keys)

lazy val forms = new DataForm(
Expand Down Expand Up @@ -103,9 +102,7 @@ object Env {
config = lila.common.PlayApp loadConfig "i18n",
db = lila.db.Env.current,
system = PlayApp.system,
messagesApi = PlayApp.withApp(_.plugin[MessagesPlugin])
.err("this plugin was not registered or disabled")
.api,
messages = PlayApp.messages,
captcher = lila.hub.Env.current.actor.captcher,
appPath = PlayApp withApp (_.path.getCanonicalPath)
)
Expand Down
10 changes: 4 additions & 6 deletions modules/i18n/src/main/FileFix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package lila.i18n
import java.io._
import scala.concurrent.Future

import play.api.i18n.{ MessagesApi, Lang }
import play.api.i18n.Lang

private[i18n] final class FileFix(
pool: I18nPool,
path: String,
keys: I18nKeys,
api: MessagesApi) {
messages: Messages) {

val apply: Funit =
Future.traverse(pool.nonDefaultLangs.toList)(fix).void

private def fix(lang: Lang): Funit = {
val messages = sanitize((api.messages get lang.language) | Map.empty)
write(lang, messages)
}
private def fix(lang: Lang): Funit =
write(lang, sanitize((messages get lang.language) | Map.empty))

private def sanitize(messages: Map[String, String]) =
keys.keys map { key =>
Expand Down
8 changes: 4 additions & 4 deletions modules/i18n/src/main/TransInfo.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lila.i18n

import play.api.i18n.{ MessagesApi, Lang }
import play.api.i18n.Lang

case class TransInfo(
lang: Lang,
Expand Down Expand Up @@ -37,15 +37,15 @@ private[i18n] object TransInfos {

val defaultCode = "en"

def apply(api: MessagesApi, keys: I18nKeys): TransInfos = TransInfos {
def apply(messages: Messages, keys: I18nKeys): TransInfos = TransInfos {
val nbMessages = keys.count
LangList.sortedList.filter(_._1 != defaultCode) map {
case (code, name) => TransInfo(
lang = Lang(code),
name = name,
contributors = Contributors(code),
nbTranslated = api.messages.get(code) ?? (_.size),
nbMissing = nbMessages - (api.messages.get(code) ?? (_.size))
nbTranslated = messages.get(code) ?? (_.size),
nbMissing = nbMessages - (messages.get(code) ?? (_.size))
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/i18n/src/main/Translation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ private[i18n] object Translation {
merge(defaults) andThen readDate('createdAt)
)) andThen Json.reads[Translation],
Json.writes[Translation] andThen (__.json update writeDate('createdAt))
)
)
}
5 changes: 2 additions & 3 deletions modules/i18n/src/main/Translator.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package lila.i18n

import play.api.i18n.{ MessagesApi, Lang }
import play.api.i18n.Lang
import play.api.mvc.RequestHeader
import play.twirl.api.Html

private[i18n] final class Translator(api: MessagesApi, pool: I18nPool) {
private[i18n] final class Translator(messages: Messages, pool: I18nPool) {

private val messages = api.messages
private val defaultMessages = messages.get("default") err "No default messages"

def html(key: String, args: List[Any])(implicit req: RequestHeader): Html =
Expand Down
2 changes: 2 additions & 0 deletions modules/i18n/src/main/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package lila

package object i18n extends PackageObject with WithPlay {

type Messages = Map[String, Map[String, String]]

object tube {

private[i18n] implicit lazy val translationTube =
Expand Down
2 changes: 1 addition & 1 deletion modules/security/src/main/Api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private[security] final class Api(firewall: Firewall, tor: Tor) {
"user" -> BSONDocument("$ne" -> userId)
),
BSONDocument("user" -> true)
).cursor[BSONDocument].collect[List]().map {
).cursor[BSONDocument]().collect[List]().map {
_.flatMap(_.getAs[String]("user"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import play._
import play.Play.autoImport._
import play.sbt.PlayImport._
import play.twirl.sbt.Import._
import PlayKeys._
import sbt._, Keys._
Expand All @@ -9,7 +9,7 @@ object ApplicationBuild extends Build {
import BuildSettings._
import Dependencies._

lazy val root = Project("lila", file(".")) enablePlugins PlayScala settings (
lazy val root = Project("lila", file(".")) enablePlugins _root_.play.sbt.PlayScala settings (
scalaVersion := globalScalaVersion,
resolvers ++= Dependencies.Resolvers.commons,
scalacOptions := compilerOptions,
Expand Down
3 changes: 2 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import play.sbt.PlayImport._
import sbt._, Keys._

object Dependencies {
Expand Down Expand Up @@ -41,7 +42,7 @@ object Dependencies {
val prismic = "io.prismic" %% "scala-kit" % "1.3.4"

object play {
val version = "2.3.9"
val version = "2.4.2"
val api = "com.typesafe.play" %% "play" % version
val test = "com.typesafe.play" %% "play-test" % version
}
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.9")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.2")

// addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.6")

0 comments on commit 2b47e23

Please sign in to comment.