Skip to content

Commit

Permalink
remove i18n subdomains, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed May 26, 2017
1 parent 5ced819 commit fad609c
Show file tree
Hide file tree
Showing 44 changed files with 246 additions and 437 deletions.
2 changes: 1 addition & 1 deletion app/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Global extends GlobalSettings {
if (lila.common.PlayApp.isProd) {
lila.mon.http.response.code500()
fuccess(InternalServerError(views.html.base.errorPage(ex) {
lila.api.Context(req, lila.app.Env.api.assetVersion.get, lila.i18n.I18nKey.en)
lila.api.Context(req, lila.app.Env.api.assetVersion.get, lila.i18n.defaultLang)
}))
} else super.onError(req, ex)
} else fuccess(InternalServerError(ex.getMessage))
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object Challenge extends LilaController {
}

private def translations(implicit ctx: Context) = Env.i18n.jsDump.keysToObject(List(
Env.i18n.keys.waiting
), Env.i18n.pool lang ctx.req)
lila.i18n.I18nKeys.waiting
), ctx.lang)

protected[controllers] def showId(id: String)(implicit ctx: Context): Fu[Result] =
OptionFuResult(env.api byId id)(showChallenge)
Expand Down
25 changes: 13 additions & 12 deletions app/controllers/Dasher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ import lila.api.Context
import lila.app._
import lila.common.LightUser.lightUserWrites
import lila.pref.JsonView._
import lila.i18n.I18nKeys

object Dasher extends LilaController {

private def translations(implicit ctx: Context) = Env.i18n.jsDump.keysToObject(
ctx.isAnon.fold(
List(
Env.i18n.keys.signIn,
Env.i18n.keys.signUp
I18nKeys.signIn,
I18nKeys.signUp
),
List(
Env.i18n.keys.profile,
Env.i18n.keys.inbox,
Env.i18n.keys.preferences,
Env.i18n.keys.logOut
I18nKeys.profile,
I18nKeys.inbox,
I18nKeys.preferences,
I18nKeys.logOut
)
) ::: List(
Env.i18n.keys.networkLagBetweenYouAndLichess,
Env.i18n.keys.timeToProcessAMoveOnLichessServer,
Env.i18n.keys.sound
), Env.i18n.pool lang ctx.req
I18nKeys.networkLagBetweenYouAndLichess,
I18nKeys.timeToProcessAMoveOnLichessServer,
I18nKeys.sound
), ctx.lang
)

