Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore the system default Locale to retrieve resourceBundle

I am localizing a web application using a java.util.ResourceBundle class and property files.

I have two locales, fr_FR and en_US, and I want to use en_US as the default, so I wrote the following files :

  • messages_fr_FR.properties with fr_FR messages
  • messages.properties with en_US messages

My problem is that the ResourceBundle.getBundle(BUNDLE_NAME, locale) method fall back Locale.getDefault() before using the default property file, which means if the JVM Locale is set to fr_FR, ResourceBundle.getBundle("name", new Locale("en", "US")) returns the fr_FR bundle.

I could rename the messages.properties file, but this could returns a MissingResourceException with other desired and default locales.

How can I ignore the System Locale, without duplicating the property file or calling Locale.setDefault ?

like image 951
corentin Avatar asked Oct 17 '14 10:10

corentin


1 Answers

You can do that by using a custom ResourceBundle.Control, either by overloading the getFallbackLocaleexplicitly, or by using :

ResourceBundle.getBundle("name", new Locale("en", "US"), ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES))
like image 102
corentin Avatar answered Nov 04 '22 12:11

corentin