0

Can someone please explain what is the relation between Locale and ResourceBundle classes provided by the JDK ? Is it the same as the difference between i18n vs l10n as given in this answer ?

Community
  • 1
  • 1
Geek
  • 26,489
  • 43
  • 149
  • 227

1 Answers1

1

The relation between Locale and ResourceBundle is, Locale is used to read the right properties file. Basically, the ResourceBundle.Control class takes a Locale object, and given the base name of the properties file, it tries to find the best matching one. For example:

ResourceBundle rb = ResourceBundle.getbundle("messages", Locale.forLanguageTag("zh-TW");

If you do that, the ResourceBundle.Control will look for messsages_zh_TW.properties, messages_zh-Hant-TW.properties, messages_zh-Hant.properties, messages_zh.properties and finally fall-back to messages.properties if it cannot find any of the specific resource file.

EDIT

Just to add one final thing. If you omit the Locale parameter, the ResourceBundle.getBundle(String) will return the properties file based on your default Locale - the one that you can obtain like this:

Locale default = Locale.getDefault(Locale.Category.DISPLAY);

Although it might seem unnecessary of excessive typing (in the context of desktop applications), it is a good practice to always pass the Locale, as it makes code easier to understand.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79