Skip to content

Play 2.0 add-on allowing to compute localized messages on client side

Notifications You must be signed in to change notification settings

kordzik/play-jsmessages

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Play JsMessages library

This library allows to compute localized messages on client side, in Play 2.0 projects.

Usage (Scala)

Add the following dependency to your Play project:

  val appDependencies = Seq(
    "com.github.julienrf" %% "play-jsmessages" % "1.2.1"
  )
  val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
    resolvers += "julienrf.github.com" at "http://julienrf.github.com/repo/"
  )

Use the JsMessages object to export your application localized messages on client side:

  def jsMessages = Action { implicit request =>
    Ok(JsMessages(Some("window.Messages"))).as(JAVASCRIPT)
  }

It takes an optional namespace as parameter. You can use any valid JavaScript namespace, the generated function will then be assigned to this namespace. Use None if you only want to generate the function, without assigning it to a namespace.

Then you can compute messages on client side:

  alert(Messages('greeting', 'World'));

A template tag is also defined:

  @jsMessages(Some("window.Messages"))

Last but not least, you can export only a subset of your i18n keys:

  JsMessages.subset(Some("window.Messages"))(
    "error.required",
    "error.number"
  )

Usage (Java)

Add the following dependency to your Play project:

  val appDependencies = Seq(
    "com.github.julienrf" %% "play-jsmessages" % "1.2.1"
  )
  val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
    resolvers += "julienrf.github.com" at "http://julienrf.github.com/repo/"
  )

Use the JsMessages object to export your application localized messages on client side:

    public static Result jsMessages() {
        return ok(jsmessages.JsMessages.generate("window.Messages")).as("application/javascript");
    }

It takes an optional namespace as parameter. You can use any valid JavaScript namespace, the generated function will then be assigned to this namespace. Use null if you only want to generate the function, without assigning it to a namespace.

Then you can compute messages on client side:

  alert(Messages('greeting', 'World'));

A template tag is also defined:

  @jsmessages.JsMessages("window.Messages")(play.api.Play.current, lang)

Finally, you can only export a subset of your i18n keys:

    public static Result jsMessages() {
        return ok(jsmessages.JsMessages.subset("window.Messages",
            "error.required",
            "error.number"
        )).as("application/javascript");
    }

About

Play 2.0 add-on allowing to compute localized messages on client side

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published