I have three resource files in class path:
labels.properties:
language = Default
labels_en.properties:
language = English
labels_fr.properties:
language = French
Is there a way to get a ResourceBundle
object that always loads labels.properties
NO MATTER what my default Locale
is?
ResourceBundle.getBundle("labels")
returns the one corresponding to the current default locale (as expected).
The only way I can find is to set the default locale to a non-existing locale, but this may break other modules.
Thank you!
Locale.setDefault( Locale.ENGLISH);
Assert.assertEquals( "English", ResourceBundle.getBundle( "labels").getString( "language"));
Locale.setDefault( Locale.FRENCH);
Assert.assertEquals( "French", ResourceBundle.getBundle( "labels").getString( "language"));
Assert.assertEquals( "French", ResourceBundle.getBundle( "labels", new Locale( "do-not-exist")).getString( "language"));
Locale.setDefault( new Locale( "do-not-exist"));
Assert.assertEquals( "Default", ResourceBundle.getBundle( "labels").getString( "language"));
You can pass in a ResourceBundle.Control which, regardless of requested Locale, always searches only the root ResourceBundle:
ResourceBundle rootOnly = ResourceBundle.getBundle("labels",
new ResourceBundle.Control() {
@Override
public List<Locale> getCandidateLocales(String name,
Locale locale) {
return Collections.singletonList(Locale.ROOT);
}
});
另一种方法是设置应用程序的默认语言环境,例如
Locale.setDefault(Locale.US);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.