Skip to content

Commit 0a6b626

Browse files
committed
translate tournament names
TODO: client-side. On /tournament, don't overwrite translated names with english names coming from XHR
1 parent 93511dc commit 0a6b626

31 files changed

+203
-176
lines changed

app/controllers/Api.scala

+8-6
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,13 @@ final class Api(
196196
}
197197
}
198198

199-
def currentTournaments = ApiRequest { _ =>
199+
def currentTournaments = ApiRequest { implicit req =>
200+
implicit val lang = reqLang
200201
env.tournament.api.fetchVisibleTournaments flatMap
201202
env.tournament.apiJsonView.apply map Data.apply
202203
}
203204

204-
def tournament(id: String) = ApiRequest { req =>
205+
def tournament(id: String) = ApiRequest { implicit req =>
205206
env.tournament.tournamentRepo byId id flatMap {
206207
_ ?? { tour =>
207208
val page = (getInt("page", req) | 1) atLeast 1 atMost 200
@@ -213,9 +214,8 @@ final class Api(
213214
getTeamName = env.team.getTeamName.apply _,
214215
playerInfoExt = none,
215216
socketVersion = none,
216-
partial = false,
217-
lang = lila.i18n.defaultLang
218-
) map some
217+
partial = false
218+
)(reqLang) map some
219219
}
220220
} map toApiResult
221221
}
@@ -253,6 +253,7 @@ final class Api(
253253
}
254254

255255
def tournamentsByOwner(name: String) = Action.async { implicit req =>
256+
implicit val lang = reqLang
256257
(name != "lichess") ?? env.user.repo.named(name) flatMap {
257258
_ ?? { user =>
258259
val nb = getInt("nb", req) | Int.MaxValue
@@ -295,7 +296,8 @@ final class Api(
295296
key = "user_activity.api.ip"
296297
)
297298

298-
def activity(name: String) = ApiRequest { req =>
299+
def activity(name: String) = ApiRequest { implicit req =>
300+
implicit val lang = reqLang
299301
UserActivityRateLimitPerIP(HTTPRequest lastRemoteAddress req, cost = 1) {
300302
lila.mon.api.activity.increment(1)
301303
env.user.repo named name flatMap {

app/controllers/LilaController.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import lila.notify.Notification.Notifies
1616
import lila.oauth.{ OAuthScope, OAuthServer }
1717
import lila.security.{ FingerHash, FingerPrintedUser, Granter, Permission }
1818
import lila.user.{ UserContext, User => UserModel }
19+
import lila.i18n.I18nLangPicker
1920

2021
abstract private[controllers] class LilaController(val env: Env)
2122
extends BaseController
@@ -56,6 +57,7 @@ abstract private[controllers] class LilaController(val env: Env)
5657
implicit def ctxLang(implicit ctx: Context) = ctx.lang
5758
implicit def ctxReq(implicit ctx: Context) = ctx.req
5859
implicit def reqConfig(implicit req: RequestHeader) = ui.EmbedConfig(req)
60+
def reqLang(implicit req: RequestHeader) = I18nLangPicker(req)
5961

6062
protected def EnableSharedArrayBuffer(res: Result): Result = res.withHeaders(
6163
"Cross-Origin-Opener-Policy" -> "same-origin",
@@ -471,7 +473,7 @@ abstract private[controllers] class LilaController(val env: Env)
471473
}
472474

473475
private def getAndSaveLang(req: RequestHeader, user: Option[UserModel]): Lang = {
474-
val lang = lila.i18n.I18nLangPicker(req, user.flatMap(_.lang))
476+
val lang = I18nLangPicker(req, user.flatMap(_.lang))
475477
user.filter(_.lang.fold(true)(_ != lang.code)) foreach { env.user.repo.setLang(_, lang) }
476478
lang
477479
}
@@ -616,7 +618,7 @@ abstract private[controllers] class LilaController(val env: Env)
616618
jsonFormError(err)(lila.i18n.defaultLang)
617619

618620
protected def jsonFormErrorFor(err: Form[_], req: RequestHeader, user: Option[UserModel]) =
619-
jsonFormError(err)(lila.i18n.I18nLangPicker(req, user.flatMap(_.lang)))
621+
jsonFormError(err)(I18nLangPicker(req, user.flatMap(_.lang)))
620622

621623
protected def pageHit(req: RequestHeader): Unit =
622624
if (HTTPRequest isHuman req) lila.mon.http.path(req.path).increment()

app/controllers/Tournament.scala

+5-8
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ final class Tournament(
106106
getTeamName = env.team.getTeamName,
107107
playerInfoExt = none,
108108
socketVersion = version.some,
109-
partial = false,
110-
lang = ctx.lang
109+
partial = false
111110
)
112111
chat <- canHaveChat(tour, json.some) ?? env.chat.api.userChat.cached
113112
.findMine(Chat.Id(tour.id), ctx.me)
@@ -138,8 +137,7 @@ final class Tournament(
138137
getTeamName = env.team.getTeamName,
139138
playerInfoExt = playerInfoExt,
140139
socketVersion = socketVersion,
141-
partial = partial,
142-
lang = ctx.lang
140+
partial = partial
143141
)
144142
} dmap { Ok(_) }
145143
}
@@ -222,7 +220,7 @@ final class Tournament(
222220
def terminate(id: String) = Secure(_.TerminateTournament) { implicit ctx => me =>
223221
OptionResult(repo byId id) { tour =>
224222
api kill tour
225-
env.mod.logApi.terminateTournament(me.id, tour.fullName)
223+
env.mod.logApi.terminateTournament(me.id, tour.name()(lila.i18n.defaultLang))
226224
Redirect(routes.Tournament show tour.id)
227225
}
228226
}
@@ -322,9 +320,8 @@ final class Tournament(
322320
env.team.getTeamName,
323321
none,
324322
none,
325-
partial = false,
326-
lila.i18n.defaultLang
327-
) map { Ok(_) }
323+
partial = false
324+
)(reqLang) map { Ok(_) }
328325
}
329326
}
330327
}

app/controllers/User.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ final class User(
9999
page = page
100100
)(ctx.body)
101101
_ <- env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.userIds)
102-
_ <- env.tournament.cached.nameCache preloadMany pag.currentPageResults.flatMap(_.tournamentId)
102+
_ <- env.tournament.cached.nameCache preloadMany {
103+
pag.currentPageResults.flatMap(_.tournamentId).map(_ -> ctxLang)
104+
}
103105
res <- if (HTTPRequest isSynchronousHttp ctx.req) for {
104106
info <- env.userInfo(u, nbs, ctx)
105107
_ <- env.team.cached.nameCache preloadMany info.teamIds
@@ -222,8 +224,10 @@ final class User(
222224
page = page
223225
)(ctx.body)
224226
pag <- pagFromDb.mapFutureResults(env.round.proxyRepo.upgradeIfPresent)
225-
_ <- env.tournament.cached.nameCache preloadMany pag.currentPageResults.flatMap(_.tournamentId)
226-
_ <- env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.userIds)
227+
_ <- env.tournament.cached.nameCache preloadMany {
228+
pag.currentPageResults.flatMap(_.tournamentId).map(_ -> ctxLang)
229+
}
230+
_ <- env.user.lightUserApi preloadMany pag.currentPageResults.flatMap(_.userIds)
227231
} yield pag
228232
}
229233
}

app/templating/TournamentHelper.scala

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package lila.app
22
package templating
33

4+
import play.api.i18n.Lang
5+
import play.api.libs.json.Json
6+
47
import controllers.routes
58
import lila.app.ui.ScalatagsTemplate._
9+
import lila.rating.PerfType
610
import lila.tournament.{ Schedule, Tournament }
711
import lila.user.User
8-
import lila.rating.PerfType
9-
10-
import play.api.libs.json.Json
1112

1213
trait TournamentHelper { self: I18nHelper with DateHelper with UserHelper =>
1314

@@ -26,21 +27,22 @@ trait TournamentHelper { self: I18nHelper with DateHelper with UserHelper =>
2627
}
2728
}
2829

29-
def tournamentLink(tour: Tournament): Frag =
30+
def tournamentLink(tour: Tournament)(implicit lang: Lang): Frag =
3031
a(
3132
dataIcon := "g",
3233
cls := (if (tour.isScheduled) "text is-gold" else "text"),
3334
href := routes.Tournament.show(tour.id).url
34-
)(tour.fullName)
35+
)(tour.name())
3536

36-
def tournamentLink(tourId: String): Frag =
37+
def tournamentLink(tourId: String)(implicit lang: Lang): Frag =
3738
a(
3839
dataIcon := "g",
3940
cls := "text",
4041
href := routes.Tournament.show(tourId).url
4142
)(tournamentIdToName(tourId))
4243

43-
def tournamentIdToName(id: String) = env.tournament getTourName id getOrElse "Tournament"
44+
def tournamentIdToName(id: String)(implicit lang: Lang) =
45+
env.tournament.getTourName get id getOrElse "Tournament"
4446

