Localization in Java by ResourceBundle Library

December 28, 2022

Localization in Java by ResourceBundle Library

ResourceBundle library is used in Java in order to handle localization by managing different locales/languages. ResourceBundle class is used to return messages in the provided locale or default locale, so you do not need to think how to handle & display messages in different languages.

Localization in Java By ResourceBundle Library

The most important three pros of ResourceBundle according to Oracle spec doc are;

  • Your application can be easily localized, or translated, into different languages
  • Your application can handle multiple locales at once
  • Your application can be easily modified later to support even more locales

ResourceBundle works with a base name which is the name of the resource and the locale as the suffix of the base name. You can find the related message with a key assigned to it. Each message is stored as key-value pair in the related resource file. For instance; in my example I will use messages to be localized, so I give “messages” as the base name and the locale will be the suffix of this base name. Then, we will reach the related message by using the key assigned to that value.

I will give a quick example how to use ResourceBundle library within my example SpringBoot project repo within my GitHub account. You can specifically check the related pull request in order to implement ResourceBundle feature.

More details about ResourceBundle can be reached from the official Java Spec doc.

Now, let’s move the implementation of ResourceBundle.


First of all, let’s create four messages.properties. I am using IntelliJ IDEA as my editor. So, you only right-click on the resources section in the project structure, then click “New” and “Resource Bundle” as seen in the below screenshot.

Adding ResourceBundle in Intellij - 1

Then, fill the related info the pop-up window. In my example, I am creating a resource bundle with “messages” name. So, you can add as many locale as you require. IntelliJ IDEA groups the same resource bundles under the same structure, so you can easily track your changes.

Adding ResourceBundle in Intellij - 2

I have created four “messages” resource, default one, Turkish, English & French. Below is the code snippet for English resource under “messages” resource bundle.

ResourceBundle Message

Now, let’s create our service implementation class in order to read from related resource bundle.

Please check [the Gist](https://gist.github.com/evrentan/398ab2ecdde5608bbb4d87c93c202809#file-messageutilityserviceimpl-java "MessageUtilityServiceImpl Class for Reading "messages" Resource Bundle") to see how I declare a message utility class to read from related resource bundle.

We have five main static functions in this class.

  • getBundle(Locale locale) → To find the related bundle in the resource bundle. If the Locale is null, then it returns the default bundle in the system or the one that is defined in the same class as a static variable, defaultLocale.
  • getMessage(String messageKey) → To find the related message in the resource bundle in the default locale.
  • getMessage(String messageKey, Locale locale) → To find the related message in the resource bundle in the requested Locale.
  • getMessage(String messageKey, Object …messageArguments) → To find the related message in the resource bundle in the default locale with the requested message arguments.
  • getMessage(String messageKey, Locale locale, Object… messageArguments) → To find the related message in the resource bundle in the requested locale with the requested message arguments.

Finally, let’s use these within our code. I have added resource bundle usage in the custom REST exception handler class. You can find the how to use them in the Gist.

You can find the result in the below screenshot. We are sending the “locale” parameter as “tr” in the header and we are getting the corresponding message from “messages_tr.properties” file.

ResourceBundle Usage

In a Nutshell

ResourceBundle library can be used in Java in order to localization.

You can find the related code snippets from my GitHub account under public shared SpringBoot project repo as always.

Your comments & questions are always welcomed.

Share: