How to pass Messages when I inject MessageApi and use the I18nSupport Trait
Asked Answered
C

1

12

My controller actions use a custom action that looks like:

class ActionWithContext @Inject()(....)
  extends ActionBuilder[ContextAwareRequest] {

  def invokeBlock[A](request: Request[A], block: (ContextAwareRequest[A]) =>                   Future[Result]) = {
  ... 
  }

}

class ContextAwareRequest[A](val context: MyContext, request: Request[A]) extends WrappedRequest[A](request)

My controller looks like:

import play.api.i18n.{MessagesApi, I18nSupport, Messages}
import play.api.i18n.Messages.Implicits._
class WebsiteController @Inject() (val messagesApi: MessagesApi, actionWithContext: ActionWithContext, ..)
      extends BaseController with I18nSupport {


    def edit(websiteId: Int)  =  actionWithContext { request =>
        val model = ..
        Ok(views.html.backend.websites.edit(model)(request.context))
      }

    }

The view page looks like:

@(form: Form[controllers.WebsiteForm])(implicit context: MyContext, m: Messages) {


}

I am getting the error:

not enough arguments for method apply: (implicit context: com.example.services.components.MyContext, implicit m: play.api.i18n.Messages)play.twirl.api.HtmlFormat.Appendable in class edit.
[error] Unspecified value parameter m.
[error]     Ok(views.html.websites.edit(model)(request.context))
[error]                                

I tried passing in the messageApi like:

 Ok(views.html.websites.edit(model)(request.context, messageApi))

But I then got a type mismatch error between Messages and MessageApi.

What should I be doing?

P.S I am not even using messages in my views, but I am using the Form helpers which I guess use messages under the covers...

Cristencristi answered 14/12, 2015 at 1:23 Comment(0)
N
9

It looks like you're trying to pass one implicit parameter explicitly and the other implicitly. You have to either pass them both explicitly:

Ok(views.html.websites.edit(model)(request.context, implicitly[Messages]))

Or both implicitly:

implicit val context = request.context
Ok(views.html.websites.edit(model))
Novel answered 14/12, 2015 at 3:42 Comment(9)
Would it be possible to add the 'implicit val context = request.context in myActionWithContext somehow? Because every action will have this code and if I can avoid adding it to all my actions that would be great.Cristencristi
You should be able to write a trait that contains something like implicit def request2Context(implicit request: ContextAwareRequest): MyContext = request.context, then mix-in that trait similarly to how you're mixing in I18nSupport. See this for inspiration: github.com/playframework/playframework/blob/master/framework/…Novel
Or just add that def to your BaseController.Novel
If I am not using Messages in my view, why is it expecting it? It is because of the forms helpers?Cristencristi
Could I somehow do the implicitly[Messages] in my basecontroller also? not sure if I can inject in my basecontroller somehow?Cristencristi
I am getting this error now: class WebsiteController needs to be abstract, since method messagesApi in trait I18nSupport of type => play.api.i18n.MessagesApi is not definedCristencristi
Try replacing messagesApi: MessagesApi with val messagesApi: MessagesApi in your WebsiteController class definition.Novel
(As per playframework.com/documentation/2.4.x/…).Novel
Still getting the same error after making in a val.Cristencristi

© 2022 - 2024 — McMap. All rights reserved.