scalascala-implicits

Why am I getting "could not find implicit value for parameter messages: play.api.i18n.Messages"


I am getting the above compilcation error on Messages("title")

import play.api.i18n.Messages
import play.api.mvc._
import scala.concurrent.Future 

trait ApplicationController extends Controller {

  def get = Action.async {
    implicit request => Future.successful(Ok(views.html.index(Messages("title"))))
  }
}

object ApplicationController extends ApplicationController

my messages file in the conf folder of the project contains the following

 title = hello

and my template has takes the following in case you are wondering:

@(title: String)

Why am I getting this compilation error?


Solution

  • You need to inject it in Play 2.5. For example here is how declaration of one of my controllers looks like:

    import play.api.i18n.MessagesApi
    import javax.inject._
    
    class ApplicationController @Inject()(
                                               val messagesApi:MessagesApi,
                                               val env:Environment[User, CookieAuthenticator],
                                               implicit val webJarAssets:WebJarAssets,
                                               val timeZoneItemService:TimeZoneItemService,
                                               val userService: UserService,
                                               authInfoRepository: AuthInfoRepository,
                                               passwordHasher: PasswordHasher
                                             )
    

    You can read more on this here.