org.xnap.commons.i18n

Class I18nFactory

public class I18nFactory extends Object

Factory class that creates and caches I18n instances.

Given a Class object the factory looks up the resource bundle responsible for handling message translations. The bundle is returned with an I18n object wrapped around it, which provides the translation methods. The lookup is described at getI18n.

Use the factory for creating I18n objects to make sure no extraneous objects are created.

Since: 0.9

Author: Felix Berger Tammo van Lessen Steffen Pingel

Field Summary
static intDEFAULT
Use the default configuration.
static StringDEFAULT_BASE_NAME
Default name for Message bundles, is "i18n.Messages".
static intFALLBACK
Fall back to a default resource bundle that returns the passed text if no resource bundle can be located.
static intNO_CACHE
Do not cache I18n instance.
static StringPROPS_FILENAME
Filename of the properties file that contains the i18n properties, is "i18n.properties".
static intREAD_PROPERTIES
Look for files named PROPS_FILENAME to determine the basename.
Method Summary
static I18ngetI18n(Class clazz)
static I18ngetI18n(Class clazz, Locale locale)
static I18ngetI18n(Class clazz, Locale locale, int flags)
Returns the I18n instance responsible for translating messages in the package specified by clazz.
static I18ngetI18n(Class clazz, String bundleName)
static I18ngetI18n(Class clazz, String bundleName, Locale locale)
static I18ngetI18n(Class clazz, String bundleName, Locale locale, int flags)
static I18ngetI18n(String path, String bundleName, ClassLoader classLoader, Locale locale, int flags)

Field Detail

DEFAULT

public static final int DEFAULT
Use the default configuration.

Since: 0.9.1

DEFAULT_BASE_NAME

public static final String DEFAULT_BASE_NAME
Default name for Message bundles, is "i18n.Messages".

Since: 0.9.1

FALLBACK

public static final int FALLBACK
Fall back to a default resource bundle that returns the passed text if no resource bundle can be located.

Since: 0.9.1

NO_CACHE

public static final int NO_CACHE
Do not cache I18n instance.

Since: 0.9.1

PROPS_FILENAME

public static final String PROPS_FILENAME
Filename of the properties file that contains the i18n properties, is "i18n.properties".

Since: 0.9

READ_PROPERTIES

public static final int READ_PROPERTIES
Look for files named PROPS_FILENAME to determine the basename.

Since: 0.9.1

Method Detail

getI18n

public static I18n getI18n(Class clazz)
Calls getI18n(clazz, Locale.getDefault()).

getI18n

public static I18n getI18n(Class clazz, Locale locale)
Calls getI18n(clazz, locale, READ_PROPERTIES).

Since: 0.9.1

getI18n

public static I18n getI18n(Class clazz, Locale locale, int flags)
Returns the I18n instance responsible for translating messages in the package specified by clazz.

Lookup works by iterating upwards in the package hierarchy: First the internal cache is asked for an I18n object for a package, otherwise the algorithm looks for an i18n.properties file in the package. The properties file is queried for a key named basename whose value should be the fully qualified resource/class name of the resource bundle, e.g org.xnap.commons.i18n.Messages.

If after the first iteration no I18n instance has been found, a second search begins by looking for resource bundles having the name baseName.

Parameters: clazz the package hierarchy of the clazz and its class loader are used for resolving and loading the resource bundle baseName the name of the underlying resource bundle locale the locale of the underlying resource bundle flags a combination of these configuration flags: FALLBACK

Returns: created or cached I18n instance

Throws: MissingResourceException if no resource bundle was found

Since: 0.9.1

getI18n

public static I18n getI18n(Class clazz, String bundleName)
Calls getI18n(clazz, bundleName, Locale.getDefault()).

Since: 0.9

getI18n

public static I18n getI18n(Class clazz, String bundleName, Locale locale)
Calls getI18n(clazz, bundleName, locale, DEFAULT).

Since: 0.9.1

getI18n

public static I18n getI18n(Class clazz, String bundleName, Locale locale, int flags)
Calls getI18n(getPackageName(clazz), bundleName, clazz.getClassLoader(), locale, DEFAULT).

Since: 0.9.1

getI18n

public static I18n getI18n(String path, String bundleName, ClassLoader classLoader, Locale locale, int flags)

Since: 0.9.1