How can I get localized property values in a gradle build file?

(Barry Becker) #1

I want to generate some localized html files as part of my build and deployment. As a simple test, I tried this task

task i18n(description:“i18n test”) << {

def locales = [Locale.ENGLISH, Locale.JAPANESE, Locale.GERMAN]

def keys = [ “s1”, “s2”, “s3” ]

[locales, keys].combinations().each{ locale, key ->

def labels = ResourceBundle.getBundle(‘html’, locale)

println “Locale = ${locale.toString()}, key = $key, value = ${labels.getString(key)}”

} }

But it says Can’t find bundle for base name html, locale en_US. I put the resource bundles in the same directory as the build.gradle file (,, etc). However, it seems that gradle does not try to load the resource bundles from that directory, but rather tries to get them from the gradle classLoader. I’m not sure where that might be, but it is probably outside of source control. Is there a convenient way to get localized messages from property files from within the gradle build script?

Accessing message bundles in buildsrc
(Luke Daley) #2

The simplest way would be to properties files in ‘buildSrc/src/main/resources’. This will put these files on the classpath for the build execution.

See for more details.

(Barry Becker) #3

Thank you! That worked. It took me a little while to realize that the resources had to go in the buildSrc/main directory and not the src/main directory (which I call “source” in my multi-project). I would prefer if I could have the resources be in the buildSrc directory of the subproject in which they are used instead of the top level, but it does not look like there is a way to do that.