def get = Open { implicit ctx =>
Expand All @@ -37,8 +38,8 @@ object Dasher extends LilaController {
Json.obj(
"user" -> ctx.me.map(_.light),
"lang" -> Json.obj(
"current" -> Env.i18n.pool.lang(ctx.req).language.toString,
"accepted" -> (ctx.req.acceptLanguages.map(_.language.toString)(breakOut): List[String]).distinct
"current" -> ctx.lang.code,
"accepted" -> (ctx.req.acceptLanguages.map(_.code)(breakOut): List[String]).distinct
),
"sound" -> Json.obj(
"list" -> lila.pref.SoundSet.list.map { set =>
Expand Down
23 changes: 14 additions & 9 deletions app/controllers/I18n.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package controllers

import play.api.data.Form
import play.api.data._
import play.api.data.Forms._
import play.api.i18n.Lang
import play.api.libs.json.Json

import lila.app._
import lila.common.{ LilaCookie, HTTPRequest }

object I18n extends LilaController {

private val form = Form(single("lang" -> text.verifying { code =>
Lang.get(code) ?? lila.i18n.I18nDb.langs.contains
}))

def select = OpenBody { implicit ctx =>
import play.api.data.Forms._
import play.api.data._
implicit val req = ctx.body
Form(single("lang" -> text.verifying(Env.i18n.pool contains _))).bindFromRequest.fold(
form.bindFromRequest.fold(
_ => notFound,
lang => {
ctx.me.filterNot(_.lang contains lang) ?? { me =>
lila.user.UserRepo.setLang(me.id, lang)
code => {
val lang = Lang(code)
ctx.me.filterNot(_.lang contains lang.code) ?? { me =>
lila.user.UserRepo.setLang(me.id, lang.code)
}
} >> negotiate(
html = {
Expand All @@ -35,10 +40,10 @@ object I18n extends LilaController {
}
}
}
if (ctx.isAnon) redir.withCookies(LilaCookie.session("lang", lang))
if (ctx.isAnon) redir.withCookies(LilaCookie.session("lang", lang.code))
else redir
}.fuccess,
api = _ => Ok(Json.obj("lang" -> lang)).fuccess
api = _ => Ok(Json.obj("lang" -> lang.code)).fuccess
)
)
}
Expand Down
24 changes: 9 additions & 15 deletions app/controllers/LilaController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private[controllers] trait LilaController
api = _ => fuccess(Ok(jsonOkBody) as JSON)
)

implicit def lang(implicit req: RequestHeader) = Env.i18n.pool lang req
// implicit def lang(implicit req: RequestHeader) = Env.i18n.pool lang req
implicit def lang(implicit ctx: Context) = ctx.lang

protected def NoCache(res: Result): Result = res.withHeaders(
CACHE_CONTROL -> "no-cache, no-store, must-revalidate", EXPIRES -> "0"
Expand All @@ -56,7 +57,7 @@ private[controllers] trait LilaController
protected def Open[A](p: BodyParser[A])(f: Context => Fu[Result]): Action[A] =
Action.async(p) { req =>
CSRF(req) {
reqToCtx(req) flatMap maybeI18nRedirect(f)
reqToCtx(req) flatMap f
}
}

Expand All @@ -66,7 +67,7 @@ private[controllers] trait LilaController
protected def OpenBody[A](p: BodyParser[A])(f: BodyContext[A] => Fu[Result]): Action[A] =
Action.async(p) { req =>
CSRF(req) {
reqToCtx(req) flatMap maybeI18nRedirect(f)
reqToCtx(req) flatMap f
}
}

Expand All @@ -77,9 +78,7 @@ private[controllers] trait LilaController
Action.async(p) { req =>
CSRF(req) {
reqToCtx(req) flatMap { ctx =>
ctx.me.fold(authenticationFailed(ctx)) { me =>
maybeI18nRedirect((c: Context) => f(c)(me))(ctx)
}
ctx.me.fold(authenticationFailed(ctx))(f(ctx))
}
}
}
Expand All @@ -90,10 +89,8 @@ private[controllers] trait LilaController
protected def AuthBody[A](p: BodyParser[A])(f: BodyContext[A] => UserModel => Fu[Result]): Action[A] =
Action.async(p) { req =>
CSRF(req) {
reqToCtx(req) flatMap { implicit ctx =>
ctx.me.fold(authenticationFailed) { me =>
maybeI18nRedirect((c: BodyContext[A]) => f(c)(me))(ctx)
}
reqToCtx(req) flatMap { ctx =>
ctx.me.fold(authenticationFailed(ctx))(f(ctx))
}
}
}
Expand Down Expand Up @@ -122,9 +119,6 @@ private[controllers] trait LilaController
protected def SecureBody(perm: Permission.type => Permission)(f: BodyContext[_] => UserModel => Fu[Result]): Action[AnyContent] =
SecureBody(BodyParsers.parse.anyContent)(perm(Permission))(f)

private def maybeI18nRedirect[C <: Context](f: C => Fu[Result])(ctx: C): Fu[Result] =
Env.i18n.requestHandler(ctx.req, ctx.me, ctx.lang).fold(f(ctx))(fuccess)

protected def Firewall[A <: Result](a: => Fu[A])(implicit ctx: Context): Fu[Result] =
Env.security.firewall.accepts(ctx.req) flatMap {
_ fold (a, {
Expand Down Expand Up @@ -278,13 +272,13 @@ private[controllers] trait LilaController
}).dmap(_.withHeaders("Vary" -> "Accept"))

protected def reqToCtx(req: RequestHeader): Fu[HeaderContext] = restoreUser(req) flatMap { d =>
val ctx = UserContext(req, d.map(_.user), Env.i18n.langPicker(req, d.map(_.user)))
val ctx = UserContext(req, d.map(_.user), lila.i18n.I18nLangPicker(req, d.map(_.user)))
pageDataBuilder(ctx, d.exists(_.hasFingerprint)) dmap { Context(ctx, _) }
}

protected def reqToCtx[A](req: Request[A]): Fu[BodyContext[A]] =
restoreUser(req) flatMap { d =>
val ctx = UserContext(req, d.map(_.user), Env.i18n.langPicker(req, d.map(_.user)))
val ctx = UserContext(req, d.map(_.user), lila.i18n.I18nLangPicker(req, d.map(_.user)))
pageDataBuilder(ctx, d.exists(_.hasFingerprint)) dmap { Context(ctx, _) }
}

Expand Down
18 changes: 9 additions & 9 deletions app/controllers/UserAnalysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import scala.concurrent.duration._

import lila.app._
import lila.game.{ GameRepo, Pov }
import lila.i18n.I18nKeys
import lila.round.Forecast.{ forecastStepJsonFormat, forecastJsonWriter }
import views._

Expand Down Expand Up @@ -41,19 +42,18 @@ object UserAnalysis extends LilaController with TheftPrevention {
}

private lazy val keyboardI18nKeys = {
val trans = Env.i18n.keys
Seq(
trans.keyboardShortcuts,
trans.keyMoveBackwardOrForward,
trans.keyGoToStartOrEnd,
trans.keyShowOrHideComments,
trans.keyEnterOrExitVariation,
trans.youCanAlsoScrollOverTheBoardToMoveInTheGame,
trans.pressShiftPlusClickOrRightClickToDrawCirclesAndArrowsOnTheBoard
I18nKeys.keyboardShortcuts,
I18nKeys.keyMoveBackwardOrForward,
I18nKeys.keyGoToStartOrEnd,
I18nKeys.keyShowOrHideComments,
I18nKeys.keyEnterOrExitVariation,
I18nKeys.youCanAlsoScrollOverTheBoardToMoveInTheGame,
I18nKeys.pressShiftPlusClickOrRightClickToDrawCirclesAndArrowsOnTheBoard
)
}

def keyboardI18n = Action.async { implicit req =>
def keyboardI18n = Open { implicit ctx =>
JsonOk(fuccess(Env.i18n.jsDump.keysToObject(keyboardI18nKeys, lang)))
}

Expand Down
2 changes: 1 addition & 1 deletion app/templating/AiHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import lila.user.UserContext
trait AiHelper { self: I18nHelper =>

def aiName(level: Int, withRating: Boolean = true)(implicit ctx: UserContext): String = {
val name = trans.aiNameLevelAiLevel.str("Stockfish AI", level)
val name = lila.i18n.I18nKeys.aiNameLevelAiLevel.str("Stockfish AI", level)
val rating = withRating ?? {
aiRating(level) ?? { r => s" ($r)" }
}
Expand Down
7 changes: 4 additions & 3 deletions app/templating/AnalysisHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ package templating
import lila.analyse.Advice.Judgment

import lila.api.Context
import lila.i18n.I18nKeys

trait AnalysisHelper { self: I18nHelper with SecurityHelper =>

def judgmentName(judgment: Judgment)(implicit ctx: Context) = judgment match {
case Judgment.Blunder => trans.blunders()
case Judgment.Mistake => trans.mistakes()
case Judgment.Inaccuracy => trans.inaccuracies()
case Judgment.Blunder => I18nKeys.blunders()
case Judgment.Mistake => I18nKeys.mistakes()
case Judgment.Inaccuracy => I18nKeys.inaccuracies()
case judgment => judgment.toString
}
}
10 changes: 6 additions & 4 deletions app/templating/FormHelper.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package lila.app
package templating

import lila.api.Context
import play.api.data._
import play.twirl.api.Html

import lila.api.Context
import lila.i18n.I18nKeys

trait FormHelper { self: I18nHelper =>

private val errNames = Map(
"error.minLength" -> trans.textIsTooShort,
"error.maxLength" -> trans.textIsTooLong,
"captcha.fail" -> trans.notACheckmate
"error.minLength" -> I18nKeys.textIsTooShort,
"error.maxLength" -> I18nKeys.textIsTooLong,
"captcha.fail" -> I18nKeys.notACheckmate
)

def errMsg(form: Field)(implicit ctx: Context): Html = errMsg(form.errors)
Expand Down
53 changes: 27 additions & 26 deletions app/templating/GameHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import play.twirl.api.Html

import lila.game.{ Game, Player, Namer, Pov }
import lila.user.{ User, UserContext }
import lila.i18n.I18nKeys

trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHelper with ChessgroundHelper =>

Expand Down Expand Up @@ -64,30 +65,30 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}

def variantName(variant: chess.variant.Variant)(implicit ctx: UserContext) = variant match {
case chess.variant.Standard => trans.standard.str()
case chess.variant.FromPosition => trans.fromPosition.str()
case chess.variant.Standard => I18nKeys.standard.str()
case chess.variant.FromPosition => I18nKeys.fromPosition.str()
case v => v.name
}

def variantNameNoCtx(variant: chess.variant.Variant) = variant match {
case chess.variant.Standard => trans.standard.en()
case chess.variant.FromPosition => trans.fromPosition.en()
case chess.variant.Standard => I18nKeys.standard.en()
case chess.variant.FromPosition => I18nKeys.fromPosition.en()
case v => v.name
}

def shortClockName(clock: Option[Clock.Config])(implicit ctx: UserContext): Html =
clock.fold(trans.unlimited())(shortClockName)
clock.fold(I18nKeys.unlimited())(shortClockName)

def shortClockName(clock: Clock.Config): Html = Html(clock.show)

def modeName(mode: Mode)(implicit ctx: UserContext): String = mode match {
case Mode.Casual => trans.casual.str()
case Mode.Rated => trans.rated.str()
case Mode.Casual => I18nKeys.casual.str()
case Mode.Rated => I18nKeys.rated.str()
}

def modeNameNoCtx(mode: Mode): String = mode match {
case Mode.Casual => trans.casual.en()
case Mode.Rated => trans.rated.en()
case Mode.Casual => I18nKeys.casual.en()
case Mode.Rated => I18nKeys.rated.en()
}

def playerUsername(player: Player, withRating: Boolean = true, withTitle: Boolean = true) =
Expand Down Expand Up @@ -133,7 +134,7 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
val href = s"${routes.User show user.name}${if (mod) "?mod" else ""}"
val content = playerUsername(player, withRating)
val diff = (player.ratingDiff ifTrue withDiff).fold(Html(""))(showRatingDiff)
val mark = engine ?? s"""<span class="engine_mark" title="${trans.thisPlayerUsesChessComputerAssistance()}"></span>"""
val mark = engine ?? s"""<span class="engine_mark" title="${I18nKeys.thisPlayerUsesChessComputerAssistance()}"></span>"""
val dataIcon = withOnline ?? """data-icon="r""""
val space = if (withOnline) "&nbsp;" else ""
val tag = if (link) "a" else "span"
Expand All @@ -142,31 +143,31 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}

def gameEndStatus(game: Game)(implicit ctx: UserContext): Html = game.status match {
case S.Aborted => trans.gameAborted()
case S.Mate => trans.checkmate()
case S.Aborted => I18nKeys.gameAborted()
case S.Mate => I18nKeys.checkmate()
case S.Resign => game.loser match {
case Some(p) if p.color.white => trans.whiteResigned()
case _ => trans.blackResigned()
case Some(p) if p.color.white => I18nKeys.whiteResigned()
case _ => I18nKeys.blackResigned()
}
case S.UnknownFinish => trans.finished()
case S.Stalemate => trans.stalemate()
case S.UnknownFinish => I18nKeys.finished()
case S.Stalemate => I18nKeys.stalemate()
case S.Timeout => game.loser match {
case Some(p) if p.color.white => trans.whiteLeftTheGame()
case Some(_) => trans.blackLeftTheGame()
case None => trans.draw()
case Some(p) if p.color.white => I18nKeys.whiteLeftTheGame()
case Some(_) => I18nKeys.blackLeftTheGame()
case None => I18nKeys.draw()
}
case S.Draw => trans.draw()
case S.Outoftime => trans.timeOut()
case S.Draw => I18nKeys.draw()
case S.Outoftime => I18nKeys.timeOut()
case S.NoStart => Html {
val color = game.loser.fold(Color.white)(_.color).name.capitalize
s"$color didn't move"
}
case S.Cheat => Html("Cheat detected")
case S.VariantEnd => game.variant match {
case chess.variant.KingOfTheHill => trans.kingInTheCenter()
case chess.variant.ThreeCheck => trans.threeChecks()
case chess.variant.RacingKings => trans.raceFinished()
case _ => trans.variantEnding()
case chess.variant.KingOfTheHill => I18nKeys.kingInTheCenter()
case chess.variant.ThreeCheck => I18nKeys.threeChecks()
case chess.variant.RacingKings => I18nKeys.raceFinished()
case _ => I18nKeys.variantEnding()
}
case _ => Html("")
}
Expand Down Expand Up @@ -240,7 +241,7 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}

def challengeTitle(c: lila.challenge.Challenge)(implicit ctx: UserContext) = {
val speed = c.clock.map(_.config).fold(trans.unlimited.str()) { clock =>
val speed = c.clock.map(_.config).fold(I18nKeys.unlimited.str()) { clock =>
s"${chess.Speed(clock).name} (${clock.show})"
}
val variant = c.variant.exotic ?? s" ${c.variant.name}"
Expand Down
Loading

0 comments on commit fad609c

Please sign in to comment.