简体   繁体   中英

Play framework 2 - i18n in javascript files

I am using Scala Play Framework 2. I want multilanguage javascript files and it would be perfect to have the possibility to put Messages("title.items") inside javascript files.

To do so, I think we should create a new Asset controller that inject the Lang object. Is there a better way? Where could I find some resources about?

您可以使用Play2的JsMessages插件

I used it this way.

ApplicationController.scala .

class ApplicationController @Inject() (
  val messagesApi: MessagesApi
) extends Controller with I18nSupport {

  def messages(): EssentialAction = {
    Ok("window.messagesApi=".concat(Json.toJson(messagesApi.messages("en")).toString).concat(";")).as(JAVASCRIPT)
  }
}

routes

GET /messages.js controllers.ApplicationController.messages()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM