Logo
Unit 15 – Internationalization and Localization in Java

Internationalization and Localization in Java

Duration: 7 minutes

Hello, Java developers!

In this session, we’ll explore Internationalization (i18n) and Localization (l10n) in Java. These are key concepts in developing global applications that can be easily adapted to various languages and regions without engineering changes.

Understanding i18n and l10n

Internationalization (i18n): Preparing your application to support multiple languages and regions, typically by externalizing texts and other locale-sensitive data.Localization (l10n): The process of adapting an internationalized application for a specific language and region by adding locale-specific components and translating text.Resource Bundles and Locale

Java uses ResourceBundle class to store locale-specific resources. A resource bundle contains locale-specific objects like text translations. The Locale class represents a specific geographical, political, or cultural region.

Example: Localizing a Java Application

Suppose you want to localize a greeting in your application.

Create property files for each locale:

MessagesBundle_en_US.properties (for U.S. English)makefileCopy codegreeting = HelloMessagesBundle_fr_FR.properties (for French)makefileCopy codegreeting = BonjourJava code to use these resources:

java
Copy code
import java.util.Locale;
import java.util.ResourceBundle;
public class InternationalizationExample {
public static void main(String[] args) {
// Setting the Locale to US
Locale locale = new Locale("en", "US");
ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", locale);
// Retrieve the appropriate greeting
System.out.println(messages.getString("greeting")); // Outputs "Hello"
}
}

To change the language, just change the Locale:

java
Copy code
Locale locale = new Locale("fr", "FR");

Exercise: Localize a Java Application

Your exercise is to:

Create a small Java application that displays a few messages (like greetings, dates, currency).Use ResourceBundle to externalize the messages.Create property files for at least two different locales (e.g., English and Spanish).Change the Locale in your application to see the messages change accordingly.Conclusion

Congratulations! You’ve taken an important step in understanding how to build applications that cater to a global audience. Internationalization and localization are crucial for applications that aim to reach a wide and diverse user base.

Experiment with different locales and resource bundles to get a feel for how you can adapt your applications to different languages and regions.

Happy coding, and enjoy creating applications that speak globally!

You've completed this module!

Continue learning via other JDoodle's professional java Tutorials!

View Modules

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!