/*
* Jacareto Copyright (c) 2002-2005
* Applied Computer Science Research Group, Darmstadt University of
* Technology, Institute of Mathematics & Computer Science,
* Ludwigsburg University of Education, and Computer Based
* Learning Research Group, Aachen University. All rights reserved.
*
* Jacareto is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* Jacareto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with Jacareto; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
package jacareto.system;
import jacareto.toolkit.EnhancedHashtable;
import jacareto.toolkit.HashtableException;
import org.apache.log4j.Logger;
import java.io.IOException;
/**
* A storage for texts depending on a language (keyword -> text mappings like the ResourceBundle
* class of the java API) The mapping can be given as a enhanced hashtable object. All values will
* be interpreted as Strings. TODO: Default language if a string cannot be found in the specified
* language
*
* @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
* @version 1.01
*/
public class Language {
/** The mapping. */
private EnhancedHashtable mapping;
/** The logger object. */
private Logger logger;
/** The identifier string of this language (en, for example). */
private String idstring;
/**
* Creates a new language object with the given mapping and the given logger instance.
*
* @param mapping the mapping of keywords to language dependent texts
* @param logger the logger instance for error messages
*/
public Language (EnhancedHashtable mapping, Logger logger) {
setMapping (mapping);
setLogger (logger);
setIdentifier ("");
}
/**
* Creates a new language object with an empty mapping and the root logger.
*/
public Language () {
this(new EnhancedHashtable(), Logger.getRootLogger ());
}
/**
* Creates a new language object which loads the mapping from a properties file specified in
* the customization instance for a given language.
*
* @param customization the customization instance with th language -> properties filename
* mapping
* @param basedir the basedir for the properties files
* @param language the language ("en" for English, for example)
* @param logger the logger instance
*
* @throws IOException if the language file could not be read
* @throws HashtableException if there is no setting for the language file map or the language
* key is not contained in the map
*/
public Language (Customization customization, String basedir, String language, Logger logger)
throws IOException, HashtableException {
this(new Customization(basedir + "/" +
customization.getMap ("Language.Files").getString (language)), logger);
setIdentifier (language);
}
/**
* Creates a new language object which loads the mapping from a properties file specified in
* the customization instance for a given language.
*
* @param customization the customization instance with th language -> properties filename
* mapping
* @param language the language ("en" for English, for example)
* @param logger the logger instance
*
* @throws IOException if the language file could not be read
* @throws HashtableException if there is no setting for the language file map or the language
* key is not contained in the map
*/
public Language (Customization customization, String language, Logger logger)
throws IOException, HashtableException {
this(new Customization(customization.getMap ("Language.Files").getString (language)), logger);
setIdentifier (language);
}
/**
* Sets the logger instance
*
* @param logger the new logger object
*/
public void setLogger (Logger logger) {
this.logger = logger;
}
/**
* Returns the current logger instance.
*
* @return DOCUMENT ME!
*/
public Logger getLogger () {
return logger;
}
/**
* Sets the identifier of this language (en, for example.
*
* @param idstring the identifier string
*/
public void setIdentifier (String idstring) {
this.idstring = idstring;
}
/**
* Returns the identifier of this language (en, for example).
*
* @return the identifier string
*/
public String getIdentifier () {
return idstring;
}
/**
* Sets the mapping
*
* @param mapping the new mapping
*/
public void setMapping (EnhancedHashtable mapping) {
this.mapping = mapping;
}
/**
* Returns the current mapping.
*
* @return DOCUMENT ME!
*/
public EnhancedHashtable getMapping () {
return mapping;
}
/**
* Returns the text belonging to a given keyword. If the key does not exist or the text is
* <code>null</code>, an error message will be printed and the empty string. will be returned.
*
* @param key the keyword
*
* @return the text belonging to the keyword, or the empty string if the key does not exist or
* the text is <code>null</code>.
*/
public String getString (String key) {
try {
return mapping.getString (key);
} catch (HashtableException h) {
logger.error ("Language Error: Non-existent key \"" + key + "\".");
return "";
}
}
/**
* Sets a value for a key.
*
* @param key the keyword
* @param value the value
*/
public void setString (String key, String value) {
mapping.put (key, value);
}
/**
* Returns whether or not a key is contained.
*
* @param key the key
*
* @return <code>true</code> if the key is contained, otherwise <code>false</code>
*/
public boolean containsKey (String key) {
return mapping.containsKey (key);
}
}
|