org.clazzes.util.aop.i18n
Class MessagesFactory

java.lang.Object
  extended by org.clazzes.util.aop.i18n.MessagesFactory

public abstract class MessagesFactory
extends Object

A factory for Messages, which eases the retrieval of localized resources.


Field Summary
static String SOURCE_LANGUAGE_PROPERTY
           
 
Constructor Summary
MessagesFactory()
           
 
Method Summary
protected static ResourceBundle loadResources(Locale locale, ClassLoader cl, String path)
           
static Messages newMessages(Locale locale, Class<?> forClass)
          Return a Messages instance by locale and a resource bundle to be loaded from a properties file using the class loader and the class name of the given class.
static Messages newMessages(Locale locale, ClassLoader classLoader, String resourcePath)
          Construct a Messages instance by locale and a resource bundle to be loaded from a properties file from the resourcePath from the given class loader.
static Messages newMessages(Locale locale, PluralRule pr, Class<?> forClass)
          Return a Messages instance by locale, plural rule and a resource bundle to be loaded from a properties file using the class loader and the class name of the given class.
static Messages newMessages(Locale locale, PluralRule pr, ClassLoader classLoader, String resourcePath)
          Construct a Messages instance by locale, plural rule and a resource bundle to be loaded from a properties file from the resourcePath from the given class loader.
static Messages newMessages(Locale locale, PluralRule pr, ResourceBundle resources)
          Construct a Messages instance by locale, plural rule and a resource bundle.
static Messages newMessages(Locale locale, ResourceBundle resources)
          Construct a Messages instance by locale,and a resource bundle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOURCE_LANGUAGE_PROPERTY

public static final String SOURCE_LANGUAGE_PROPERTY
See Also:
Constant Field Values
Constructor Detail

MessagesFactory

public MessagesFactory()
Method Detail

loadResources

protected static final ResourceBundle loadResources(Locale locale,
                                                    ClassLoader cl,
                                                    String path)

newMessages

public static Messages newMessages(Locale locale,
                                   PluralRule pr,
                                   ResourceBundle resources)
Construct a Messages instance by locale, plural rule and a resource bundle.

Parameters:
locale - The locale to use.
pr - The plural rule to use.
resources - The resource bundle to load messages from.
Returns:
A new Messages instance.

newMessages

public static Messages newMessages(Locale locale,
                                   ResourceBundle resources)
Construct a Messages instance by locale,and a resource bundle. The plural rule is constructed using the default plural rule factory.

Parameters:
locale - The locale to use.
resources - The resource bundle to load messages from.
Returns:
A new Messages instance.

newMessages

public static Messages newMessages(Locale locale,
                                   PluralRule pr,
                                   ClassLoader classLoader,
                                   String resourcePath)
Construct a Messages instance by locale, plural rule and a resource bundle to be loaded from a properties file from the resourcePath from the given class loader.

Parameters:
locale - The locale to use.
pr - The plural rule to use.
classLoader - The class loader to load the resource bundle from.
resourcePath - A resource path specified as a dot-separated class name.
Returns:
A new Messages instance.

newMessages

public static Messages newMessages(Locale locale,
                                   ClassLoader classLoader,
                                   String resourcePath)

Construct a Messages instance by locale and a resource bundle to be loaded from a properties file from the resourcePath from the given class loader.

If the resource could no be found for the given language, the default properties file without language extension will be loaded and the locale will be set to the language contained in the "org.clazzes.util.aop.i18n.sourceLanguage" inside the fallback resources. If no "org.clazzes.util.aop.i18n.sourceLanguage" could be found, the fallback language is assumed to be Locale.ENGLISH.

The plural rule is constructed using the default plural rule factory.

Parameters:
locale - The locale to use.
classLoader - The class loader to load the resource bundle from.
resourcePath - A resource path specified as a dot-separated class name.
Returns:
A new Messages instance.

newMessages

public static Messages newMessages(Locale locale,
                                   PluralRule pr,
                                   Class<?> forClass)
Return a Messages instance by locale, plural rule and a resource bundle to be loaded from a properties file using the class loader and the class name of the given class.

Parameters:
locale - The locale to use.
pr - The plural rule to use.
forClass - The class to use as the class loader and path hint.
Returns:
A new Messages instance.

newMessages

public static Messages newMessages(Locale locale,
                                   Class<?> forClass)

Return a Messages instance by locale and a resource bundle to be loaded from a properties file using the class loader and the class name of the given class. The plural rule is constructed using the default plural rule factory.

If the resource could no be found for the given language, the default properties file without language extension will be loaded and the locale will be set to the language contained in the "org.clazzes.util.aop.i18n.sourceLanguage" inside the fallback resources. If no "org.clazzes.util.aop.i18n.sourceLanguage" could be found, the fallback language is assumed to be Locale.ENGLISH.

This method offers the most convenient way to load resources in an application by writing an accessor class

 package org.my.pkg.i18n;
 abstract class MyMessages {
   public static final Messages getMesssages(Locale locale) {
     return MessagesFactory.newMessages(locale,MyMessages.class);
 }
 
and placing your resources in files like:
 org/my/pkg/i18n/MyMessages.properties
 org/my/pkg/i18n/MyMessages_de.properties
 org/my/pkg/i18n/MyMessages_fr.properties
 ...
 

Parameters:
locale - The locale to use.
forClass - The class to use as the class loader and path hint.
Returns:
A new Messages instance.


Copyright © 2012. All Rights Reserved.