Last active
December 15, 2015 19:40
-
-
Save sam/745037dff20683e8c5ea to your computer and use it in GitHub Desktop.
How to provide your own custom MessagesApi in PlayFramework when using a "base" subproject for branded projects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wieck.base | |
package controllers | |
import play.api.{Application, Play, mvc, i18n}, mvc._, i18n._ | |
abstract class ApplicationController extends Controller { | |
private val messagesApiCache = Application.instanceCache[BaseMessagesApi] | |
implicit def messages(implicit lang: Lang): Messages = new Messages(lang, messagesApiCache(Play.current)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wieck.base | |
import javax.inject.Inject | |
import play.api.{Configuration, Environment, i18n}, i18n.{Langs, DefaultMessagesApi} | |
class BaseMessagesApi @Inject() (environment: Environment, configuration: Configuration, langs: Langs) extends DefaultMessagesApi(environment, configuration, langs) { | |
override protected def loadAllMessages: Map[String, Map[String, String]] = { | |
langs.availables.map(_.code).map { lang => | |
(lang, loadMessages("messages." + lang)) | |
}.toMap | |
// The following line is the only deviation from the DefaultMessagesApi. It loads your base messages, | |
// then merges your customized messages over them. | |
.+("default" -> (loadMessages("messages.base") ++ loadMessages("messages"))) | |
.+("default.play" -> loadMessages("messages.default")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment