2016-12-02 37 views
0

我有一個庫,允許用戶在與Play 2.3一起使用的瀏覽器會話期間在英語和威爾士語之間切換。播放2.5 I18n - 切換語言

切換到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.4並檢查它是否有效。然後遷移到2.5。如果這不起作用,請嘗試縮小問題範圍,並嘗試確定代碼在哪一點不再按預期運行。 – rethab

回答

1

簡短的答案是你沒有聲明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) 

這就是你需要的一切

運行,這將首先在瀏覽器中使用的語言顯示。點擊一個鏈接將切換到所選語言。

enter image description here

enter image description here