Locales and Resource Bundles in Java

Master locales, Locale class, locale creation, default locale, resource bundles, PropertyResourceBundle, and ListResourceBundle for the OCP 21 exam.

Table of Contents

1. Locales Overview

A locale represents a specific geographical, political, or cultural region. It determines language, date/time formats, number formats, and currency formats.

1.1 Locale Components

  • Language: Two-letter ISO 639 code (e.g., "en", "fr")
  • Country: Two-letter ISO 3166 code (e.g., "US", "GB")
  • Variant: Additional information (e.g., "POSIX", "WIN")

2. Locale Class

The Locale class represents a locale and provides methods for locale operations.

2.1 Common Locale Constants

Example:
import java.util.Locale;

// Predefined locales
Locale us = Locale.US;           // en_US
Locale uk = Locale.UK;           // en_GB
Locale france = Locale.FRANCE;   // fr_FR
Locale german = Locale.GERMAN;   // de
Locale defaultLocale = Locale.getDefault();

// Getting locale components
String language = us.getLanguage();  // "en"
String country = us.getCountry();    // "US"
String displayName = us.getDisplayName();  // "English (United States)"

3. Creating Locales

3.1 Locale Constructors

Example:
import java.util.Locale;

// Constructor with language
Locale locale1 = new Locale("en");

// Constructor with language and country
Locale locale2 = new Locale("en", "US");

// Constructor with language, country, and variant
Locale locale3 = new Locale("en", "US", "POSIX");

// Using Locale.Builder (recommended)
Locale locale4 = new Locale.Builder()
    .setLanguage("fr")
    .setRegion("CA")
    .build();

// Using forLanguageTag() (BCP 47 format)
Locale locale5 = Locale.forLanguageTag("en-US");

4. Resource Bundles

Resource bundles contain locale-specific data, typically used for internationalization (i18n).

4.1 Loading Resource Bundles

Example:
import java.util.ResourceBundle;
import java.util.Locale;

// Load default bundle
ResourceBundle bundle = ResourceBundle.getBundle("Messages");

// Load bundle for specific locale
ResourceBundle bundleFR = ResourceBundle.getBundle("Messages", Locale.FRANCE);

// Get values
String greeting = bundle.getString("greeting");
String welcome = bundle.getString("welcome");

// Check if key exists
if (bundle.containsKey("key")) {
    String value = bundle.getString("key");
}

5. PropertyResourceBundle

PropertyResourceBundle loads resources from properties files.

5.1 Properties Files

Example:
// Messages.properties (default)
greeting=Hello
welcome=Welcome

// Messages_fr.properties (French)
greeting=Bonjour
welcome=Bienvenue

// Messages_fr_CA.properties (French Canada)
greeting=Bonjour
welcome=Bienvenue au Canada

// Loading
ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.FRANCE);
String greeting = bundle.getString("greeting");  // "Bonjour"

6. ListResourceBundle

ListResourceBundle is an abstract class for creating resource bundles programmatically.

6.1 Implementing ListResourceBundle

Example:
import java.util.ListResourceBundle;

// Default bundle
class Messages extends ListResourceBundle {
    protected Object[][] getContents() {
        return new Object[][] {
            {"greeting", "Hello"},
            {"welcome", "Welcome"}
        };
    }
}

// French bundle
class Messages_fr extends ListResourceBundle {
    protected Object[][] getContents() {
        return new Object[][] {
            {"greeting", "Bonjour"},
            {"welcome", "Bienvenue"}
        };
    }
}

// Usage
ResourceBundle bundle = ResourceBundle.getBundle("Messages", Locale.FRANCE);
String greeting = bundle.getString("greeting");

7. Exam Key Points

Critical Concepts for OCP 21 Exam:

  • Locale: Represents geographical/cultural region
  • Language Code: Two-letter ISO 639 code
  • Country Code: Two-letter ISO 3166 code
  • Locale.getDefault(): Get system default locale
  • ResourceBundle: Contains locale-specific data
  • getBundle(): Load resource bundle for locale
  • getString(): Get value from resource bundle
  • PropertyResourceBundle: Loads from properties files
  • ListResourceBundle: Programmatic resource bundle
  • Bundle Naming: BaseName_locale.properties
  • Fallback: Falls back to default if locale-specific not found

Post a Comment

0 Comments