lila/app/controllers/I18n.scala

50 lines
1.4 KiB
Scala
Raw Normal View History

2013-04-09 12:58:34 -06:00
package controllers
2017-05-26 09:15:15 -06:00
import play.api.data._
import play.api.data.Forms._
import play.api.libs.json.Json
2013-04-09 12:58:34 -06:00
import lila.app._
2019-12-04 16:39:16 -07:00
import lila.common.HTTPRequest
2013-04-09 12:58:34 -06:00
2019-12-04 16:39:16 -07:00
final class I18n(env: Env) extends LilaController(env) {
2013-04-09 12:58:34 -06:00
2017-05-26 11:16:30 -06:00
private def toLang = lila.i18n.I18nLangPicker.byStr _
2017-05-26 09:15:15 -06:00
private val form = Form(single("lang" -> text.verifying { code =>
2017-05-26 11:16:30 -06:00
toLang(code).isDefined
2017-05-26 09:15:15 -06:00
}))
def select = OpenBody { implicit ctx =>
implicit val req = ctx.body
2017-05-26 09:15:15 -06:00
form.bindFromRequest.fold(
_ => notFound,
2017-05-26 09:15:15 -06:00
code => {
2017-05-26 11:16:30 -06:00
val lang = toLang(code) err "Universe is collapsing"
ctx.me.filterNot(_.lang contains lang.code).?? { me =>
2019-12-04 18:47:46 -07:00
env.user.repo.setLang(me.id, lang.code)
2017-05-26 11:16:30 -06:00
} >> negotiate(
html = {
val redir = Redirect {
HTTPRequest.referer(ctx.req).fold(routes.Lobby.home.url) { str =>
try {
2017-05-26 12:33:45 -06:00
val pageUrl = new java.net.URL(str)
val path = pageUrl.getPath
val query = pageUrl.getQuery
if (query == null) path
else path + "?" + query
} catch {
case e: java.net.MalformedURLException => routes.Lobby.home.url
}
}
2015-04-02 15:03:26 -06:00
}
2019-12-04 16:39:16 -07:00
if (ctx.isAnon) redir.withCookies(env.lilaCookie.session("lang", lang.code))
2017-05-26 11:16:30 -06:00
else redir
}.fuccess,
api = _ => Ok(Json.obj("lang" -> lang.code)).fuccess
)
}
)
2014-12-31 09:15:34 -07:00
}
2013-04-09 12:58:34 -06:00
}