Skip to main content Skip to docs navigation
TechSpiderTutorials

java.util.ResourceBundle class in Java

public abstract class ResourceBundle extends Object
On this page

java.util.ResourceBundle Class

Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

This allows you to write programs that can:

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

Creating Objects

 // Load default ResourceBundle for the default locale (English)
    ResourceBundle messages = ResourceBundle.getBundle("resources.messages");
					

ResourceBundle Class Methods

public String getBaseBundleName()

Returns the base name of this bundle, if known, or null if unknown.


public static ResourceBundle getBundle(String baseName)

Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.


public static ResourceBundle getBundle(String baseName, Locale locale)

Gets a resource bundle using the specified base name and locale, and the caller's class loader.


public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)

Gets a resource bundle using the specified base name, locale, and class loader.


public static ResourceBundle getBundle(String baseName, Locale targetLocale, ClassLoader loader, ResourceBundle.Control control)

Returns a resource bundle using the specified base name, target locale, class loader and control.


public static ResourceBundle getBundle(String baseName, Locale targetLocale, ResourceBundle.Control control)

Returns a resource bundle using the specified base name, target locale and control, and the caller's class loader.


public static ResourceBundle getBundle(String baseName, ResourceBundle.Control control)

Returns a resource bundle using the specified base name, the default locale and the specified control.


public abstract Enumeration getKeys()

Returns an enumeration of the keys.


public Locale getLocale()

Returns the locale of this resource bundle.


public Object getObject(String key)

Gets an object for the given key from this resource bundle or one of its parents.


public String getString(String key)

Gets a string for the given key from this resource bundle or one of its parents.


public String[] getStringArray(String key)

Gets a string array for the given key from this resource bundle or one of its parents.


public protected abstract Object handleGetObject(String key)

Gets an object for the given key from this resource bundle.


public protected Set<String> handleKeySet()

Returns a Set of the keys contained only in this ResourceBundle.


public Set<String> keySet()

Returns a Set of all keys contained in this ResourceBundle and its parent bundles.


protected void setParent(ResourceBundle parent)

Sets the parent bundle of this bundle.


ResourceBundle Class Example

Example Overview:

In this example, we will create a simple Java application that uses ‘ResourceBundle‘ to manage localized messages in English and French. We'll create two properties files: ‘messages_en.properties‘ for English and ‘messges_fr.properties‘ for French. Each file will contain key-value pairs where keys represent message identifiers and values represent localized strings.

1. Create Properties Files:
Create two properties files under a directory named ‘resources› messages_en.properties
greeting=Hello, welcome to our application!
farewell=Goodbye, have a nice day!	
messages_fr.properties:
greeting=Bonjour, bienvenue dans notre application !
farewell=Au revoir, passez une bonne journée !	
2. Java Code:

Write Java code to load and use these resource bundles based on the user's locale.

import java.util.Locale;
   import java.util.ResourceBundle;

   public class ResourceBundleExample {
       public static void main(String[] args) {
           // Load default ResourceBundle for the default locale (English)
           ResourceBundle messages = ResourceBundle.getBundle("resources.messages");

           // Display messages in default locale (English)
           String greeting = messages.getString("greeting");
           String farewell = messages.getString("farewell");

           System.out.println("Default Locale (English):");
           System.out.println("Greeting: " + greeting);
           System.out.println("Farewell: " + farewell);
           System.out.println();

           // Change locale to French
           Locale.setDefault(new Locale("fr", "FR"));
           ResourceBundle messagesFr = ResourceBundle.getBundle("resources.messages");

           // Display messages in French locale
           greeting = messagesFr.getString("greeting");
           farewell = messagesFr.getString("farewell");

           System.out.println("Locale (French):");
           System.out.println("Greeting: " + greeting);
           System.out.println("Farewell: " + farewell);
       }
   }
ResourceBundle Loading: ResourceBundle.getBundle("resources.messages") loads the appropriate resource bundle based on the default locale (‘en_US’ in this case).
Locale Setting: ‘Locale.setDefault(new Locale("fr", "FR"))‘ changes the default locale to French (‘fr_FR‘).
Retrieving Messages: ‘messages.getString("greeting")‘ retrieves the localized string for the key ‘"greeting"‘ from the appropriate bundle based on the current locale.

Output:

The output will show the localized greetings and farewells for both English and French locales.


   Default Locale (English):
   Greeting: Hello, welcome to our application!
   Farewell: Goodbye, have a nice day!
   
   Locale (French):
   Greeting: Bonjour, bienvenue dans notre application !
   Farewell: Au revoir, passez une bonne journée !
   
TechSpider Tutorials  TechSpider Tutorials
  • Welcome to our tutorial website, your comprehensive resource for learning C, C++, Java SE, Java EE, and other cutting-edge technologies. Whether you're a beginner or an experienced programmer, dive into our tutorials to master programming fundamentals, explore advanced concepts, and stay updated with the latest trends in software development. Start your learning journey today and empower yourself with the skills needed to build robust applications and advance your career in the tech industry.