Ramin Ramin - 1 year ago 299
Scala Question

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download