3

我有一个国际化的 Scala Play 2.7.x WebApp 并且有通常的路线,例如

GET /               controllers.ApplicationController.index
GET /page/somePage/ controllers.SomeController.somePage
GET /contact        controllers.ContactController.view

现在我想添加一条新路由,它基本上将更改语言重定向到任何目标路由。我通过在上面添加一个额外的路由来实现这个用例routes

GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")

这个想法是,每次你做例如

http://localhost:9000/en => will go to home page in english
http://localhost:9000/en/contact => will go to contact page in english
http://localhost:9000/es => will go to home page in spanish
http://localhost:9000/es/contact => will go to contact page in spanish

等等。不幸的是,它并不总是有效,例如包含在/en/page/somePage/它之前的那个将无法正确匹配第一条规则:

 GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")

大概是因为中间/...我该如何解决?

为了完整起见,这是我的ApplicationController.langRedirect(...)实现:

def langRedirect(lang: String, target: String = "") = silhouette.UserAwareAction.async { implicit request =>
    Future.successful(Redirect("/" + target).withLang(Lang(lang)))
}
4

2 回答 2

3

使用Router.withPrefix,您可以为所有路由添加语言代码前缀。

这是一个例子。

package handlers

import javax.inject.Inject
import play.api.http._
import play.api.i18n.{ Langs, Lang }
import play.api.mvc.{ Handler, RequestHeader }

class I18nRequestHandler @Inject()(
    webCommands: play.core.WebCommands,
    optDevContext: play.api.OptionalDevContext,
    router: play.api.routing.Router,
    errorHandler: HttpErrorHandler,
    configuration: HttpConfiguration,
    filters: HttpFilters,
    langs: Langs)
  extends DefaultHttpRequestHandler(
    webCommands, optDevContext, router, errorHandler, configuration, filters) {

  def getLang(request: RequestHeader): Lang = {
    // Get the first path
    request.path.tail.split('/').headOption
      .flatMap(path => Lang.get(path))
      // language from the fist path, if it is in "play.i18n.langs (application.conf)"
      .filter(lang => langs.availables.exists(_ == lang))
      // Or preferred language, refereeing "Accept-Languages"
      .getOrElse(langs.preferred(request.acceptLanguages))
  }

  override def handlerForRequest(request: RequestHeader): (RequestHeader, Handler) = {
    // To use the language code from the path with MessagesApi,
    // Replace "Accept-Languages" to the language from the path.
    val requestWithLang = request.withHeaders(
      request.headers.replace(HeaderNames.ACCEPT_LANGUAGE -> getLang(request).code))
    super.handlerForRequest(requestWithLang)
  }

  override def routeRequest(request: RequestHeader): Option[Handler] = {
    val lang = getLang(request)
    request.path.tail.split('/').headOption
      // If the first path is right language code (if not, Not Found)
      .filter(_ == lang.code)
      // Route this request with language code prefix
      .flatMap(_ => router.withPrefix("/" + lang.code).handlerFor(request))
  }
}

要启用I18nRequestHandler,您必须将其添加到“application.conf”。

play.http.requestHandler = "handlers.I18nRequestHandler"

还将支持的语言添加到“application.conf”。

play.i18n.langs = [ "en", "es" ]

此代码强制所有路由具有语言代码前缀。如果您需要“/”之类的特殊路由来让用户选择其语言,请创建自定义路由并将其添加到routeRequest方法中。

希望这是你想要的;)

于 2019-10-13T15:00:33.687 回答
2

OK 找到了一个可能的解决方案,即添加第二条顶级路线,该路线将采用任何可能的目标,包括/,我的routes文件顶部现在如下所示:

GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
GET /$lang<(en|es)>/*target controllers.ApplicationController.langRedirect(lang: String, target: String = "")    

GET /               controllers.ApplicationController.index
GET /page/somePage/ controllers.SomeController.somePage
GET /contact        controllers.ContactController.view

为什么我需要两个?因为首页只能http://localhost:9000/en和不能http://localhost:9000/en/

但是,我很乐意学习(并接受)更好/更简单的解决方案。

于 2019-10-09T17:56:04.573 回答