36

I have a Groovy Grails application and I want to access programmatically to a property defined in messages.properties.

As a test, I've tried the following statement:

println "capacity.created: ${messages.properties['capacity.created']}"

But it doesn't work (throws an exception).

Any help is welcomed.

Luis

Tung
  • 1,579
  • 4
  • 15
  • 32
Luixv
  • 8,590
  • 21
  • 84
  • 121

3 Answers3

70

For reading property files in Groovy you can use the utility class ConfigSlurper and access the contained properties using GPath expressions. However, you have to be aware that ConfigSlurper doesn't support standard Java property files. Normally the ConfigSlurper will be used to read .groovy files that may be similar to a property file, but adhere to standard groovy notation, thus Strings are inside quotes and comments start with // or are inside a /* */ block. So, to read a Java properties file you need to create a java.util.Properties object and use that to create a ConfigSlurper:

def props = new Properties()
new File("message.properties").withInputStream { 
  stream -> props.load(stream) 
}
// accessing the property from Properties object using Groovy's map notation
println "capacity.created=" + props["capacity.created"]

def config = new ConfigSlurper().parse(props)
// accessing the property from ConfigSlurper object using GPath expression
println "capacity.created=" + config.capacity.created

If you only use the property file from within Groovy code you should use the Groovy notation variant directly.

def config = new ConfigSlurper().parse(new File("message.groovy").toURL())

This also gives you some nice advantages over standard property files, e.g. instead of

capacity.created="x"
capacity.modified="y"

you can write

capacity {
  created="x"
  modified="y"
}
Christoph Metzendorf
  • 7,968
  • 2
  • 31
  • 28
  • Thanks for your answer. What you did is valid for any property file but message is an i18n file. I d'ont think that this is going to work if the locale of the client is set to other language. I mean, you are not reading message_de, message_es, message_fr, etc. Besides that all the messages are already read by the system. I wouldn't read them again if I can avoid that. – Luixv Jan 14 '10 at 14:28
  • 4
    Just a notice for loading from classpath use `properties.load(getClass().getClassLoader().getResourceAsStream("message.properties"))` – banterCZ Jan 31 '14 at 07:38
  • In newer java Versions ```toURL()``` is deprecated, simply do the following: ```def config = new ConfigSlurper().parse(new File("message.groovy").toURI().toURL())``` – VolkerK Feb 28 '17 at 08:13
9

I found a way to access to message properties directly wothout re-reading all the messages properties files (message_de.properties, message_fr.properties, etc.) It is is very easy.

message(code:"capacity.created")

and it works!

Luis

Luixv
  • 8,590
  • 21
  • 84
  • 121
2

It's not a best practice to read message.properties for i18n. You can use:

message(code:"capacity.created")

in controllers as @Luixv suggested or

messageSource.getMessage("capacity.created",
                        [].toArray(), "Capacity Created.", null)

in any other spring/grails bean after injecting the bean messageSource.

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90