Skip to content

Commit

Permalink
translation mess WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed May 27, 2017
1 parent 029a5fa commit 6897b9e
Show file tree
Hide file tree
Showing 30 changed files with 102 additions and 83 deletions.
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 = lila.i18n.I18nKeys.aiNameLevelAiLevel.str("Stockfish AI", level)
val name = lila.i18n.I18nKeys.aiNameLevelAiLevel.literalStr("Stockfish AI", level)
val rating = withRating ?? {
aiRating(level) ?? { r => s" ($r)" }
}
Expand Down
8 changes: 4 additions & 4 deletions app/templating/GameHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}

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

Expand All @@ -87,8 +87,8 @@ trait GameHelper { self: I18nHelper with UserHelper with AiHelper with StringHel
}

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

def playerUsername(player: Player, withRating: Boolean = true, withTitle: Boolean = true) =
Expand Down
2 changes: 1 addition & 1 deletion app/templating/I18hHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait I18nHelper {
implicit def lang(implicit ctx: UserContext) = ctx.lang

def transKey(key: String, args: Seq[Any] = Nil)(implicit lang: Lang): String =
Translator.transTo(key, args, lang)
Translator.literal(key, args, lang)

def i18nJsObjectMessage(keys: Seq[I18nKey])(implicit lang: Lang): JsObject =
i18nEnv.jsDump.keysToMessageObject(keys, lang)
Expand Down
5 changes: 3 additions & 2 deletions app/templating/SetupHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ trait SetupHelper { self: I18nHelper =>
variantTuple(chess.variant.FromPosition)

def translatedSpeedChoices(implicit ctx: Context) = Speed.limited map { s =>
val minutes = s.range.max / 60 + 1
(
s.id.toString,
s.toString + " - " + I18nKeys.lessThanNbMinutes(s.range.max / 60 + 1),
s.toString + " - " + I18nKeys.lessThanNbMinutes.pluralStr(minutes, minutes),
none
)
}
Expand Down Expand Up @@ -137,7 +138,7 @@ trait SetupHelper { self: I18nHelper =>

def translatedChallengeChoices(implicit ctx: Context) = List(
(Pref.Challenge.NEVER, I18nKeys.never.str()),
(Pref.Challenge.RATING, I18nKeys.ifRatingIsPlusMinusX(lila.pref.Pref.Challenge.ratingThreshold).toString()),
(Pref.Challenge.RATING, I18nKeys.ifRatingIsPlusMinusX.literalStr(lila.pref.Pref.Challenge.ratingThreshold)),
(Pref.Challenge.FRIEND, I18nKeys.onlyFriends.str()),
(Pref.Challenge.ALWAYS, I18nKeys.always.str())
)
Expand Down
12 changes: 6 additions & 6 deletions app/templating/UserHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ trait UserHelper { self: I18nHelper with StringHelper with NumberHelper =>

def userGameFilterTitleNoTag(info: UserInfo, filter: GameFilter)(implicit ctx: UserContext) = Html((filter match {
case GameFilter.All => info.user.count.game + " " + I18nKeys.gamesPlayed()
case GameFilter.Me => ctx.me ?? (me => I18nKeys.nbGamesWithYou.str(info.nbWithMe))
case GameFilter.Me => ctx.me ?? (me => I18nKeys.nbGamesWithYou.pluralSameStr(info.nbWithMe))
case GameFilter.Rated => info.nbRated + " " + I18nKeys.rated()
case GameFilter.Win => I18nKeys.nbWins(info.user.count.win)
case GameFilter.Loss => I18nKeys.nbLosses(info.user.count.loss)
case GameFilter.Draw => I18nKeys.nbDraws(info.user.count.draw)
case GameFilter.Win => I18nKeys.nbWins.pluralSameStr(info.user.count.win)
case GameFilter.Loss => I18nKeys.nbLosses.pluralSameStr(info.user.count.loss)
case GameFilter.Draw => I18nKeys.nbDraws.pluralSameStr(info.user.count.draw)
case GameFilter.Playing => info.nbPlaying + " playing"
case GameFilter.Bookmark => I18nKeys.nbBookmarks(info.nbBookmark)
case GameFilter.Imported => I18nKeys.nbImportedGames(info.nbImported)
case GameFilter.Bookmark => I18nKeys.nbBookmarks.pluralSameStr(info.nbBookmark)
case GameFilter.Imported => I18nKeys.nbImportedGames.pluralSameStr(info.nbImported)
case GameFilter.Search => Html(I18nKeys.advancedSearch.str().replaceFirst(" ", "\n"))
}).toString)

Expand Down
2 changes: 1 addition & 1 deletion app/views/account/email.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h1 class="lichess_title">
}
</h1>
@form("email").value.filter(_.nonEmpty).map { email =>
<p>@trans.youHaveAlreadyRegisteredTheEmail(email)</p>
<p>@trans.youHaveAlreadyRegisteredTheEmail.literal(email)</p>
}.getOrElse {
<p>@trans.emailIsOptional()</p>
<form action="@routes.Account.emailApply" method="POST">
Expand Down
2 changes: 1 addition & 1 deletion app/views/account/pref.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2>@trans.promoteToQueenAutomatically()</h2>
@base.form.radios(form("behavior.autoQueen"), translatedAutoQueenChoices)
</li>
<li>
<h2>@trans.claimDrawOnThreefoldRepetitionAutomatically("<a rel=\"nofollow\" href=\"http://en.wikipedia.org/wiki/Threefold_repetition\">", "</a>")</h2>
<h2>@trans.claimDrawOnThreefoldRepetitionAutomatically()</h2>
@base.form.radios(form("behavior.autoThreefold"), translatedAutoThreefoldChoices)
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/auth/passwordResetSent.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="content_box small_box signup">
<div class="signup_box">
<h1 class="lichess_title is-green text" data-icon="E">@trans.checkYourEmail()</h1>
<p>@trans.weHaveSentYouAnEmailTo(email)</p>
<p>@trans.weHaveSentYouAnEmailTo.literal(email)</p>
<p>@trans.ifYouDoNotSeeTheEmailCheckOtherPlaces()</p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/auth/signup.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1 class="lichess_title">@trans.signUp()</h1>
<li data-icon="">&nbsp;
@trans.computersAreNotAllowedToPlay()<br />
<small>
@trans.byRegisteringYouAgreeToBeBoundByOur(tosLink)
@trans.byRegisteringYouAgreeToBeBoundByOur.literal(tosLink)
</small>
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion app/views/base/captcha.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
<div class="checkmateSection">
<label>
<strong>@trans.colorPlaysCheckmateInOne(captcha.white.fold(trans.white, trans.black)())</strong><br />
<strong>@trans.colorPlaysCheckmateInOne.literal(captcha.white.fold(trans.white, trans.black).str())</strong><br />
<br />
@trans.thisIsAChessCaptcha()<br />
@trans.clickOnTheBoardToMakeYourMove()
Expand Down
2 changes: 1 addition & 1 deletion app/views/challenge/explanation.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p>
@views.html.game.variantLink(c.variant, variantName(c.variant)) • @modeName(c.mode)<br />
@c.daysPerTurn.map { days =>
<span class="text" data-icon=";">@{(days == 1).fold(trans.oneDay(), trans.nbDays(days))}</span>
<span class="text" data-icon=";">@{(days == 1).fold(trans.oneDay(), trans.nbDays.pluralSame(days))}</span>
}.getOrElse {
<span class="text" data-icon="p">@shortClockName(c.clock.map(_.config))</span>
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/coach/widget.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h1>
@c.user.seenAt.map { seen =>
<tr class="seen">
<th></th>
<td>@trans.lastSeenActive(momentFromNow(seen))</td>
<td>@trans.lastSeenActive.literal(momentFromNow(seen))</td>
</tr>
}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/forum/categ/show.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<td class="right">@topic.nbReplies.localize</td>
<td class="last_post">
@topic.lastPost.map { post =>
<a href="@routes.ForumTopic.show(categ.slug, topic.slug, topic.lastPage)#@post.number">@momentFromNow(post.createdAt)</a><br />@trans.by(authorLink(post))
<a href="@routes.ForumTopic.show(categ.slug, topic.slug, topic.lastPage)#@post.number">@momentFromNow(post.createdAt)</a><br />@trans.by.literal(authorLink(post))
}
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/forum/categ/table.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2 class="description">@categ.desc</h2>
<td class="last_post">
@categ.lastPost.map {
case (topic, post, page) => {
<a href="@routes.ForumTopic.show(categ.slug, topic.slug, page)#@post.number">@momentFromNow(post.createdAt)</a><br />@trans.by(authorName(post))
<a href="@routes.ForumTopic.show(categ.slug, topic.slug, page)#@post.number">@momentFromNow(post.createdAt)</a><br />@trans.by.literal(authorName(post))
}
}
</td>
Expand Down
2 changes: 1 addition & 1 deletion app/views/game/side.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
} else {
@game.clock.map(_.config.show).getOrElse {
@game.daysPerTurn.map { days =>
<span data-hint="@trans.correspondence()" class="hint--top">@{(days == 1).fold(trans.oneDay(), trans.nbDays(days))}</span>
<span data-hint="@trans.correspondence()" class="hint--top">@{(days == 1).fold(trans.oneDay(), trans.nbDays.pluralSame(days))}</span>
}.getOrElse {
<span data-hint="@trans.unlimited()" class="hint--top"></span>
}
Expand Down
4 changes: 2 additions & 2 deletions app/views/game/vstext.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
@pov.game.clock.map { c =>
<div class="center"><span data-icon="p"> @shortClockName(c.config)</span></div>
}.getOrElse {
@ctxOption.map { ctx =>
@ctxOption.map { implicit ctx =>
@pov.game.daysPerTurn.map { days =>
<div class="center">
<span data-hint="@trans.correspondence()(ctx)" class="hint--top">@{(days == 1).fold(trans.oneDay()(ctx), trans.nbDays(days)(ctx))}</span>
<span data-hint="@trans.correspondence()" class="hint--top">@{(days == 1).fold(trans.oneDay(), trans.nbDays.pluralSame(days))}</span>
</div>
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/views/game/widgets.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@if(g.imported) {
<span>IMPORT</span>
@g.pgnImport.flatMap(_.user).map { user =>
@trans.by(userIdLink(user.some, None, false))
@trans.by.literal(userIdLink(user.some, None, false))
}
@if(g.variant.exotic) {
Expand All @@ -50,7 +50,7 @@
} else {
@g.clock.map(_.config.show).getOrElse {
@g.daysPerTurn.map { days =>
<span data-hint="@trans.correspondence()" class="hint--top">@if(days == 1) {@trans.oneDay()} else {@trans.nbDays(days)}</span>
<span data-hint="@trans.correspondence()" class="hint--top">@if(days == 1) {@trans.oneDay()} else {@trans.nbDays.pluralSame(days)}</span>
}.getOrElse {
<span data-hint="@trans.unlimited()" class="hint--top"></span>
}
Expand Down
4 changes: 2 additions & 2 deletions app/views/lobby/home.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

@baseline = {
<a id="nb_connected_players" href="@routes.User.list">
@trans.nbPlayers("<strong>-,---</strong>")
@trans.nbPlayers.literal("<strong>-,---</strong>")
</a>
<a id="nb_games_in_play" href="@routes.Tv.games">
@trans.nbGamesInPlay(s"<span>${nbRounds}</span>")
@trans.nbGamesInPlay.plural(nbRounds, s"<span>${nbRounds}</span>")
</a>
@if(ctx.isMobileBrowser) {
@if(lila.common.HTTPRequest.isAndroid(ctx.req)) {
Expand Down
2 changes: 1 addition & 1 deletion app/views/mobile/home.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h2>@trans.fullFeatured()</h2>
<li>@trans.boardEditor()</li>
<li>Lichess TV</li>
<li>@trans.followAndChallengeFriends()</li>
<li>@trans.availableInNbLanguages(80)</li>
<li>@trans.availableInNbLanguages.pluralSame(80)</li>
</ul>
<div class="block">
<a href="https://github.com/veloce/lichobile">Lichess mobile</a> is built upon:
Expand Down
4 changes: 2 additions & 2 deletions app/views/relation/blocks.scala.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@(u: User, pag: Paginator[lila.relation.Related])(implicit ctx: Context)

@user.layout(title = u.username + " - " + trans.blocks(pag.nbResults)) {
@user.layout(title = u.username + " - " + trans.blocks.pluralSameStr(pag.nbResults)) {
<div class="content_box no_padding">
<h1>
@userLink(u, withOnline = false)
@trans.blocks(pag.nbResults)
@trans.blocks.pluralSame(pag.nbResults)
</h1>
@user.simpleTable(pag, routes.Relation.blocks())
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/relation/followers.scala.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@(u: User, pag: Paginator[lila.relation.Related], nbFollowing: Int)(implicit ctx: Context)

@user.layout(title = u.username + " - " + trans.nbFollowers(pag.nbResults)) {
@user.layout(title = u.username + " - " + trans.nbFollowers.pluralSameStr(pag.nbResults)) {
<div class="content_box no_padding">
<h1>
@userLink(u, withOnline = false)
@trans.nbFollowers(pag.nbResults)
@trans.nbFollowers.pluralSame(pag.nbResults)
&amp;
<a href="@routes.Relation.following(u.username)">@trans.nbFollowing(nbFollowing)</a>
<a href="@routes.Relation.following(u.username)">@trans.nbFollowing.pluralSame(nbFollowing)</a>
</h1>
@user.simpleTable(pag, routes.Relation.followers(u.username))
</div>
Expand Down
6 changes: 3 additions & 3 deletions app/views/relation/following.scala.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@(u: User, pag: Paginator[lila.relation.Related], nbFollowers: Int)(implicit ctx: Context)

@user.layout(title = u.username + " - " + trans.nbFollowing(pag.nbResults)) {
@user.layout(title = u.username + " - " + trans.nbFollowing.pluralSameStr(pag.nbResults)) {
<div class="content_box no_padding">
<h1>
@userLink(u, withOnline = false)
@trans.nbFollowing(pag.nbResults)
@trans.nbFollowing.pluralSame(pag.nbResults)
&amp;
<a href="@routes.Relation.followers(u.username)">@trans.nbFollowers(nbFollowers)</a>
<a href="@routes.Relation.followers(u.username)">@trans.nbFollowers.pluralSame(nbFollowers)</a>
</h1>
@user.simpleTable(pag, routes.Relation.following(u.username))
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/setup/ai.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="ai_info">
@ratings.map {
case (level, rating) => {
<div class="level_@level">@trans.aiNameLevelAiLevel("A.I.", level)</div>
<div class="level_@level">@trans.aiNameLevelAiLevel.literal("A.I.", level)</div>
}
}
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/setup/config.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h2>@title</h2>
<div class="ratings">
@lila.rating.PerfType.nonPuzzle.map { perfType =>
<div class="@perfType.key">
@trans.ratingX(s"""<strong data-icon="${perfType.iconChar}">${me.perfs(perfType.key).map(_.intRating).getOrElse("?")}</strong> ${perfType.name}""")
@trans.ratingX.literal(s"""<strong data-icon="${perfType.iconChar}">${me.perfs(perfType.key).map(_.intRating).getOrElse("?")}</strong> ${perfType.name}""")
</div>
}
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/simul/homepageSpotlight.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<img class="img icon" src="@staticUrl("images/fire-silhouette.svg")" />
<span class="content">
<span class="name">@s.name simul</span>
<span class="more">@trans.nbPlayers(s.applicants.size.localize) • @trans.join()</span>
<span class="more">@trans.nbPlayers.plural(s.applicants.size, s.applicants.size.localize) • @trans.join()</span>
</span>
</a>
47 changes: 32 additions & 15 deletions modules/i18n/src/main/I18nKey.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,51 @@ sealed trait I18nKey {

val key: String

def apply(args: Any*)(implicit ctx: UserContext): Html
def literalTo(lang: Lang, args: Any*): String

def str(args: Any*)(implicit ctx: UserContext): String
def pluralTo(lang: Lang, count: Count, args: Any*): String

def to(lang: Lang)(args: Any*): String
/* Implicit context convenience functions */

def en(args: Any*): String = to(enLang)(args: _*)
def literalStr(args: Any*)(implicit ctx: UserContext): String = literalTo(ctx.lang, args)

def pluralStr(count: Count, args: Any*)(implicit ctx: UserContext): String = pluralTo(ctx.lang, count, args)

def literal(args: Any*)(implicit ctx: UserContext): Html = Html(literalTo(ctx.lang, args))

def plural(count: Count, args: Any*)(implicit ctx: UserContext): Html = Html(pluralTo(ctx.lang, count, args))

/* Shortcuts */

def apply()(implicit ctx: UserContext): Html = literal()

def str()(implicit ctx: UserContext): String = literalStr()

// reuses the count as the single argument
// allows `plural(nb)` instead of `plural(nb, nb)`
def pluralSame(count: Int)(implicit ctx: UserContext): Html = plural(count, count)
def pluralSameStr(count: Int)(implicit ctx: UserContext): String = pluralStr(count, count)

/* English translations */

def literalEn(args: Any*): String = literalTo(enLang, args: _*)
def pluralEn(count: Count, args: Any*): String = pluralTo(enLang, count, args: _*)
}

final class Translated(val key: String) extends I18nKey {

def apply(args: Any*)(implicit ctx: UserContext): Html =
Translator.html(key, args.toList, ctx.lang)
def literalTo(lang: Lang, args: Any*): String =
Translator.literal(key, args, lang)

def str(args: Any*)(implicit ctx: UserContext): String =
Translator.str(key, args.toList, ctx.lang)

def to(lang: Lang)(args: Any*): String =
Translator.transTo(key, args.toList, lang)
def pluralTo(lang: Lang, count: Count, args: Any*): String =
Translator.plural(key, count, args, lang)
}

final class Untranslated(val key: String) extends I18nKey {

def apply(args: Any*)(implicit ctx: UserContext) = Html(key)

def str(args: Any*)(implicit ctx: UserContext) = key
def literalTo(lang: Lang, args: Any*) = key

def to(lang: Lang)(args: Any*) = key
def pluralTo(lang: Lang, count: Count, args: Any*) = key
}

object I18nKey {
Expand Down
6 changes: 3 additions & 3 deletions modules/i18n/src/main/JsDump.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ private[i18n] final class JsDump(path: String) {

def keysToObject(keys: Seq[I18nKey], lang: Lang) = JsObject {
keys.map { k =>
k.key -> JsString(k.to(lang)())
k.key -> JsString(k.literalTo(lang))
}
}

def keysToMessageObject(keys: Seq[I18nKey], lang: Lang) = JsObject {
keys.map { k =>
k.en() -> JsString(k.to(lang)())
k.literalEn() -> JsString(k.literalTo(lang))
}
}

Expand All @@ -30,7 +30,7 @@ private[i18n] final class JsDump(path: String) {

private def dumpFromKey(keys: Iterable[String], lang: Lang): String =
keys.map { key =>
""""%s":"%s"""".format(key, escape(Translator.str(key, Nil, lang)))
""""%s":"%s"""".format(key, escape(Translator.literal(key, Nil, lang)))
}.mkString("{", ",", "}")

private def writeRefs = writeFile(
Expand Down
Loading

0 comments on commit 6897b9e

Please sign in to comment.