-
-
Save Timshel/6052433 to your computer and use it in GitHub Desktop.
#play2.1 custom #MessagesPlugin to add messages from different sources
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 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 | |
} |
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
50:play.api.i18n.CustomMessagesPlugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment