Skip to content

Instantly share code, notes, and snippets.

@JonasHavers
Last active October 5, 2016 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonasHavers/45713ba48f85a4e6852d0565e111bca3 to your computer and use it in GitHub Desktop.
Save JonasHavers/45713ba48f85a4e6852d0565e111bca3 to your computer and use it in GitHub Desktop.
ResourceBundle.getBundle(String, Locale) fails inside Groovy constructor (MissingResourceException) (see https://issues.apache.org/jira/browse/GROOVY-7959)
$ java -version && groovy -v
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
Groovy Version: 2.4.7 JVM: 1.8.0_102 Vendor: Oracle Corporation OS: Mac OS X
$ javac JavaTextProvider.java
$ groovy main.groovy
--- ResourceBundle initialized in Groovy Script
Hallo
--- ResourceBundle initialized in Java Constructor
Hallo
--- ResourceBundle initialized in Groovy Constructor
Caught: java.util.MissingResourceException: Can't find bundle for base name text_test, locale de
java.util.MissingResourceException: Can't find bundle for base name text_test, locale de
at GroovyTextProvider.<init>(GroovyTextProvider.groovy:5)
at main.run(main.groovy:10)
class GroovyTextProvider {
private final ResourceBundle bundle
GroovyTextProvider(String name) {
bundle = ResourceBundle.getBundle("text_${name}", Locale.GERMAN)
}
String get(String key) {
bundle.getString(key)
}
}
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class JavaTextProvider {
private final ResourceBundle bundle;
public JavaTextProvider(String name) {
bundle = ResourceBundle.getBundle("text_" + name, Locale.GERMAN);
}
public String get(String key) {
return bundle.getString(key);
}
}
println '--- ResourceBundle initialized in Groovy Script'
String name = "test"
ResourceBundle bundle = ResourceBundle.getBundle("text_${name}", Locale.GERMAN)
println bundle.getString('hello')
println '--- ResourceBundle initialized in Java Constructor'
println new JavaTextProvider('test').get('hello')
println '--- ResourceBundle initialized in Groovy Constructor'
println new GroovyTextProvider('test').get('hello')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment