Griffon: i18n FXML content on the fly - No Fluff Just Stuff

Griffon: i18n FXML content on the fly

Posted by: Andres Almiray on November 6, 2012

Saw earlier on twitter a post by Thomas Bolz (@TeaBeeOh) pertaining a particular issue he's currently facing: updating i18n content defined on an FXML file without restarting. Sadly for him the short answer is that it can't be done with what FXML and the JavaFX 2.0 APIs provide out of the box, that is, setting a ResourceBundle and changing the application's Locale as a minimum.

This task ought to be simple with Griffon, and as it turns out it is :-D. The first step is to create a sample application using the JavaFX archetype

Next fire up SceneBuilder and create a simple layout with one label and 2 buttons. Each component should have their id property set. Here's the FXML file I came up with

Name it simple.fxml and place it under griffon-app/resources. Next open up griffon-app/views/simple/SimpleView.groovy and tweak the code so that it looks like the following snippet

The fxml node has been configured to read the FXML file. Make a special note of the children nodes used within fxml, these are the controls created in SceneBuilder! Here we are setting up a property binding to the Model and adding a pair of EventHandlers, one to each button. What's going to happen next is setting up the Model (griffon-app/models/simple/SimpleModel.groovy)

And the Controller (griffon-app/controllers/simple/SimpleController.groovy)

Great. All that's left to do is write the i18n resource files, one for English and another for Spanish. We'll need a file named messages.properties and another named messages_es.properties, both located inside griffon-app/i18n.

Launching the application results in the following screen



Clicking on the 'Spanish' button results in



Cool! The trick is then to use property bindings where required and react to the application's locale change, a trivial matter given that all Griffon applications trigger an event whenever their Locale changes value; compound that with the excellent I18N support found in Griffon 1.1.0 and you got a recipe for success and fun!

Keep on Groovying!

Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »