Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v1.1: Allow to export only a subset of the i18n keys

  • Loading branch information...
commit dc443898a90041326253f23d5effdc3381e49ea9 1 parent b2c5468
@julienrf authored
View
15 README.md
@@ -27,4 +27,19 @@ Then you can compute messages on client side:
```javascript
alert(Messages('greeting', 'World'));
+```
+
+A template tag is also defined:
+
+```html
+ @jsMessages("Messages")
+```
+
+Last but not least, you can export only a subset of your i18n keys:
+
+```scala
+ JsMessages.subset("Messages")(
+ "error.required",
+ "error.number"
+ )
```
View
2  jsmessages/build.sbt
@@ -1,6 +1,6 @@
name := "play-jsmessages"
-version := "1.0"
+version := "1.1"
organization := "com.github.julienrf"
View
39 jsmessages/src/main/scala/jsmessages/JsMessages.scala
@@ -27,19 +27,38 @@ object JsMessages {
*
* Note: This implementation does not handle quotes escaping in patterns (see http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html)
*/
- def apply(name: String = "Messages")(implicit app: Application, lang: Lang): String = {
+ def apply(name: String = "Messages")(implicit app: Application, lang: Lang): String = apply(name, allMessages)
+
+ /**
+ * Generates a JavaScript function able to compute localized messages for a given keys subset.
+ *
+ * Example:
+ *
+ * {{{
+ * JsMessages.subset("MyMessages")(
+ * "error.required",
+ * "error.number"
+ * )
+ * }}}
+ */
+ def subset(name: String = "Messages")(keys: String*)(implicit app: Application, lang: Lang): String = {
+ val messages = (for {
+ key <- keys
+ message <- allMessages.get(key)
+ } yield (key, message)).toMap
+ apply(name, messages)
+ }
+
+ def apply(name: String, messages: Map[String, String])(implicit app: Application, lang: Lang): String = {
import org.apache.commons.lang.StringEscapeUtils.escapeJavaScript
"""var %s=(function(){var ms={%s}; return function(k){var m=ms[k]||k;for(var i=1;i<arguments.length;i++){m=m.replace('{'+(i-1)+'}',arguments[i])} return m}})();""".format(
name,
- {
- val localizedMessages = Messages.messages.get(lang.code).getOrElse(Map.empty)
- val defaultMessages = Messages.messages.get("default").getOrElse(Map.empty)
- // The ++ operator is not commutative, beware!
- (for ((key, msg) <- defaultMessages ++ localizedMessages) yield {
- "'%s':'%s'".format(escapeJavaScript(key), escapeJavaScript(msg.replace("''", "'")))
- }).mkString(",")
- }
- )
+ (for ((key, msg) <- messages) yield {
+ "'%s':'%s'".format(escapeJavaScript(key), escapeJavaScript(msg.replace("''", "'")))
+ }).mkString(",")
+ )
}
+ private def allMessages(implicit app: Application, lang: Lang) =
+ Messages.messages.get("default").getOrElse(Map.empty) ++ Messages.messages.get(lang.code).getOrElse(Map.empty)
}
View
24 jsmessages/src/main/scala/jsmessages/html/jsMessages.scala
@@ -23,10 +23,24 @@ object jsMessages {
* greeting=Hello {0}!
* }}}
*/
- def apply(name: String = "Messages")(implicit app: play.api.Application, lang: play.api.i18n.Lang) = {
- Html(("""|<script type="text/javascript">
- |%s
- |</script>""").stripMargin.format(jsmessages.JsMessages(name)))
- }
+ def apply(name: String = "Messages")(implicit app: play.api.Application, lang: play.api.i18n.Lang) =
+ script(jsmessages.JsMessages(name))
+
+ /**
+ * Generates a JavaScript function able to compute localized messages for a given keys subset.
+ *
+ * Example:
+ *
+ * {{{
+ * @jsMessages.subset("MyMessages")(
+ * "error.required",
+ * "error.number"
+ * )
+ * }}}
+ */
+ def subset(name: String = "Messages")(keys: String*)(implicit app: play.api.Application, lang: play.api.i18n.Lang) =
+ script(jsmessages.JsMessages.subset(name)(keys: _*))
+
+ private def script(js: String) = Html("""<script type="text/javascript">%s</script>""".format(js))
}
View
1  sample/app/controllers/Application.scala
@@ -18,4 +18,5 @@ object Application extends Controller {
def jsMessages = Action { implicit request =>
Ok(JsMessages("Messages")).as(JAVASCRIPT)
}
+
}
View
2  sample/app/views/index2.scala.html
@@ -1,4 +1,4 @@
-@()(implicit request: RequestHeader, application: play.api.Application, lang: Lang)
+@()(implicit application: play.api.Application, lang: Lang)
@main {
@jsmessages.html.jsMessages("Messages")
View
2  sample/project/Build.scala
@@ -8,7 +8,7 @@ object ApplicationBuild extends Build {
val appVersion = "1.0"
val appDependencies = Seq(
- "com.github.julienrf" %% "play-jsmessages" % "1.0"
+ "com.github.julienrf" %% "play-jsmessages" % "1.1"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
Please sign in to comment.
Something went wrong with that request. Please try again.