Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Play/Scala中的隐式参数和组合视图_Scala_Playframework 2.4 - Fatal编程技术网

Play/Scala中的隐式参数和组合视图

Play/Scala中的隐式参数和组合视图,scala,playframework-2.4,Scala,Playframework 2.4,这个问题建立在我发现的一个案例之上 index.scala.html @(text: String)(implicit messages: Messages) @main("Fancy title") { <h1>@messages("header.index")</h1> <div>@text</div> } @(title: String)(content: Html)(implicit messages: Messages)

这个问题建立在我发现的一个案例之上

index.scala.html

@(text: String)(implicit messages: Messages)

@main("Fancy title") {
  <h1>@messages("header.index")</h1>
  <div>@text</div>
}
@(title: String)(content: Html)(implicit messages: Messages)
  <html>
  <head>
    <title>@title</title>
  </head>
  <body>
    <h1>@messages("header.main")</h1>
    @content
  <body>
</html>
class Application @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport { 
   def index = Action { implicit request => 
      Ok(views.html.index("blah bla blah")) 
}
@(文本:字符串)(隐式消息:消息)
@主要(“花式标题”){
@消息(“header.index”)
@正文
}
main.scala.html

@(text: String)(implicit messages: Messages)

@main("Fancy title") {
  <h1>@messages("header.index")</h1>
  <div>@text</div>
}
@(title: String)(content: Html)(implicit messages: Messages)
  <html>
  <head>
    <title>@title</title>
  </head>
  <body>
    <h1>@messages("header.main")</h1>
    @content
  <body>
</html>
class Application @Inject() (val messagesApi: MessagesApi) extends Controller with I18nSupport { 
   def index = Action { implicit request => 
      Ok(views.html.index("blah bla blah")) 
}
@(标题:字符串)(内容:Html)(隐式消息:消息)
@头衔
@消息(“header.main”)
@内容
在这个例子中,我有索引调用main,我希望两者都能访问消息

编译器在索引中给我“找不到参数消息的隐式值:play.api.i18n.messages”,但如果我从main中删除隐式参数声明,则索引工作正常并获取消息。似乎编译器告诉我它不知道如何传递隐式参数


在尝试解决方法之前,我想了解为什么这不起作用。

在Play 2.4中,您需要在控制器中插入MessageAPI,并在操作中调用首选菜单来创建消息。
如果你把它定义为在你的行动中隐含的。然后一切都会好起来。

首先,让我为控制器添加代码,以使我的原始示例更清晰

Application.scala(原件)

在更详细地研究Play Framework文档()之后,我了解了从模板中访问消息的另一种方法(可能也更干净)

Application.scala(新)

index.scala.html(新)

@import play.i18n_
@(文本:字符串)
@主要(“花式标题”){
@Messages.get(“header.index”)
@正文
}
main.scala.html(新)

@import play.i18n_
@(标题:字符串)(内容:Html)
@头衔
@Messages.get(“header.main”)
@内容
控制器不再需要所有额外的基础设施

视图需要添加导入语句,但会丢失隐式param声明

使用
@Messages.get(“xyz”)
而不是
@Messages(“xyz”)
访问消息



目前,这种方法适合我的需要。

Play framework将隐式地将您的请求转换为MessagesApi以供查看。但是,您确实需要在控制器方法中包含
request=>
隐式参数。在控制器中还包括
I18nSupport
特性

import play.api.i18n.{MessagesApi, I18nSupport}

@Singleton
class HomeController @Inject() (cc: ControllerComponents)
extends AbstractController(cc) with I18nSupport {

  def showIndex = Action { implicit request =>
    Ok(views.html.index("All the langs"))
  }
}

嗨,索罗斯,正如我在问题中提到的,我之所以能做到这一点,部分是因为我完全按照你说的做了。这是我的控制器:
class Application@Inject()(val-messagesApi:messagesApi)使用I18nSupport{def index=Action{implicit request=>Ok(views.html.index(“bla bla bla”))扩展控制器
当我试图在main中使用由控制器调用的索引调用的消息时,问题就出现了。我正在寻找一种干净的方法,用一些全局数据(在本例中是消息)注入所有嵌套视图您在创建消息的操作中调用首选菜单的确切含义是什么?此解决方案更简洁,但是,当您想要更改语言时,您该如何做?