Overview

i18n is useful library for internationalization. This product have following features.

  • multiple languages can be available at same time,
  • replace ``foo${bar.key}'' to ``foobar'' when ``bar.key=bar'' is defined, and
  • If ``bar.key'' is not defined, then ``foo${bar.key}'' will be ``foo${bar.key}''

Usage

Default locale

Messages messages = new Messages("resource.bundle.path");

String   value    = messages.get("string.resource.key");
boolean  hasValue = messages.hasValue("string.resource.key");
String   string   = messages.format("template.resource.key", "value1", "value2", ...);
Icon     icon     = messages.getIcon("icon.resource.key");
String[] array    = messages.getArray("array.resource.key");
// getArray is same as following routine.
String[] array2   = messages.get("array.resource.key").split(", *");

Locale specified

Locale locale = ... // obtains target locale.
Messages messages = new Messages("resource.bundle.path");

String   value    = messages.get(locale, "string.resource.key");
boolean  hasValue = messages.hasValue(locale, "string.resource.key");
String   string   = messages.format(locale, "template.resource.key", "value1", "value2", ...);
Icon     icon     = messages.getIcon(locale, "icon.resource.key");
String[] array    = messages.getArray(locale, "array.resource.key");
// getArray is same as following routine.
String[] array2   = messages.get(locale, "array.resource.key").split(", *");

Change default locale

Locale locale = .... // obtains target locale.
Messages messages = new Messages(locale, "resource.bundle.path");

String   value    = messages.get("string.resource.key");
boolean  hasValue = messages.hasValue("string.resource.key");
String   string   = messages.format("template.resource.key", "value1", "value2");
Icon     icon     = messages.getIcon("icon.resource.key");
String[] array    = messages.getArray("array.resource.key");

// getIcon method is same as following statement.
Icon     icon2    = new ImageIcon(getClass().getResource(messages.get("icon.resource.key")));
// getArray is same as following statement.
String[] array2   = messages.get("array.resource.key").split(", *");

Resource sample (property file)

``icon.path'' can be referenced by $icon.path such as value of ``icon.resource.key'' key.

icon.path=/resources/images
string.resource.key=String resource value
template.resource.key=Template {1} {2}
icon.resource.key=${icon.path}/image.png
array.resource.key=value1,value2,value3

Locales

constructor locale available arguments locale available obtained resource
case 1 ja_JP Yes N/A --- ja_JP
case 2 ja_JP Yes en_US Yes en_US
case 3 N/A Yes N/A --- default locale
case 4 ja_JP Yes en_US No default locale
case 5 ja_JP No --- --- Exception

Note that in case 4, en_US resource file is exists. If the resource is not exists, the obtained resource will be constructor locale.