Member-only story

Localization in Java by ResourceBundle Library

Evren Tan
3 min readMar 6, 2022

--

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
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 Resource Bundle in IntelliJ IDEA

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.

--

--

Written by Evren Tan

Software Crafter | VP of DevOps at Pointr | Community Lead of Türkiye Java Community | Java & DevOps Enthusiast | Sailor | Oliver Farmer

No responses yet

Write a response