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:      */
sascha@2290:     public synchronized 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: 
sascha@2290:     private static synchronized void ensureInstance() {
ingo@418:         if (INSTANCE == null) {
ingo@418:             INSTANCE = new Resources();
ingo@418:         }
sascha@2290:     }
sascha@2290: 
sascha@2290: 
sascha@2290:     public static Locale getLocale(CallMeta meta) {
sascha@2290:         ensureInstance();
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) {
sascha@2290:         ensureInstance();
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: 
sascha@2166:     public static String getMsg(
christian@3771:             CallMeta meta,
christian@3771:             String   key,
christian@3771:             Object[] args
christian@3771:             ) {
sascha@2166:         return getMsg(meta, key, key, args);
sascha@2166:     }
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(
christian@3771:             CallMeta meta,
christian@3771:             String   key,
christian@3771:             String   def,
christian@3771:             Object[] args)
ingo@413:     {
sascha@2166:         String template = getMsg(meta, key, (String)null);
ingo@413: 
ingo@413:         if (template == null) {
ingo@413:             return def;
ingo@413:         }
ingo@413: 
christian@3771:         return format(meta, template, args);
ingo@413:     }
ingo@413: 
sascha@3170:     public static String format(
christian@3771:             CallMeta   meta,
christian@3771:             String     key,
christian@3771:             String     def,
christian@3771:             Object ... args
christian@3771:             ) {
sascha@3170:         String template = getMsg(meta, key, (String)null);
sascha@3170: 
sascha@3170:         if (template == null) {
sascha@3398:             template = def;
sascha@3170:         }
sascha@3170: 
christian@3771:         return format(meta, template, args);
christian@3771:     }
christian@3771: 
christian@3771:     /**
christian@3771:      * Formats the given template using the arguments with respect of the
christian@3771:      * appropriate locale given by the CallMeta instance.
christian@3771:      */
christian@3771:     public static String format(CallMeta meta, String templ, Object ... args) {
christian@3771:         Locale locale = getLocale(meta);
christian@3771:         MessageFormat mf = new MessageFormat(templ, locale);
christian@3771: 
christian@3771:         return mf.format(args, new StringBuffer(), null).toString();
sascha@3170:     }
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 :