Java Utility Library

Java ResourceBundle - getLocale() Method



The java.util.ResourceBundle.getLocale() method returns the locale of this resource bundle. This method can be used after a call to getBundle() to determine whether the resource bundle returned really corresponds to the requested locale or is a fallback.

Syntax

public Locale getLocale()

Parameters

No parameter is required.

Return Value

Returns the locale of this resource bundle.

Exception

NA.

Example:

Lets assume that we have resource file called Greetings_en_US.properties in the CLASSPATH with the following content. This file contains local message for US country.

greet = Hello World!

In the below Java program, the java.util.ResourceBundle.getLocale() method is used to get the locale of the above mentioned resource bundle Greetings_en_US.properties.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a resource bundle in specified locale
    ResourceBundle bundle = ResourceBundle.getBundle("Greetings", Locale.US);

    //printing the text assigned to "greet" 
    //key in the bundle
    System.out.println(bundle.getString("greet")); 

    //printing the locale of the bundle
    System.out.println(bundle.getLocale());     
  }
}

The output of the above code will be:

Hello World!
en_US

❮ Java.util - ResourceBundle