Last active
October 5, 2016 09:29
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GroovyTextProvider { | |
private final ResourceBundle bundle | |
GroovyTextProvider(String name) { | |
bundle = ResourceBundle.getBundle("text_${name}", Locale.GERMAN) | |
} | |
String get(String key) { | |
bundle.getString(key) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello = Hallo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment