net.sf.sfac.lang
Class LanguageSupport

java.lang.Object
  extended by net.sf.sfac.lang.LanguageSupport
Direct Known Subclasses:
LanguageSupportImpl, LanguageSupportMock

public abstract class LanguageSupport
extends Object

Multilingual support: Class for the class managing the application locale and bundles.
This class defines a singleton 'language service' allowing to get string values in the current locale and to register for notification when locale changes. This 'language service' can find translations in a set of bundles.

Author:
Olivier Berlanger

Constructor Summary
LanguageSupport()
           
 
Method Summary
static void addLanguageListener(LanguageListener lis)
          Add a listener to be notified for all locale changes.
abstract  void addLanguageListenerImpl(LanguageListener lis)
           
abstract  LanguageSupport copyInstance()
           
static Locale getCurrentLocale()
          Get the current Locale of the LanguageSupport.
abstract  Locale getCurrentLocaleImpl()
           
static LanguageSupport getInstance()
          Get the unique LanguageSupport instance.
static String getLocalizedString(String key)
          Get the language-dependent string associated to the given key in the current locale.
static String getLocalizedString(String key, Object... params)
          Get the language-dependent string associated to the given key in the current locale.
abstract  String getLocalizedStringImpl(String key)
           
abstract  String getLocalizedStringImpl(String key, Object... params)
           
static String getOptionalLocalizedString(String key, String defaultValue)
          Get an optional language-dependent string associated to the given key in the current locale.
abstract  String getOptionalLocalizedStringImpl(String key, String defaultValue)
           
static void removeLanguageListener(LanguageListener lis)
          Remove a previously added LanguageListener.
abstract  void removeLanguageListenerImpl(LanguageListener lis)
           
static void setInstance(LanguageSupport newInstance)
          Set the unique LanguageSupport instance, any previous instance is discarded.
static void updateLanguage(String newLocaleId)
          Ask the language manager to change current locale to the given one.
abstract  void updateLanguageImpl(String newLocaleId)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LanguageSupport

public LanguageSupport()
Method Detail

getInstance

public static LanguageSupport getInstance()
Get the unique LanguageSupport instance. Note: this method is not synchronized because, as in all swing application, it is supposed to be accessed from a single-threaded environment.

Returns:
the global LanguageSupport instance (never null)

setInstance

public static void setInstance(LanguageSupport newInstance)
Set the unique LanguageSupport instance, any previous instance is discarded. Note: this method is not synchronized because, as in all swing application, it is supposed to be accessed from a single-threaded environment.


getLocalizedString

public static String getLocalizedString(String key)
Get the language-dependent string associated to the given key in the current locale. The key should be defined in the LanguageSupport, if not, the string returned is the key itself and an warning message is logged.

Parameters:
key - Key of the string in the LanguageSupport.
Returns:
The language-dependent string associated to the given key in the current locale or the key itself if no value is found for the key.

getLocalizedString

public static String getLocalizedString(String key,
                                        Object... params)
Get the language-dependent string associated to the given key in the current locale. The key should be defined in the LanguageSupport, if not, the string returned is the key itself and an warning message is logged.

Parameters:
key - Key of the string in the LanguageSupport.
params - Parameters to place in the string value (using a TextFormat).
Returns:
The language-dependent string associated to the given key in the current locale or the key itself if no value is found for the key.

getOptionalLocalizedString

public static String getOptionalLocalizedString(String key,
                                                String defaultValue)
Get an optional language-dependent string associated to the given key in the current locale. If the key is not defined in the LanguageSupport, the default value is returned.

Parameters:
key - Key of the string in the LanguageSupport.
defaultValue - Default value to be returned if the string is not found in the LanguageSupport.
Returns:
The language-dependent string associated to the given key in the current locale or the default value if no value is found for the key.

updateLanguage

public static void updateLanguage(String newLocaleId)
Ask the language manager to change current locale to the given one. If the given locale id is not the current one, Resources (bundles) of the LanguageSupport will be updated and then an event will be fired to all listener.

Parameters:
newLocaleId - ID of the new locale.

getCurrentLocale

public static Locale getCurrentLocale()
Get the current Locale of the LanguageSupport.

Returns:
The current Locale of the LanguageSupport.

addLanguageListener

public static void addLanguageListener(LanguageListener lis)
Add a listener to be notified for all locale changes.
Only a soft reference to the listeners will be kept by this class, so that it will not prevent garbage collection when needed.

Parameters:
lis - the new listener.

removeLanguageListener

public static void removeLanguageListener(LanguageListener lis)
Remove a previously added LanguageListener. (does nothing if the given listener was not added)

Parameters:
lis - the listener to remove.

copyInstance

public abstract LanguageSupport copyInstance()

getLocalizedStringImpl

public abstract String getLocalizedStringImpl(String key)

getLocalizedStringImpl

public abstract String getLocalizedStringImpl(String key,
                                              Object... params)

getOptionalLocalizedStringImpl

public abstract String getOptionalLocalizedStringImpl(String key,
                                                      String defaultValue)

updateLanguageImpl

public abstract void updateLanguageImpl(String newLocaleId)

getCurrentLocaleImpl

public abstract Locale getCurrentLocaleImpl()

addLanguageListenerImpl

public abstract void addLanguageListenerImpl(LanguageListener lis)

removeLanguageListenerImpl

public abstract void removeLanguageListenerImpl(LanguageListener lis)


Copyright © 2012. All Rights Reserved.