├── samples ├── java │ ├── public │ │ ├── stylesheets │ │ │ └── main.css │ │ ├── images │ │ │ └── favicon.png │ │ └── javascripts │ │ │ └── hello.js │ ├── app │ │ ├── views │ │ │ ├── index.scala.html │ │ │ └── main.scala.html │ │ └── controllers │ │ │ └── HomeController.java │ ├── conf │ │ ├── messages.en.conf │ │ ├── routes │ │ ├── application.conf │ │ └── logback.xml │ ├── README.md │ ├── project │ │ ├── build.properties │ │ └── plugins.sbt │ ├── build.sbt │ └── test │ │ └── IntegrationTest.java └── scala │ ├── public │ ├── stylesheets │ │ └── main.css │ ├── images │ │ └── favicon.png │ └── javascripts │ │ └── hello.js │ ├── conf │ ├── messages.en.conf │ ├── routes │ ├── application.conf │ └── logback.xml │ ├── app │ ├── views │ │ ├── index.scala.html │ │ └── main.scala.html │ └── controllers │ │ └── HomeController.scala │ ├── README.md │ ├── project │ ├── build.properties │ └── plugins.sbt │ ├── build.sbt │ └── test │ ├── IntegrationSpec.scala │ └── ApplicationSpec.scala ├── project ├── build.properties └── plugins.sbt ├── version.sbt ├── scripts ├── testJavaSample └── testScalaSample ├── src ├── test │ ├── resources │ │ ├── messages.pt-BR.conf │ │ ├── messages.pt.conf │ │ ├── messages.conf │ │ └── messages.en.conf │ └── scala │ │ └── com │ │ └── marcospereira │ │ └── play │ │ └── i18n │ │ └── HoconMessagesApiSpec.scala └── main │ └── scala │ └── com │ └── marcospereira │ └── play │ └── i18n │ └── HoconMessagesApi.scala ├── .gitignore ├── .travis.yml ├── appveyor.yml ├── README.md └── LICENSE /samples/java/public/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /samples/scala/public/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version = 0.13.15 -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | version in ThisBuild := "1.0.2-SNAPSHOT" 2 | -------------------------------------------------------------------------------- /scripts/testJavaSample: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | cd samples/java 4 | sbt test 5 | -------------------------------------------------------------------------------- /scripts/testScalaSample: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | cd samples/scala 4 | sbt test 5 | -------------------------------------------------------------------------------- /samples/java/app/views/index.scala.html: -------------------------------------------------------------------------------- 1 | @(message: String) 2 | @main(Messages("index.title")) { 3 | @message 4 | } 5 | -------------------------------------------------------------------------------- /src/test/resources/messages.pt-BR.conf: -------------------------------------------------------------------------------- 1 | test { 2 | messages { 3 | simple = "Oi" 4 | parameters = "Oi, {0}" 5 | } 6 | } -------------------------------------------------------------------------------- /src/test/resources/messages.pt.conf: -------------------------------------------------------------------------------- 1 | test { 2 | messages { 3 | simple = "Ola" 4 | parameters = "Ola, {0}" 5 | } 6 | } -------------------------------------------------------------------------------- /samples/java/conf/messages.en.conf: -------------------------------------------------------------------------------- 1 | index { 2 | title = "This is the title page" 3 | welcome = "Your new application is ready" 4 | } -------------------------------------------------------------------------------- /samples/java/public/images/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcospereira/play-i18n-hocon/HEAD/samples/java/public/images/favicon.png -------------------------------------------------------------------------------- /samples/java/public/javascripts/hello.js: -------------------------------------------------------------------------------- 1 | if (window.console) { 2 | console.log("Welcome to your Play application's JavaScript!"); 3 | } 4 | -------------------------------------------------------------------------------- /samples/scala/conf/messages.en.conf: -------------------------------------------------------------------------------- 1 | index { 2 | title = "This is the title page" 3 | welcome = "Your new application is ready, {0}." 4 | } -------------------------------------------------------------------------------- /samples/scala/public/images/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/marcospereira/play-i18n-hocon/HEAD/samples/scala/public/images/favicon.png -------------------------------------------------------------------------------- /samples/scala/public/javascripts/hello.js: -------------------------------------------------------------------------------- 1 | if (window.console) { 2 | console.log("Welcome to your Play application's JavaScript!"); 3 | } 4 | -------------------------------------------------------------------------------- /samples/scala/app/views/index.scala.html: -------------------------------------------------------------------------------- 1 | @(message: String)(implicit messages: Messages) 2 | 3 | @main(messages("index.title")) { 4 | @message 5 | } 6 | -------------------------------------------------------------------------------- /src/test/resources/messages.conf: -------------------------------------------------------------------------------- 1 | test { 2 | messages { 3 | simple = "Hello" 4 | parameters = "Hello, {0}" 5 | } 6 | missing = "Ok at the default file" 7 | } -------------------------------------------------------------------------------- /src/test/resources/messages.en.conf: -------------------------------------------------------------------------------- 1 | test { 2 | messages { 3 | simple = "Hello" 4 | parameters = "Hello, {0}" 5 | } 6 | resolve = ${test.messages.simple} 7 | } -------------------------------------------------------------------------------- /samples/java/README.md: -------------------------------------------------------------------------------- 1 | ## Sample Java Application 2 | 3 | A simple play-java application to test the module. 4 | 5 | It just has a test that loads the messages and check its values. -------------------------------------------------------------------------------- /samples/java/project/build.properties: -------------------------------------------------------------------------------- 1 | #Activator-generated Properties 2 | #Wed May 04 18:33:03 BRT 2016 3 | template.uuid=28ae6884-7b61-401c-834c-704789d1228a 4 | sbt.version=0.13.15 5 | -------------------------------------------------------------------------------- /samples/scala/README.md: -------------------------------------------------------------------------------- 1 | ## Sample Scala Application 2 | 3 | A simple play-scala application to test the module. 4 | 5 | It just has a test that loads the messages and check its values. -------------------------------------------------------------------------------- /samples/scala/project/build.properties: -------------------------------------------------------------------------------- 1 | #Activator-generated Properties 2 | #Wed May 04 18:33:13 BRT 2016 3 | template.uuid=15c371e1-78e0-429a-84ff-11b1d09dd4a2 4 | sbt.version=0.13.15 5 | -------------------------------------------------------------------------------- /samples/java/conf/routes: -------------------------------------------------------------------------------- 1 | GET / controllers.HomeController.index 2 | GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) 3 | -------------------------------------------------------------------------------- /samples/scala/conf/routes: -------------------------------------------------------------------------------- 1 | GET / controllers.HomeController.index 2 | GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore Play! working directory # 2 | bin/ 3 | /db 4 | .eclipse 5 | /lib/ 6 | /logs/ 7 | /modules 8 | /project/target 9 | /project/project 10 | /target 11 | tmp/ 12 | test-result 13 | server.pid 14 | *.eml 15 | /dist/ 16 | .cache 17 | samples/**/target 18 | samples/**/logs 19 | -------------------------------------------------------------------------------- /samples/java/build.sbt: -------------------------------------------------------------------------------- 1 | name := """java""" 2 | 3 | version := "1.0-SNAPSHOT" 4 | 5 | lazy val root = (project in file(".")).enablePlugins(PlayJava) 6 | 7 | scalaVersion := "2.12.2" 8 | 9 | libraryDependencies ++= Seq( 10 | guice, 11 | "com.github.marcospereira" %% "play-hocon-i18n" % "1.0.0-SNAPSHOT" 12 | ) 13 | -------------------------------------------------------------------------------- /samples/java/conf/application.conf: -------------------------------------------------------------------------------- 1 | play.http.secret.key = "changeme" 2 | 3 | play.modules { 4 | # Disable built-in i18n module 5 | disabled += play.api.i18n.I18nModule 6 | 7 | # Enable Hocon module 8 | enabled += com.marcospereira.play.i18n.HoconI18nModule 9 | } 10 | 11 | play.i18n { 12 | langs = [ "en" ] 13 | } -------------------------------------------------------------------------------- /samples/scala/conf/application.conf: -------------------------------------------------------------------------------- 1 | play.http.secret.key = "changeme" 2 | 3 | play.modules { 4 | # Disable built-in i18n module 5 | disabled += play.api.i18n.I18nModule 6 | 7 | # Enable Hocon module 8 | enabled += com.marcospereira.play.i18n.HoconI18nModule 9 | } 10 | 11 | play.i18n { 12 | langs = [ "en" ] 13 | } -------------------------------------------------------------------------------- /samples/scala/build.sbt: -------------------------------------------------------------------------------- 1 | name := """scala""" 2 | 3 | version := "1.0-SNAPSHOT" 4 | 5 | lazy val root = (project in file(".")).enablePlugins(PlayScala) 6 | 7 | scalaVersion := "2.12.2" 8 | 9 | libraryDependencies ++= Seq( 10 | guice, 11 | "com.github.marcospereira" %% "play-hocon-i18n" % "1.0.0-SNAPSHOT", 12 | "org.scalatestplus.play" %% "scalatestplus-play" % "3.0.0" % Test 13 | ) 14 | 15 | resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases" 16 | -------------------------------------------------------------------------------- /samples/scala/test/IntegrationSpec.scala: -------------------------------------------------------------------------------- 1 | import play.api.test._ 2 | import play.api.test.Helpers._ 3 | 4 | import org.scalatestplus.play._ 5 | import org.scalatestplus.play.guice._ 6 | 7 | class IntegrationSpec extends PlaySpec with GuiceOneServerPerTest with OneBrowserPerTest with HtmlUnitFactory { 8 | "Application" should { 9 | "work from within a browser" in { 10 | go to ("http://localhost:" + port) 11 | pageSource must include ("This is the title page") 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | sudo: false 3 | jdk: 4 | - oraclejdk8 5 | script: 6 | - sbt clean coverage test coverageReport codacyCoverage publishLocal 7 | - ./scripts/testJavaSample 8 | - ./scripts/testScalaSample 9 | cache: 10 | directories: 11 | - $HOME/.ivy2/cache 12 | before_cache: 13 | # Delete all ivydata files since ivy touches them on each build 14 | - find $HOME/.ivy2/cache -name "ivydata-*.properties" | xargs rm 15 | after_success: 16 | - bash <(curl -s https://codecov.io/bash) -------------------------------------------------------------------------------- /samples/scala/app/controllers/HomeController.scala: -------------------------------------------------------------------------------- 1 | package controllers 2 | 3 | import javax.inject._ 4 | 5 | import play.api._ 6 | import play.api.mvc._ 7 | import play.api.i18n._ 8 | 9 | @Singleton 10 | class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController with I18nSupport { 11 | 12 | def index = Action { implicit request: RequestHeader => 13 | implicit val lang = messagesApi.preferred(request.acceptLanguages).lang 14 | Ok(views.html.index(messagesApi("index.welcome", "marcos"))) 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /samples/scala/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | // The Play plugin 2 | addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.0") 3 | 4 | // web plugins 5 | 6 | addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") 7 | 8 | addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0") 9 | 10 | addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3") 11 | 12 | addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7") 13 | 14 | addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0") 15 | 16 | addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") 17 | 18 | addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.2") 19 | -------------------------------------------------------------------------------- /samples/java/app/controllers/HomeController.java: -------------------------------------------------------------------------------- 1 | package controllers; 2 | 3 | import javax.inject.Inject; 4 | 5 | import play.mvc.*; 6 | import play.i18n.*; 7 | 8 | import views.html.*; 9 | 10 | public class HomeController extends Controller { 11 | 12 | private MessagesApi messagesApi; 13 | 14 | @Inject 15 | public HomeController(MessagesApi messagesApi) { 16 | this.messagesApi = messagesApi; 17 | } 18 | 19 | public Result index() { 20 | Lang lang = Http.Context.current().lang(); 21 | return ok(index.render(messagesApi.get(lang, "index.welcome"))); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /samples/java/app/views/main.scala.html: -------------------------------------------------------------------------------- 1 | @(title: String)(content: Html) 2 | 3 | 4 | 5 | 6 | @* Here's where we render the page title `String`. *@ 7 | @title 8 | 9 | 10 | 11 | 12 | 13 | @content 14 | 15 | 16 | -------------------------------------------------------------------------------- /samples/scala/app/views/main.scala.html: -------------------------------------------------------------------------------- 1 | @(title: String)(content: Html) 2 | 3 | 4 | 5 | 6 | @* Here's where we render the page title `String`. *@ 7 | @title 8 | 9 | 10 | 11 | 12 | 13 | @content 14 | 15 | 16 | -------------------------------------------------------------------------------- /samples/java/test/IntegrationTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.*; 2 | 3 | import play.mvc.*; 4 | import play.test.*; 5 | 6 | import static play.test.Helpers.*; 7 | import static org.junit.Assert.*; 8 | 9 | import static org.fluentlenium.core.filter.FilterConstructor.*; 10 | 11 | public class IntegrationTest { 12 | 13 | @Test 14 | public void test() { 15 | running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, browser -> { 16 | browser.goTo("http://localhost:3333"); 17 | assertTrue(browser.pageSource().contains("Your new application is ready")); 18 | }); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | logLevel := Level.Warn 2 | 3 | resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven" 4 | 5 | resolvers += "Typesafe Repository" at "https://repo.typesafe.com/typesafe/releases/" 6 | 7 | addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.0") 8 | 9 | // Code formatting 10 | addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.6.0") 11 | 12 | // Release plugins 13 | addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") 14 | addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1") 15 | addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.5") 16 | 17 | // Code coverage plugins 18 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.0") 19 | 20 | addSbtPlugin("com.codacy" % "sbt-codacy-coverage" % "1.3.8") 21 | -------------------------------------------------------------------------------- /samples/scala/test/ApplicationSpec.scala: -------------------------------------------------------------------------------- 1 | import play.api.test._ 2 | import play.api.test.Helpers._ 3 | 4 | import org.scalatestplus.play._ 5 | import org.scalatestplus.play.guice._ 6 | 7 | class ApplicationSpec extends PlaySpec with GuiceOneAppPerTest { 8 | 9 | "Routes" should { 10 | "send 404 on a bad request" in { 11 | route(app, FakeRequest(GET, "/boum")).map(status(_)) mustBe Some(NOT_FOUND) 12 | } 13 | } 14 | 15 | "HomeController" should { 16 | "render the index page" in { 17 | val home = route(app, FakeRequest(GET, "/")).get 18 | status(home) mustBe OK 19 | contentType(home) mustBe Some("text/html") 20 | contentAsString(home) must include ("Your new application is ready") 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # Adapted from https://github.com/sbt/sbt-native-packager/blob/master/appveyor.yml 2 | version: '{build}' 3 | branches: 4 | except: 5 | - gh-pages 6 | skip_tags: true 7 | os: Windows Server 2012 8 | install: 9 | - ps: | 10 | Add-Type -AssemblyName System.IO.Compression.FileSystem 11 | if (!(Test-Path -Path "C:\sbt" )) { 12 | (new-object System.Net.WebClient).DownloadFile( 13 | 'https://dl.bintray.com/sbt/native-packages/sbt/0.13.11/sbt-0.13.11.zip', 14 | 'C:\sbt-bin.zip' 15 | ) 16 | [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\sbt-bin.zip", "C:\sbt") 17 | } 18 | - cmd: SET PATH=C:\sbt\sbt\bin;%JAVA_HOME%\bin;%PATH% 19 | - cmd: SET SBT_OPTS=-XX:MaxPermSize=2g -Xmx4g 20 | - cmd: SET COURSIER_NO_TERM=1 21 | build_script: 22 | - sbt clean compile publishLocal 23 | test_script: 24 | - sbt clean coverage test coverageReport codacyCoverage 25 | after_test: 26 | - "SET PATH=C:\\Python34;C:\\Python34\\Scripts;%PATH%" 27 | - pip install codecov 28 | - codecov 29 | cache: 30 | - C:\sbt\ 31 | - C:\Users\appveyor\.ivy2 32 | - C:\Users\appveyor\.sbt -------------------------------------------------------------------------------- /samples/java/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | // The Play plugin 2 | addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.0") 3 | 4 | // Web plugins 5 | addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") 6 | addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0") 7 | addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3") 8 | addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7") 9 | addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0") 10 | addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") 11 | addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.2") 12 | 13 | // Play enhancer - this automatically generates getters/setters for public fields 14 | // and rewrites accessors of these fields to use the getters/setters. Remove this 15 | // plugin if you prefer not to have this feature, or disable on a per project 16 | // basis using disablePlugins(PlayEnhancer) in your build.sbt 17 | addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0") 18 | 19 | // Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using 20 | // enablePlugins(PlayEbean). 21 | // addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0") 22 | -------------------------------------------------------------------------------- /samples/java/conf/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ${application.home:-.}/logs/application.log 8 | 9 | %date [%level] from %logger in %thread - %message%n%xException 10 | 11 | 12 | 13 | 14 | 15 | %coloredLevel %logger{15} - %message%n%xException{10} 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /samples/scala/conf/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ${application.home:-.}/logs/application.log 8 | 9 | %date [%level] from %logger in %thread - %message%n%xException 10 | 11 | 12 | 13 | 14 | 15 | %coloredLevel %logger{15} - %message%n%xException{10} 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Play I18n using HOCON 2 | 3 | [![Build Status](https://travis-ci.org/marcospereira/play-i18n-hocon.svg?branch=master)](https://travis-ci.org/marcospereira/play-i18n-hocon) [![Build status](https://ci.appveyor.com/api/projects/status/n5ykw0wuq04rpd5a?svg=true)](https://ci.appveyor.com/project/marcospereira/play-i18n-hocon) 4 | [![codecov](https://codecov.io/gh/marcospereira/play-i18n-hocon/branch/master/graph/badge.svg)](https://codecov.io/gh/marcospereira/play-i18n-hocon) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/af05a1d033aa4256af5329a6f4711721)](https://www.codacy.com/app/marcospereira/play-i18n-hocon?utm_source=github.com&utm_medium=referral&utm_content=marcospereira/play-i18n-hocon&utm_campaign=Badge_Grade) [![Maven](https://img.shields.io/maven-central/v/com.github.marcospereira/play-hocon-i18n_2.12.svg)](http://mvnrepository.com/artifact/com.github.marcospereira/play-hocon-i18n_2.12) 5 | 6 | 7 | [HOCON](https://github.com/typesafehub/config/blob/v1.3.0/HOCON.md) (Human-Optimized Config Object Notation) and Typesafe Config are the standard way to [configure Play applications](https://www.playframework.com/documentation/2.5.x/Configuration). But, for Internationalization, Play uses [Java Properties](https://docs.oracle.com/javase/tutorial/essential/environment/properties.html) which don't have a syntax to structure an tree of keys used to i18n. 8 | 9 | This plugin offers that by using HOCON as the language for I18n too, so your `messages` files will be like: 10 | 11 | ```HOCON 12 | pages { 13 | signup { 14 | title = "The Signup page" 15 | form { 16 | title = "The signup form" 17 | name = "Type your name" 18 | email = "Type your email" 19 | password = "Type your password" 20 | submit = "Signup now" 21 | } 22 | } 23 | } 24 | ``` 25 | 26 | This is not meant to be used as a drop-in replacement to default built-in module since Java Properties syntax is not compatible with HOCON. 27 | 28 | ## How to use 29 | 30 | Just follow the steps below: 31 | 32 | ### Add Module Dependency 33 | 34 | Add the dependency to your `build.sbt` file: 35 | 36 | ```scala 37 | libraryDependencies += "com.github.marcospereira" %% "play-hocon-i18n" % "1.0.1" 38 | ``` 39 | 40 | ### Disable built-in I18n Module 41 | 42 | Add the following line to your `conf/application.conf` file: 43 | 44 | ``` 45 | play.modules.disabled += play.api.i18n.I18nModule 46 | ``` 47 | 48 | ### Enable HOCON I18n Module 49 | 50 | Add the following line to your `conf/application.conf` file: 51 | 52 | ``` 53 | play.modules.enabled += com.marcospereira.play.i18n.HoconI18nModule 54 | ``` 55 | 56 | ### Write your message files with HOCON syntax 57 | 58 | As stated before, HOCON syntax and Java Properties are not fully compatible. The good part is that HOCON loader gives clear messages about invalid syntax and you can easily fix the errors. Of course, all HOCON features are enable here. Finally, you have to rename your messages files to have a `.conf` extension, per instance: 59 | 60 | | Before | After | 61 | |:----------------------|:---------------------------| 62 | | `conf/messages` | `conf/messages.conf` | 63 | | `conf/messages.en` | `conf/messages.en.conf` | 64 | | `conf/messages.en-US` | `conf/messages.en-US.conf` | 65 | 66 | ## License 67 | 68 | Copyright 2016 Marcos Pereira 69 | 70 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at 71 | 72 | http://www.apache.org/licenses/LICENSE-2.0 73 | 74 | Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -------------------------------------------------------------------------------- /src/test/scala/com/marcospereira/play/i18n/HoconMessagesApiSpec.scala: -------------------------------------------------------------------------------- 1 | package com.marcospereira.play.i18n 2 | 3 | import org.scalatestplus.play.PlaySpec 4 | import org.scalatestplus.play.guice.GuiceOneAppPerSuite 5 | import play.api.i18n.{ I18nModule, Lang, MessagesApi } 6 | import play.api.inject.guice.GuiceApplicationBuilder 7 | 8 | class HoconMessagesApiSpec extends PlaySpec with GuiceOneAppPerSuite { 9 | 10 | lazy val lang_en = Lang("en") 11 | lazy val lang_pt = Lang("pt") 12 | lazy val lang_pt_br = Lang("pt", "BR") 13 | 14 | implicit override lazy val app = new GuiceApplicationBuilder() 15 | .configure(Map("play.i18n.langs" -> Seq("en", "pt", "pt-BR"))) 16 | .bindings(new HoconI18nModule) 17 | .disable(classOf[I18nModule]) 18 | .build() 19 | 20 | "Hocon Messages API" must { 21 | 22 | "load messages from hocon files" in { 23 | val messagesApi = app.injector.instanceOf[MessagesApi] 24 | 25 | implicit val lang = lang_en 26 | 27 | messagesApi("test.messages.simple") mustBe "Hello" 28 | messagesApi("test.messages.parameters", "Stranger") mustBe "Hello, Stranger" 29 | } 30 | 31 | "get messages using the preferred language" in { 32 | val messagesApi = app.injector.instanceOf[MessagesApi] 33 | val messages = messagesApi.preferred(Seq(Lang("pt"))) 34 | 35 | implicit val lang = lang_pt 36 | 37 | messages("test.messages.simple") mustBe "Ola" 38 | messages("test.messages.parameters", "Stranger") mustBe "Ola, Stranger" 39 | } 40 | 41 | "get default language when preferred language is not recognized" in { 42 | val messagesApi = app.injector.instanceOf[MessagesApi] 43 | val messages = messagesApi.preferred(Seq(Lang("fr"))) 44 | 45 | implicit val lang = Lang("fr") 46 | 47 | messages("test.messages.simple") mustBe "Hello" 48 | messages("test.messages.parameters", "Stranger") mustBe "Hello, Stranger" 49 | } 50 | 51 | "get the key itself when message key does not exists" in { 52 | val messagesApi = app.injector.instanceOf[MessagesApi] 53 | 54 | implicit val lang = lang_en 55 | 56 | messagesApi("test.messages.nonExistent") mustBe "test.messages.nonExistent" 57 | } 58 | 59 | "get None when translating key does not exists" in { 60 | val messagesApi = app.injector.instanceOf[MessagesApi] 61 | messagesApi.preferred(Seq(Lang("en"))) 62 | 63 | implicit val lang = lang_en 64 | 65 | messagesApi.translate("test.messages.nonExistent", Seq.empty).isEmpty mustBe true 66 | } 67 | 68 | "get translation from default messages when key is missing for preferred language" in { 69 | val messagesApi = app.injector.instanceOf[MessagesApi] 70 | 71 | implicit val lang = lang_en 72 | 73 | messagesApi("test.missing") mustBe "Ok at the default file" 74 | } 75 | 76 | "get translation for the preferred region" in { 77 | val messagesApi = app.injector.instanceOf[MessagesApi] 78 | val messages = messagesApi.preferred(Seq(Lang("pt", "BR"))) 79 | 80 | implicit val lang = lang_pt_br 81 | 82 | messages("test.messages.simple") mustBe "Oi" 83 | messages("test.messages.parameters", "Estranho") mustBe "Oi, Estranho" 84 | } 85 | 86 | "get translation for language when region is missing" in { 87 | val messagesApi = app.injector.instanceOf[MessagesApi] 88 | 89 | implicit val lang = Lang("en", "UK") 90 | 91 | messagesApi.preferred(Seq(Lang("en", "UK"))) 92 | messagesApi("test.messages.simple") mustBe "Hello" 93 | } 94 | 95 | "get default play messages" in { 96 | val messagesApi = app.injector.instanceOf[MessagesApi] 97 | 98 | implicit val lang = lang_en 99 | 100 | messagesApi("constraint.required") mustBe "Required" 101 | } 102 | 103 | "get resolved messages" in { 104 | val messagesApi = app.injector.instanceOf[MessagesApi] 105 | messagesApi.preferred(Seq(Lang("en"))) 106 | 107 | implicit val lang = lang_en 108 | 109 | messagesApi("test.resolve") mustBe "Hello" 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/main/scala/com/marcospereira/play/i18n/HoconMessagesApi.scala: -------------------------------------------------------------------------------- 1 | package com.marcospereira.play.i18n 2 | 3 | import java.net.URL 4 | import java.util.Properties 5 | import javax.inject.{ Inject, Singleton } 6 | 7 | import com.typesafe.config.ConfigFactory 8 | import play.api.http.HttpConfiguration 9 | import play.api.i18n._ 10 | import play.api.inject.Module 11 | import play.api.{ Configuration, Environment } 12 | import play.utils.Resources 13 | 14 | import scala.collection.JavaConverters._ 15 | 16 | @Singleton 17 | class HoconMessagesApiProvider @Inject() ( 18 | environment: Environment, 19 | config: Configuration, 20 | langs: Langs, 21 | httpConfiguration: HttpConfiguration 22 | ) 23 | extends DefaultMessagesApiProvider(environment, config, langs, httpConfiguration) { 24 | 25 | override lazy val get: MessagesApi = { 26 | new DefaultMessagesApi( 27 | loadAllMessages, 28 | langs, 29 | langCookieName = langCookieName, 30 | langCookieSecure = langCookieSecure, 31 | langCookieHttpOnly = langCookieHttpOnly, 32 | httpConfiguration = httpConfiguration 33 | ) 34 | } 35 | 36 | override protected def loadMessages(file: String): Map[String, String] = { 37 | getResources(file) 38 | .filterNot(url => Resources.isDirectory(environment.classLoader, url)).reverse 39 | .map(getMessages) 40 | .foldLeft(Map.empty[String, String]) { _ ++ _ } 41 | } 42 | 43 | override protected def loadAllMessages: Map[String, Map[String, String]] = { 44 | langs.availables.map(_.code).map { lang => 45 | (lang, loadMessages(s"messages.$lang.conf")) 46 | }.toMap ++ Map( 47 | "default" -> loadMessages("messages.conf"), 48 | "default.play" -> loadMessages("messages.default") 49 | ) 50 | } 51 | 52 | override protected def joinPaths(first: Option[String], second: String) = first match { 53 | case Some(parent) => new java.io.File(parent, second).getPath 54 | case None => second 55 | } 56 | 57 | private def getResources(file: String): List[URL] = { 58 | environment.classLoader.getResources(joinPaths(messagesPrefix, file)).asScala.toList 59 | } 60 | 61 | private def getMessages(url: URL): Map[String, String] = { 62 | // messages.default is bundled with play and it is a properties file 63 | val config = if (url.toString.endsWith("messages.default")) { 64 | ConfigFactory.parseProperties(getProperties(url)) 65 | } else { 66 | ConfigFactory.parseURL(url) 67 | } 68 | 69 | config.resolve().entrySet().asScala 70 | .map(e => e.getKey -> String.valueOf(e.getValue.unwrapped())) 71 | .toMap 72 | } 73 | 74 | private def getProperties(url: URL): Properties = { 75 | val properties = new Properties() 76 | val input = url.openStream() 77 | try { 78 | properties.load(input) 79 | } finally { 80 | input.close() 81 | } 82 | properties 83 | } 84 | } 85 | 86 | /** 87 | * Module that replaces built-in MessagesApi implementation with a HOCON format based implementation. To enable this 88 | * module, you have to edit your `application.conf` and add these two lines: 89 | * 90 | * {{{ 91 | * play.modules.disabled += play.api.i18n.I18nModule 92 | * play.modules.enabled += com.marcospereira.play.i18n.HoconI18nModule 93 | * }}} 94 | */ 95 | class HoconI18nModule extends Module { 96 | def bindings(environment: Environment, configuration: Configuration) = { 97 | Seq( 98 | bind[Langs].toProvider[DefaultLangsProvider], 99 | bind[MessagesApi].toProvider[HoconMessagesApiProvider], 100 | bind[play.i18n.MessagesApi].toSelf, 101 | bind[play.i18n.Langs].toSelf 102 | ) 103 | } 104 | } 105 | 106 | /** 107 | * Components for Compile Time Dependency Injection. 108 | */ 109 | trait HoconI18nComponents extends I18nComponents { 110 | 111 | def environment: Environment 112 | def configuration: Configuration 113 | def httpConfiguration: HttpConfiguration 114 | def langs: Langs 115 | 116 | override lazy val messagesApi: MessagesApi = new HoconMessagesApiProvider(environment, configuration, langs, httpConfiguration).get 117 | } 118 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------