com.threerings.util
Class MessageManager

java.lang.Object
  extended by com.threerings.util.MessageManager

public class MessageManager
extends Object

The message manager provides a thin wrapper around Java's built-in localization support, supporting a policy of dividing up localization resources into logical units, all of the translations for which are contained in a single messages file.

The message manager assumes that the locale remains constant for the duration of its operation. If the locale were to change during the operation of the client, a call to setLocale(java.util.Locale) should be made to inform the message manager of the new locale (which will clear the message bundle cache).


Field Summary
static String GLOBAL_BUNDLE
          The name of the global resource bundle (which other bundles revert to if they can't locate a message within themselves).
 
Constructor Summary
MessageManager(String resourcePrefix)
          Constructs a message manager with the supplied resource prefix and the default locale.
 
Method Summary
 MessageBundle getBundle(String path)
          Fetches the message bundle for the specified path.
 Locale getLocale()
          Get the locale that is being used to translate messages.
 void setClassLoader(ClassLoader loader)
          Allows a custom classloader to be configured for locating translation resources.
 void setLocale(Locale locale)
          Sets the locale to the specified locale.
 void setLocale(Locale locale, boolean updateGlobal)
          Sets the locale to the specified locale.
 void setPrefix(String resourcePrefix)
          Sets the appropriate resource prefix for where to find subsequent message bundles.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GLOBAL_BUNDLE

public static final String GLOBAL_BUNDLE
The name of the global resource bundle (which other bundles revert to if they can't locate a message within themselves). It must be named global.properties and live at the top of the bundle hierarchy.

See Also:
Constant Field Values
Constructor Detail

MessageManager

public MessageManager(String resourcePrefix)
Constructs a message manager with the supplied resource prefix and the default locale. The prefix will be prepended to the path of all resource bundles prior to their resolution. For example, if a prefix of rsrc.messages was provided and a message bundle with the name game.chess was later requested, the message manager would attempt to load a resource bundle with the path rsrc.messages.game.chess and would eventually search for a file in the classpath with the path rsrc/messages/game/chess.properties.

See the documentation for ResourceBundle.getBundle(String,Locale,ClassLoader) for a more detailed explanation of how resource bundle paths are resolved.

Method Detail

getLocale

public Locale getLocale()
Get the locale that is being used to translate messages. This may be useful if using standard translations, for example new SimpleDateFormat("EEEE", getLocale()) to get the name of a weekday that matches the language being used for all other client translations.


setLocale

public void setLocale(Locale locale)
Sets the locale to the specified locale. Subsequent message bundles fetched via the message manager will use the new locale. The message bundle cache will also be cleared.


setLocale

public void setLocale(Locale locale,
                      boolean updateGlobal)
Sets the locale to the specified locale. Subsequent message bundles fetched via the message manager will use the new locale. The message bundle cache will also be cleared.

Parameters:
updateGlobal - set to true if you want the global bundle reloaded in the new locale

setPrefix

public void setPrefix(String resourcePrefix)
Sets the appropriate resource prefix for where to find subsequent message bundles.


setClassLoader

public void setClassLoader(ClassLoader loader)
Allows a custom classloader to be configured for locating translation resources.


getBundle

public MessageBundle getBundle(String path)
Fetches the message bundle for the specified path. If no bundle can be located with the specified path, a special bundle is returned that returns the untranslated message identifiers instead of an associated translation. This is done so that error code to handle a failed bundle load need not be replicated wherever bundles are used. Instead an error will be logged and the requesting service can continue to function in an impaired state.



Copyright © 2012. All Rights Reserved.