Class Messages
This class offers some methods to provide internationalization services to other projects. All the methods are static.
The most useful method is getText(key) (and family),
which returns the translation associated
with the provided key. The class must be initialized before getText can be
used.
The typical usage sequence would be:
- Add some locale to the prefered locales list:
Messages.addLocale(new Locale("es")) - Add some resource file containing translations:
Messages.addResourceFamily("text", new File(".")) - And finaly getText can be used:
Messages.getText("aceptar")
The resource files are Java properties files, which contain key=translation
pairs, one
pair per line. These files must be encoded using iso-8859-1 encoding, and unicode escaped
sequences must be used to include characters outside the former encoding.
There are several ways to specify the property file to load, see the different
addResourceFamily methods for details.
- Author:
- Cesar Martinez Izquierdo (cesar.martinez@iver.es)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdds a Locale at the end of the ordered list of preferred locales.static voidaddResourceFamily(String family, File folder) Adds an additional family of resource files containing some translations.static voidaddResourceFamily(String family, File[] dirList) Adds an additional family of resource files containing some translations.static voidaddResourceFamily(String family, ClassLoader loader) Adds an additional family of resource files containing some translations.static voidaddResourceFamily(String family, ClassLoader loader, String callerName) Adds an additional family of resource files containing some translations.static voidaddResourceFamily(String family, String callerName) Adds an additional family of resource files containing some translations.static PropertiesgetAllTexts(Locale lang) static StringGets the base language, the language considered the origin of translations, which will be (possibly) stored in a property file without language suffix (ie: text.properties instead of text_es.properties).static Localestatic Liststatic ArrayListReturns an ArrayList containing the ordered list of prefered Locales Each element of the ArrayList is a Locale object.static StringGets the localized message associated with the provided key.static StringGets the localized message associated with the provided key.static StringGets the localized message associated with the provided key.static Stringstatic Stringstatic Stringstatic Stringstatic StringGets the localized message associated with the provided key.static PropertiesgetTranslations(Locale locale) static booleanChecks if some locale has been added to the preferred locales list, which is necessary before loading any translation because only the translations for the preferred locales are loaded.protected static Setstatic booleanremoveLocale(Locale lang) Removes the specified Locale from the list of preferred locales and the translations associated with this locale.static voidCleans the translation tables (removes all the translations from memory).static voidsetBaseLanguage(String lang) Sets the base language, the language considered the origin of translations, which will be (possibly) stored in a property file without language suffix (ie: text.properties instead of text_es.properties).static voidsetCurrentLocale(Locale locale) static voidsetCurrentLocale(Locale locale, Locale[] alternatives) static voidsetPreferredLocales(ArrayList preferredLocalesList) Sets the ordered list of preferred locales.protected static intThe number of translation keys which have been loaded till now (In other words: the number of available translation strings).static Stringstatic String
-
Constructor Details
-
Messages
public Messages()
-
-
Method Details
-
getText
Gets the localized message associated with the provided key. If the key is not in the dictionary, return the key and register the failure in the log.
The
callerNameparameter is only used as a label when logging, so any String can be used. However, a meaningful String should be used, such as the name of the class requiring the translation services, in order to identify the source of the failure in the log.- Parameters:
key- An String which identifies the translation that we want to get.callerName- A symbolic name given to the caller of this method, to show it in the log if the key was not found- Returns:
- an String with the message associated with the provided key. If the key is not in the dictionary, return the key. If the key is null, return null.
-
getText
-
translate
-
translate
-
getText
Gets the localized message associated with the provided key. If the key is not in the dictionary or the translation is empty, return the key and register the failure in the log.
- Parameters:
key- An String which identifies the translation that we want to get.- Returns:
- an String with the message associated with the provided key. If the key is not in the dictionary or the translation is empty, return the key. If the key is null, return null.
-
getText
-
getText
Gets the localized message associated with the provided key. If the key is not in the dictionary or the translation is empty, it returns null and the failure is only registered in the log if the param log is true.
- Parameters:
key- An String which identifies the translation that we want to get.log- Determines whether log a key failure or not- Returns:
- an String with the message associated with the provided key, or null if the key is not in the dictionary or the translation is empty.
-
getText
-
getText
Gets the localized message associated with the provided key. If the key is not in the dictionary, it returns null and the failure is only registered in the log if the param log is true.
- Parameters:
key- An String which identifies the translation that we want to get.callerName- A symbolic name given to the caller of this method, to show it in the log if the key was not foundlog- Determines whether log a key failure or not- Returns:
- an String with the message associated with the provided key, or null if the key is not in the dictionary.
-
getText
-
addResourceFamily
Adds an additional family of resource files containing some translations. A family is a group of files with a common baseName. The file must be an iso-8859-1 encoded file, which can contain any unicode character using unicode escaped sequences, and following the syntax:
key1=value1 key2=value2where 'key1' is the key used to identify the string and must not contain the '=' symbol, and 'value1' is the associated translation.For example:
cancel=Cancelar accept=AceptarOnly one pair key-value is allowed per line.
The actual name of the resource file to load is determined using the rules explained in the class java.util.ResourceBundle. Summarizing, for each language in the specified preferred locales list it will try to load a file with the following structure:
family_locale.propertiesFor example, if the preferred locales list contains {"fr", "es", "en"}, and the family name is "text", it will try to load the files "text_fr.properties", "text_es.properties" and finally "text_en.properties".
Locales might be more specific, such us "es_AR" (meaning Spanish from Argentina) or "es_AR_linux" (meaning Linux system preferring Spanish from Argentina). In the later case, it will try to load "text_es_AR_linux.properties", then "text_es_AR.properties" if the former fails, and finally "text_es.properties".
The directory used to locate the resource file is determining by using the getResource method from the provided ClassLoader.
- Parameters:
family- The family name (or base name) which is used to search actual properties files.loader- A ClassLoader which is able to find a property file matching the specified family name and the preferred locales- See Also:
-
addResourceFamily
Adds an additional family of resource files containing some translations. The search path to locate the files is provided by the dirList parameter.
See
addResourceFamily(String, ClassLoader)for a discussion about the format of the property files and the way to determine the candidat files to load. Note that those methods are different in the way to locate the candidat files. This method searches in the provided paths (dirListparameter), while the referred method searches using the getResource method of the provided ClassLoader.- Parameters:
family- The family name (or base name) which is used to search actual properties files.dirList- A list of search paths to locate the property files- Throws:
MalformedURLException- See Also:
-
addResourceFamily
Adds an additional family of resource files containing some translations. The search path to locate the files is provided by the dir parameter.
See
addResourceFamily(String, ClassLoader)for a discussion about the format of the property files and the way to determine the candidat files to load. Note that those methods are different in the way to locate the candidat files. This method searches in the provided path (dirparameter), while the referred method searches using the getResource method of the provided ClassLoader.- Parameters:
family- The family name (or base name) which is used to search actual properties files.folder- The search path to locate the property files- Throws:
MalformedURLException- See Also:
-
addResourceFamily
Adds an additional family of resource files containing some translations. The search path is determined by the getResource method from the provided ClassLoader.
This method is identical to
addResourceFamily(String, ClassLoader), except that it adds acallerNameparameter to show in the log.See
addResourceFamily(String, ClassLoader)for a discussion about the format of the property files andthe way to determine the candidat files to load.- Parameters:
family- The family name (or base name) which is used to search actual properties files.loader- A ClassLoader which is able to find a property file matching the specified family name and the preferred localescallerName- A symbolic name given to the caller of this method, to show it in the log if there is an error- See Also:
-
addResourceFamily
Adds an additional family of resource files containing some translations.
This method is identical to
addResourceFamily(String, ClassLoader, String), except that it uses the caller's class loader.See
addResourceFamily(String, ClassLoader)for a discussion about the format of the property files and the way to determine the candidat files to load.- Parameters:
family- The family name (or base name) which is used to search actual properties files.callerName- A symbolic name given to the caller of this method, to show it in the log if there is an error. This is only used to show something meaningful in the log, so you can use any string- See Also:
-
getPreferredLocales
Returns an ArrayList containing the ordered list of prefered Locales Each element of the ArrayList is a Locale object.- Returns:
- an ArrayList containing the ordered list of prefered Locales Each element of the ArrayList is a Locale object.
-
setPreferredLocales
Sets the ordered list of preferred locales. Each element of the ArrayList is a Locale object.
Note that calling this method does not load any translation, it just adds the language to the preferred locales list, so this method must be always called before the translations are loaded using the addResourceFamily() methods.
It there was any language in the preferred locale list, the language and its associated translations are deleted.
- Parameters:
preferredLocalesList- an ArrayList containing Locale objects. The ArrayList represents an ordered list of preferred locales
-
getCurrentLocale
-
setCurrentLocale
- Parameters:
locale-
-
setCurrentLocale
-
addLocale
Adds a Locale at the end of the ordered list of preferred locales. Note that calling this method does not load any translation, it just adds the language to the preferred locales list, so this method must be always called before the translations are loaded using the addResourceFamily() methods.- Parameters:
lang- A Locale object specifying the locale to add
-
removeLocale
Removes the specified Locale from the list of preferred locales and the translations associated with this locale.- Parameters:
lang- A Locale object specifying the locale to remove- Returns:
- True if the locale was in the preferred locales list, false otherwise
-
removeResources
public static void removeResources()Cleans the translation tables (removes all the translations from memory). -
size
The number of translation keys which have been loaded till now (In other words: the number of available translation strings).- Parameters:
lang- The language for which we want to know the number of translation keys- Returns:
- The number of translation keys for the provided language.
-
keySet
-
hasLocales
public static boolean hasLocales()Checks if some locale has been added to the preferred locales list, which is necessary before loading any translation because only the translations for the preferred locales are loaded.- Returns:
-
getBaseLanguage
Gets the base language, the language considered the origin of translations, which will be (possibly) stored in a property file without language suffix (ie: text.properties instead of text_es.properties).- Returns:
- the base language name
-
setBaseLanguage
Sets the base language, the language considered the origin of translations, which will be (possibly) stored in a property file without language suffix (ie: text.properties instead of text_es.properties).- Parameters:
lang- The base language to be set
-
getAllTexts
-
getTranslations
-
getNotTranslatedKeys
-