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 ResourceBundle – internationalized program

farenda 2016-04-28 0

Java ResourceBundle internationalize program

In this tutorial we’re going to implement a simple dictionary to show how to use Resourcebundle to internationalize program in an easy way.

I18n dictionary using ResourceBundle

package com.farenda.java.util;

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

import static java.util.ResourceBundle.getBundle;

public class ResourceBundleExample {

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

    public static void main(String[] args) {
        Locale.setDefault(Locale.ENGLISH);

        displayWord("book", Locale.getDefault());
        displayWord("book", Locale.JAPAN);
        displayWord("book", new Locale("pl", "PL"));
        displayWord("book", new Locale("eo"));
    }

    private static void displayWord(String word, Locale locale) {
        ResourceBundle words = getBundle(BUNDLE_NAME, locale);
        System.out.printf("'%s' in locale '%s': %s%n",
                word, locale, words.getString(word));
    }
}

When run, the above program will print the following:

'book' in locale 'en': book
'book' in locale 'ja_JP': book
'book' in locale 'pl_PL': książka
'book' in locale 'eo': libro

We don’t have ResourceBundle for the Japanese locale, so it falls back to the default, which is English here. Also note that we create locale pl_PL, but the matched file with translations is Words_pl.properties – this was the closest match.

Properties files with translations

We need to create a set of properties files in the path used in BUNDLE_NAME. Here it is com.farenda.java.util package and the files should start with Words base name. After the base name you can add language prefix and then, optionally, country code. Full name can consists of:

baseName + "_" + language + "_" + script + "_" + country + "_" + variant
or
baseName + "_" + language + "_" + country + "_" + variant

Words.properties for default English locale:

book=book

Words_pl_PL.properties for Polish locale (note that we need to escape diacritics):

book=ksi\u0105\u017cka

And Words_eo.properties for Esperanto language:

book=libro

References:

  • ResourceBundle API
Share with the World!
Categories Java Tags java, java-util
Previous: Java find relative path
Next: Java programmatic ResourceBundle – dynamic translations

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
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok