org.universAAL.ui.internationalization.util
Class MessageLocaleHelper

Package class diagram package MessageLocaleHelper
java.lang.Object
  extended by org.universAAL.ui.internationalization.util.MessageLocaleHelper

public class MessageLocaleHelper
extends Object

Helper class to get the appropriate messages in the preferred (or secondary, if preferred not available) language. Determines the Locale of the preferred (or secondary) Language for the user stored in UIPreferencesSubProfile. Then it can be used as normal messages class, to retrieve the correct internationalized messages for the given user.

Author:
amedrano, eandgrg

Constructor Summary
MessageLocaleHelper(ModuleContext mc, UIPreferencesSubProfile uiPreferencesSubprofile, List<URL> urlList)
          This constructor is to be used when the UIPreferencesSubProfile is already available, IE: by the DialogManager.
MessageLocaleHelper(ModuleContext mc, User user, List<URL> urlList)
          This constructor should be used by Applications.
 
Method Summary
 Locale getSelectedMessageLocale()
          Get the selected Locale for the messages.
 String getString(String key)
          Get a string in internationalization Messages file.
 String getString(String key, String[] substitutions)
          Like getString(String), This method will get the internationalization string for a given key.
 Locale getUserLocaleFromPreferredLanguage()
          Get the language for the user.
 void reloadMessages()
          Reloads the messages.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageLocaleHelper

public MessageLocaleHelper(ModuleContext mc,
                           User user,
                           List<URL> urlList)
                    throws Exception
This constructor should be used by Applications. Typical Call is something like:
 
 ArrayList list = new ArrayList();
 list.add(getClass().getClassLoader().getResource("messages.properties"));
 list.add(new File(CONFIG_FOLDER, "messages.properties").toURI().toURL());
 list.add(new URL("Form_Resource_Server"));
 //list.add(Other sources, for example a web server hosting these files);
 MessageLocaleHelper messages = new MessageLocaleHelper(mc, user, list);
 
 ...
 
 messages.getString("welcomeScreen.sayHello");
 
 

Parameters:
mc - The ModuleContext to enable logging and service call.
user - The User to address the messages.
urlList - the list of alternative URL locations for messages.
Throws:
Exception
See Also:
MessageLocaleHelper#reloadMessages()}

MessageLocaleHelper

public MessageLocaleHelper(ModuleContext mc,
                           UIPreferencesSubProfile uiPreferencesSubprofile,
                           List<URL> urlList)
                    throws Exception
This constructor is to be used when the UIPreferencesSubProfile is already available, IE: by the DialogManager.

Parameters:
mc -
uiPreferencesSubprofile -
urlList -
Throws:
Exception
See Also:
MessageLocaleHelper#reloadMessages()}
Method Detail

reloadMessages

public void reloadMessages()
                    throws Exception
Reloads the messages. Used when messages files are updated. It checks all the Alternative Locations for the preferred language, the first instance (in the alternatives list) of preferred language locale messages is loaded. If there are no preferred language locale messages files, then the first instance of secondary language is loaded. As last resort if nor primary nor secondary language is found then the first instance of default message file is loaded. It will throw an exception if no files could be loaded.

Throws:
Exception

getUserLocaleFromPreferredLanguage

public final Locale getUserLocaleFromPreferredLanguage()
Get the language for the user.

Returns:
the Locale from the preferred (or if preferred not found secondary) language for the user stored in UIPreferencesSubProfile.

getSelectedMessageLocale

public final Locale getSelectedMessageLocale()
Get the selected Locale for the messages.

Returns:
the selected message locale, null if default is selected.

getString

public final String getString(String key)
Get a string in internationalization Messages file.

Parameters:
key - the key for the string
Returns:
the string.

getString

public String getString(String key,
                        String[] substitutions)
Like getString(String), This method will get the internationalization string for a given key. Aditionally it will subsitude the variables defined like "{n}" with the String in the index n of the substitutions parameter.
Eg: for a properties file conaining:
key=page \{0\} of \{1\}
this code:
 MessageLocaleHelper mlh = ...
 mlh.getString("key",new String[]{"1","10"});
 
will return: "page 1 of 10"

Parameters:
key -
substitutions -
Returns:


Copyright © 2014 universAAL Consortium. All Rights Reserved.