4547
object scheduledTournamentNameShortHtml {
4648
private def icon(c: Char) = s"""<span data-icon="$c"></span>"""

app/views/game/side.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ object side {
127127
},
128128
tour.map { t =>
129129
st.section(cls := "game__tournament")(
130-
a(cls := "text", dataIcon := "g", href := routes.Tournament.show(t.tour.id))(t.tour.fullName),
130+
a(cls := "text", dataIcon := "g", href := routes.Tournament.show(t.tour.id))(t.tour.name()),
131131
div(cls := "clock", dataTime := t.tour.secondsToFinish)(div(cls := "time")(t.tour.clockStatus))
132132
)
133133
} orElse {

app/views/tournament/crud.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ object crud {
4545

4646
def edit(tour: Tournament, form: Form[_])(implicit ctx: Context) =
4747
layout(
48-
title = tour.fullName,
48+
title = tour.name(),
4949
css = "mod.form"
5050
) {
5151
div(cls := "crud edit page-menu__content box box-pad")(
5252
div(cls := "box__top")(
5353
h1(
54-
a(href := routes.Tournament.show(tour.id))(tour.fullName),
54+
a(href := routes.Tournament.show(tour.id))(tour.name()),
5555
" ",
5656
span("Created by ", usernameOrId(tour.createdBy), " on ", showDate(tour.createdAt))
5757
),
@@ -160,7 +160,7 @@ object crud {
160160
tr(cls := "paginated")(
161161
td(
162162
a(href := routes.TournamentCrud.edit(tour.id))(
163-
strong(tour.fullName),
163+
strong(tour.name()),
164164
" ",
165165
em(tour.spotlight.map(_.headline))
166166
)

app/views/tournament/finishedPaginator.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object finishedPaginator {
2828
td(cls := "icon")(iconTag(tournamentIconChar(t))),
2929
td(cls := "header")(
3030
a(href := routes.Tournament.show(t.id))(
31-
span(cls := "name")(t.fullName),
31+
span(cls := "name")(t.name()),
3232
span(cls := "setup")(
3333
t.clock.show,
3434
"",

app/views/tournament/home.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var d=lichess.StrongSocket.defaults;d.params.flag="tournament";d.events.reload=a
6868
scheduled.map { tour =>
6969
tour.schedule.filter(s => s.freq != lila.tournament.Schedule.Freq.Hourly) map { s =>
7070
a(href := routes.Tournament.show(tour.id), dataIcon := tournamentIconChar(tour))(
71-
strong(tour.name),
71+
strong(tour.name(false)),
7272
momentFromNow(s.at)
7373
)
7474
}

app/views/tournament/homepageSpotlight.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object homepageSpotlight {
2727
}
2828
},
2929
span(cls := "content")(
30-
span(cls := "name")(tour.fullName),
30+
span(cls := "name")(tour.name()),
3131
if (tour.isDistant) span(cls := "more")(momentFromNow(tour.startsAt))
3232
else
3333
frag(
@@ -47,7 +47,7 @@ object homepageSpotlight {
4747
iconTag(pt.iconChar)(cls := "img")
4848
},
4949
span(cls := "content")(
50-
span(cls := "name")(tour.fullName),
50+
span(cls := "name")(tour.name()),
5151
span(cls := "more")(
5252
trans.nbPlayers.plural(tour.nbPlayers, tour.nbPlayers.localize),
5353
"",

app/views/tournament/show.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object show {
2222
shieldOwner: Option[lila.tournament.TournamentShield.OwnerId]
2323
)(implicit ctx: Context) =
2424
views.html.base.layout(
25-
title = s"${tour.fullName} #${tour.id}",
25+
title = s"${tour.name()} #${tour.id}",
2626
moreJs = frag(
2727
jsAt(s"compiled/lichess.tournament${isProd ?? (".min")}.js"),
2828
embedJsUnsafe(s"""lichess=lichess||{};lichess.tournament=${safeJsonValue(
@@ -49,9 +49,9 @@ object show {
4949
chessground = false,
5050
openGraph = lila.app.ui
5151
.OpenGraph(
52-
title = s"${tour.fullName}: ${tour.variant.name} ${tour.clock.show} ${tour.mode.name} #${tour.id}",
52+
title = s"${tour.name()}: ${tour.variant.name} ${tour.clock.show} ${tour.mode.name} #${tour.id}",
5353
url = s"$netBaseUrl${routes.Tournament.show(tour.id).url}",
54-
description = s"${tour.nbPlayers} players compete in the ${showEnglishDate(tour.startsAt)} ${tour.fullName}. " +
54+
description = s"${tour.nbPlayers} players compete in the ${showEnglishDate(tour.startsAt)} ${tour.name()}. " +
5555
s"${tour.clock.show} ${tour.mode.name} games are played during ${tour.minutes} minutes. " +
5656
tour.winnerId.fold("Winner is not yet decided.") { winnerId =>
5757
s"${usernameOrId(winnerId)} takes the prize home!"

app/views/tournament/teamBattle.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ object teamBattle {
1414

1515
def edit(tour: Tournament, form: Form[_])(implicit ctx: Context) =
1616
views.html.base.layout(
17-
title = tour.fullName,
17+
title = tour.name(),
1818
moreCss = cssTag("tournament.form"),
1919
moreJs = jsTag("tournamentTeamBattleForm.js")
2020
)(
2121
main(cls := "page-small")(
2222
div(cls := "tour__form box box-pad")(
23-
h1(tour.fullName),
23+
h1(tour.name()),
2424
standardFlash(),
2525
if (tour.isFinished) p("This tournament is over, and the teams can no longer be updated.")
2626
else p("List the teams that will compete in this battle."),
@@ -58,7 +58,7 @@ object teamBattle {
5858
td(cls := "icon")(iconTag(tournamentIconChar(t))),
5959
td(cls := "header")(
6060
a(href := routes.Tournament.show(t.id))(
61-
span(cls := "name")(t.fullName),
61+
span(cls := "name")(t.name()),
6262
span(cls := "setup")(
6363
t.clock.show,
6464
"",

app/views/userTournament/list.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object list {
3939
td(cls := "icon")(iconTag(tournamentIconChar(e.tour))),
4040
td(cls := "header")(
4141
a(href := routes.Tournament.show(e.tour.id))(
42-
span(cls := "name")(e.tour.fullName),
42+
span(cls := "name")(e.tour.name()),
4343
span(cls := "setup")(
4444
e.tour.clock.show,
4545
"",

modules/activity/src/main/JsonView.scala

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lila.activity
22

33
import org.joda.time.Interval
4+
import play.api.i18n.Lang
45
import play.api.libs.json._
56

67
import lila.common.Iso
@@ -43,20 +44,20 @@ final class JsonView(
4344
implicit val tourRatioWrites = Writes[TourRatio] { r =>
4445
JsNumber((r.value * 100).toInt atLeast 1)
4546
}
46-
implicit val tourEntryWrites = OWrites[TourEntry] { e =>
47+
implicit def tourEntryWrites(implicit lang: Lang) = OWrites[TourEntry] { e =>
4748
Json.obj(
4849
"tournament" -> Json.obj(
4950
"id" -> e.tourId,
50-
"name" -> ~getTourName(e.tourId)
51+
"name" -> ~getTourName.get(e.tourId)
5152
),
5253
"nbGames" -> e.nbGames,
5354
"score" -> e.score,
5455
"rank" -> e.rank,
5556
"rankPercent" -> e.rankRatio
5657
)
5758
}
58-
implicit val toursWrites = Json.writes[ActivityView.Tours]
59-
implicit val puzzlesWrites = Json.writes[Puzzles]
59+
implicit def toursWrites(implicit lang: Lang) = Json.writes[ActivityView.Tours]
60+
implicit val puzzlesWrites = Json.writes[Puzzles]
6061
implicit def simulWrites(user: User) = OWrites[Simul] { s =>
6162
Json.obj(
6263
"id" -> s.id,
@@ -92,7 +93,7 @@ final class JsonView(
9293
}
9394
import Writers._
9495

95-
def apply(a: ActivityView, user: User): Fu[JsObject] = fuccess {
96+
def apply(a: ActivityView, user: User)(implicit lang: Lang): Fu[JsObject] = fuccess {
9697
Json
9798
.obj("interval" -> a.interval)
9899
.add("games", a.games)

modules/api/src/main/PgnDump.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ final class PgnDump(
1313
getTournamentName: lila.tournament.GetTourName
1414
)(implicit ec: scala.concurrent.ExecutionContext) {
1515

16+
implicit private val lang = lila.i18n.defaultLang
17+
1618
def apply(game: Game, initialFen: Option[FEN], analysis: Option[Analysis], flags: WithFlags): Fu[Pgn] =
1719
dumper(game, initialFen, flags) flatMap { pgn =>
1820
if (flags.tags) (game.simulId ?? simulApi.idToName) map { simulName =>
19-
simulName.orElse(game.tournamentId flatMap getTournamentName).fold(pgn)(pgn.withEvent)
21+
simulName.orElse(game.tournamentId flatMap getTournamentName.get).fold(pgn)(pgn.withEvent)
2022
} else fuccess(pgn)
2123
} map { pgn =>
2224
val evaled = analysis.ifTrue(flags.evals).fold(pgn)(addEvals(pgn, _))

0 commit comments

Comments
 (0)