Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java ListResourceBundle example

farenda 2016-05-02 0

Java ListResourceBundle tutorial

In this tutorial we’re going to show how to use Java ListResourceBundle to make you program speak in different languages.

ListResourceBundle subclass

In the following code we’re going to provide translations for Esperanto language.

Notice that unlike full implementation of ResourceBundle subclass we have to implement only one method:

  • Object[][] getContents()

Not three (see Extending ResourceBundle):

  • handleGetObject(String key),
  • handleKeySet(),
  • getKeys().
package com.farenda.java.util;

import java.util.ListResourceBundle;

public class Numbers_eo extends ListResourceBundle {

    private static final Object[][] TRANSLATIONS = {
        {"sum-of-numbers", "%s plus %s estas %s"},
        {"one", "unu"},
        {"two", "du"},
        {"three", "tri"}
    };

    @Override
    protected Object[][] getContents() {
        return TRANSLATIONS;
    }
}

Important: make the class thread-safe if plan to use it concurrently!

Use of ListResourceBundle

Here we’ll load the resource bundle using static ResourceBundle.getBundle(String, Locale) method, take localized messages from it and display them:

private static final String BUNDLE_NAME
        = "com.farenda.java.util.Numbers";

public static void main(String[] args) {
    ResourceBundle bundle
        = getBundle(BUNDLE_NAME, new Locale("eo"));

    translate(bundle, "one");
    translate(bundle, "two");
    translate(bundle, "three");

    System.out.printf(
            bundle.getString("sum-of-numbers"),
            bundle.getString("one"),
            bundle.getString("two"),
            bundle.getString("three"));
}

static void translate(ResourceBundle bundle, String key) {
    System.out.printf("'%s' in Esperanto: %s%n",
            key, bundle.getString(key));
}

And here’s the result of running the example:

'one' in Esperanto: unu
'two' in Esperanto: du
'three' in Esperanto: tri
unu plus du estas tri

Remember that bundle name have to be full path to resource bundle!

References:

  • Intro to ResourceBundles in Java
  • ResourceBundle direct subclass
    Checkout for more examples and how to mix class-based ResourceBundles with property-based ones.
Share with the World!
Categories Java Tags java, java-util
Previous: Java Initialization order
Next: Java PriorityQueue by example

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored