Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 333
Blankman Avatar asked Dec 14 '15 01:12

Blankman


1 Answers

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))
like image 152
danielnixon Avatar answered Nov 20 '22 07:11

danielnixon