All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.ext.i18n.MultiFormatMessagesModule.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package play.ext.i18n

import javax.inject.{Inject, Singleton}

import play.api.i18n._
import play.api.inject.Module
import play.api.{Configuration, Environment}

import play.ext.i18n.MessagesLoaders._

/** Messaging plugin for parsing files in various different formats
  *
  * @author Karel Cemus
  */
@Singleton
class MultiFormatMessagesApi @Inject()(implicit environment: Environment, configuration: Configuration, langs: Langs) extends DefaultMessagesApi(messages = {

  implicit val config = configuration
  implicit val env    = environment

  /** all files regardless the format */
  def allFiles: List[MessageFile] = enabledFormats.flatMap(format => MessageFile(format) ++ MessageFile(langs.availables, format))

  /** enabled loaders, disabled are dropped */
  def enabledFormats: List[Format] = supportedFormats.filter(_.isEnabled(configuration.get[Configuration]("play.i18n.formats")))

  /** map of supported loaders, mapping format name -> format loader */
  def supportedFormats = List(
    Format("properties", None, PropertyFileLoader),
    Format("yaml", Some("yaml"), YamlFileLoader)
  )

  allFiles.map { file =>
    file.key -> file.load
  }.foldLeft(Map.empty[String, Map[String, String]]) {
    case (merged, (lang, data)) if merged.contains(lang) =>
      // detect collisions and log them
      data.keys.foreach { key =>
        if (merged(lang).contains(key))
          log.warn(s"Localization key '$key' is defined in multiple files for language '$lang'.")
      }
      merged + (lang -> data.++(merged(lang)))
    case (merged, (key, data)) =>
      merged + (key -> data)
  }
}, langs = langs)

class MultiFormatMessagesModule extends Module {
  def bindings(environment: Environment, configuration: Configuration) = {
    Seq(
      bind[Langs].toProvider[DefaultLangsProvider],
      bind[MessagesApi].to[MultiFormatMessagesApi],
      bind[play.i18n.MessagesApi].toSelf,
      bind[play.i18n.Langs].toSelf
    )
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy