Skip to content

Instantly share code, notes, and snippets.

@Timshel
Forked from playxamplez-admin/CODE
Last active December 20, 2015 01:58
Show Gist options
  • Save Timshel/6052433 to your computer and use it in GitHub Desktop.
Save Timshel/6052433 to your computer and use it in GitHub Desktop.
#play2.1 custom #MessagesPlugin to add messages from different sources
package play.api.i18n
import play.api._
import play.api.i18n._
import scala.collection.JavaConverters._
import scalax.file._
import scalax.io.JavaConverters._
/**
* Play Plugin for internationalisation.
*/
class CustomMessagesPlugin(app: Application) extends MessagesPlugin(app) {
def loadMessages(file: String): Map[String, String] = {
app.classloader.getResources(file).asScala.toList.reverse.map { messageFile =>
new Messages.MessagesParser(messageFile.asInput, messageFile.toString).parse.map { message =>
message.key -> message.pattern
}.toMap
}.foldLeft(Map.empty[String, String]) { _ ++ _ }
}
def messages = ( Lang.availables(app)
.map( l => ( l.code, "message.%s".format(l.code)) ) :+ ( ("default", "messages") ) )
.map { files =>
// Add custom loading here
(files._1, loadMessages(files._2) )
}.toMap
/**
* The underlying internationalisation API.
*/
override lazy val api = MessagesApi(messages)
/**
* Loads all configuration and message files defined in the classpath.
*/
override def onStart() = api
}
50:play.api.i18n.CustomMessagesPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment