ingo@125: package de.intevation.flys.artifacts.resources;
ingo@125: 
ingo@413: import java.text.MessageFormat;
ingo@125: import java.util.Locale;
ingo@125: import java.util.MissingResourceException;
ingo@125: import java.util.ResourceBundle;
ingo@125: 
ingo@125: import org.apache.log4j.Logger;
ingo@125: 
ingo@125: import de.intevation.artifacts.CallMeta;
ingo@125: 
ingo@125: /**
ingo@125:  * This class provides methods for i18n.
ingo@125:  *
ingo@125:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@125:  */
ingo@125: public class Resources {
ingo@125: 
ingo@125:     /** The logger that is used in this class.*/
ingo@125:     private static Logger logger = Logger.getLogger(Resources.class);
ingo@125: 
ingo@125:     /** The singleton instance.*/
ingo@125:     private static Resources INSTANCE;
ingo@125: 
ingo@125:     /** The locales supported by this server.*/
ingo@125:     protected Locale[] locales;
ingo@125: 
ingo@125:     /**
ingo@125:      * No instance of this class is necessary.
ingo@125:      */
ingo@125:     private Resources() {
ingo@125:     }
ingo@125: 
ingo@125: 
ingo@125:     /**
ingo@125:      * Returns the locales supported by this server.
ingo@125:      *
ingo@125:      * @return the supported locales.
ingo@125:      */
ingo@125:     public Locale[] getLocales() {
ingo@125:         if (locales == null) {
ingo@125:             readLocales();
ingo@125:         }
ingo@125: 
ingo@125:         return locales;
ingo@125:     }
ingo@125: 
ingo@125: 
ingo@125:     /**
ingo@125:      * Read the locales configured for this server.
ingo@125:      */
ingo@125:     protected void readLocales() {
ingo@125:         // TODO IMPLEMENT ME
ingo@125: 
ingo@125:         locales = new Locale[2];
ingo@125:         locales[0] = Locale.GERMANY;
ingo@125:         locales[1] = Locale.ENGLISH;
ingo@125:     }
ingo@125: 
ingo@125: 
ingo@418:     public static Locale getLocale(CallMeta meta) {
ingo@418:         if (INSTANCE == null) {
ingo@418:             INSTANCE = new Resources();
ingo@418:         }
ingo@418: 
ingo@418:         Locale[] locales = INSTANCE.getLocales();
ingo@418:         return meta.getPreferredLocale(locales);
ingo@418:     }
ingo@418: 
ingo@418: 
ingo@125:     /**
ingo@125:      * This method returns the translated value for <i>key</i> or <i>def</i> if
ingo@125:      * <i>key</i> is not existing in the resource bundle.
ingo@125:      *
ingo@125:      * @param meta The CallMeta object of the request that contains the
ingo@125:      * preferred locale.
ingo@125:      * @param key The key that should be translated.
ingo@125:      * @param def A default value that is returned, if <i>key</i> was not found.
ingo@125:      *
ingo@125:      * @return the translated message.
ingo@125:      */
ingo@125:     public static String getMsg(CallMeta meta, String key, String def) {
ingo@125:         if (INSTANCE == null) {
ingo@125:             INSTANCE = new Resources();
ingo@125:         }
ingo@125: 
ingo@125:         Locale[] locales = INSTANCE.getLocales();
ingo@125:         Locale   locale  = meta.getPreferredLocale(locales);
ingo@125: 
ingo@125:         return getMsg(locale, key, def);
ingo@125:     }
ingo@125: 
ingo@125: 
ingo@125:     /**
ingo@413:      * Returns a translated message based on a template specified by <i>key</i>
ingo@413:      * that has necessary values to be filled in.
ingo@413:      *
ingo@413:      * @param meta The CallMeta object.
ingo@413:      * @param key The key of the template in the resource bundle.
ingo@413:      * @param def the default value if no template was found with <i>key</i>.
ingo@413:      * @param args The arguments that are necessary for the template.
ingo@413:      *
ingo@413:      * @return a translated string.
ingo@413:      */
ingo@413:     public static String getMsg(
ingo@413:         CallMeta meta,
ingo@413:         String   key,
ingo@413:         String   def,
ingo@413:         Object[] args)
ingo@413:     {
ingo@413:         String template = getMsg(meta, key, null);
ingo@413: 
ingo@413:         if (template == null) {
ingo@413:             return def;
ingo@413:         }
ingo@413: 
ingo@413:         return MessageFormat.format(template, args);
ingo@413:     }
ingo@413: 
ingo@413: 
ingo@413:     /**
ingo@125:      * This method returns the translated value for <i>key</i> or <i>def</i> if
ingo@125:      * <i>key</i> is not existing in the resource bundle.
ingo@125:      *
ingo@125:      * @param locale The locale.
ingo@125:      * @param key The key that should be translated.
ingo@125:      * @param def A default value that is returned, if <i>key</i> was not found.
ingo@125:      *
ingo@125:      * @return the translated message.
ingo@125:      */
ingo@125:     public static String getMsg(Locale locale, String key, String def) {
ingo@125:         ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);
ingo@125: 
ingo@125:         try {
ingo@125:             return bundle.getString(key);
ingo@125:         }
ingo@125:         catch (MissingResourceException mre) {
ingo@125:             logger.warn("No message found for key: " + key);
ingo@125: 
ingo@125:             return def;
ingo@125:         }
ingo@125:     }
ingo@125: }
ingo@125: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :