我有一个库,允许用户在与Play 2.3一起使用的浏览器会话期间切换英语和威尔士语。
切换到Play 2.5后,此库不再有效。我曾尝试在https://www.playframework.com/documentation/2.5.x/ScalaI18N处遵循“文档”,但到目前为止,我没有运气。
到目前为止我的实施是:
包含开关的旋转模板
@import play.api.Play.current
@import play.api.i18n.Messages.Implicits._
@import uk.my-org.urls.Link
@(langMap : Map[String, Lang], langToCall : (String => Call), customClass: Option[String] = None)(implicit lang: Lang)
<p>Current = @lang</p>
<p class="@if(customClass.isDefined) {@customClass.get}">
@langMap.map { case (key: String, value: Lang) =>
@if(lang.code != value.code) {
@Link.toInternalPage(
id = Some(s"$key-switch"),
url = s"${langToCall(key)}",
value = Some(key.capitalize)
).toHtml
} else {
@key.capitalize
}
@if(key != langMap.last._1) { @Html(" | ") }
}
</p>
包含开关的页面
@import uk.my-org.exampleplay25.controllers.LanguageController
@import play.api.Application
@import play.api.i18n.Messages
@()(implicit request: Request[_], application: Application, messages: Messages, lang: Lang, messagesApi: MessagesApi)
<h1 id="message">Hello from example-play-25-frontend !</h1>
<h1>@Messages("test.message")</h1>
@clc = @{ Application.instanceCache[LanguageController].apply(application) }
@uk.my-org.exampleplay25.views.html.language_selection(clc.languageMap, clc.langToCall, Some("custom-class"))
SwitchingController
package uk.my-org.exampleplay25.controllers
import javax.inject.Inject
import play.api.Play
import play.api.i18n._
import play.api.mvc._
import uk.my-org.play.config.RunMode
class LanguageController @Inject()(implicit val messagesApi: MessagesApi, lang: Langs) extends Controller with I18nSupport with RunMode {
protected def fallbackURL: String = Play.current.configuration.getString(s"$env.language.fallbackUrl").getOrElse("/")
def languageMap: Map[String, Lang] = Map(
"english" -> Lang("en"),
"cymraeg" -> Lang("cy-GB"),
"french" -> Lang("fr")
)
def langToCall(lang: String): Call = routes.LanguageController.switchToLanguage(lang)
def switchToLanguage(language: String) = Action { implicit request =>
val lang = languageMap.getOrElse(language, LanguageUtils.getCurrentLang)
val redirectURL = request.headers.get(REFERER).getOrElse(fallbackURL)
Redirect(redirectURL).withLang(Lang.apply(lang.code)).flashing(LanguageUtils.FlashWithSwitchIndicator)
}
}
Application.conf
play.i18n.langs = [ "en", "cy", "fr" ]
应用路线
GET /hello-world @uk.my-org.exampleplay25.controllers.HelloWorld.helloWorld
GET /language/:lang @uk.my-org.exampleplay25.controllers.LanguageController.switchToLanguage(lang: String)
Hello World控制器
package uk.my-org.exampleplay25.controllers
import javax.inject.{Inject, Singleton}
import play.api.Play.current
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc._
import uk.my-org.play.frontend.controller.FrontendController
import scala.concurrent.Future
@Singleton
class HelloWorld @Inject()(implicit val messagesApi: MessagesApi) extends FrontendController with I18nSupport {
val helloWorld = Action.async { implicit request =>
Future.successful(Ok(uk.my-org.exampleplay25.views.html.helloworld.hello_world()))
}
}
谁能看到我做错了什么?
答案 0 :(得分:2)
简短的回答是,您没有声明cy-GB
作为一种语言,但您正试图切换到它。
答案很长,你的事情过于复杂。这是一个用最少的代码演示语言切换的例子。
模板
这将显示来自相关消息文件的消息,并显示从请求标头派生的lang
。
@()(implicit messages: Messages, lang: Lang)
<h3>@messages("greeting")</h3>
<p>Current = @lang</p>
<ul>
<li><a href="@routes.LanguageController.switchToLanguage("en")">English</a></li>
<li><a href="@routes.LanguageController.switchToLanguage("cy")">Welsh</a></li>
</ul>
配置
声明您要支持的语言:
play.i18n.langs = [ "en", "cy" ]
应用程序控制器
这里唯一特别的是I18nSupport
特质。
package controllers
import javax.inject.Inject
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, Controller}
import views.html.index
import scala.concurrent.{ExecutionContext, Future}
class FooController @Inject()(val messagesApi: MessagesApi,
exec: ExecutionContext) extends Controller with I18nSupport {
def home = Action.async { implicit request =>
Future {Ok(index())}(exec)
}
}
更改语言
同样,没有什么特别的,也不需要额外的支持。
package controllers
import javax.inject.Inject
import play.api.i18n._
import play.api.mvc._
class LanguageController @Inject()(val messagesApi: MessagesApi) extends Controller with I18nSupport {
def switchToLanguage(language: String) = Action { implicit request =>
Redirect(routes.FooController.home()).withLang(Lang(language))
}
}
翻译
conf/messages.en
greeting=Good morning
conf/messages.cy
greeting=Bore da
<强>路由强>
GET / controllers.FooController.home
GET /lang/:lang controllers.LanguageController.switchToLanguage(lang: String)
这就是你需要的一切
运行此功能最初将以您的浏览器使用的语言显示。单击链接将切换到所选语言。