view flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java @ 3785:a5f65e8983be

Merged revisions 5501-5502,5504-5508,5511-5513,5516-5519 via svnmerge from file:///home/clients/bsh/bsh-generischer-viewer/Material/SVN/flys-artifacts/trunk ........ r5501 | felix | 2012-09-18 11:49:45 +0200 (Di, 18 Sep 2012) | 1 line fix issue865 - missing showarea theme prop. ........ r5502 | clins | 2012-09-18 12:18:30 +0200 (Di, 18 Sep 2012) | 1 line Add robustness checks to prevent NPEs ........ r5504 | felix | 2012-09-18 14:03:15 +0200 (Di, 18 Sep 2012) | 1 line i18n for area label (fix issue487). ........ r5505 | clins | 2012-09-18 16:19:59 +0200 (Di, 18 Sep 2012) | 1 line Update themes to show point descriptions ........ r5506 | rrenkert | 2012-09-18 17:00:30 +0200 (Di, 18 Sep 2012) | 3 lines Removed incorrect characteristic diameter. ........ r5507 | rrenkert | 2012-09-18 17:03:20 +0200 (Di, 18 Sep 2012) | 3 lines Fixed some stupid bugs in bed quality data factory and calculation. ........ r5508 | teichmann | 2012-09-18 17:45:49 +0200 (Di, 18 Sep 2012) | 1 line The usual whitespace and import cleanups. ........ r5511 | teichmann | 2012-09-18 18:24:51 +0200 (Di, 18 Sep 2012) | 1 line Use generics aware Collections.emptyList(). ........ r5512 | teichmann | 2012-09-18 20:36:52 +0200 (Di, 18 Sep 2012) | 1 line Some more little steps towards "Auslagerung extremer Wasserspiegellagen". ........ r5513 | clins | 2012-09-18 23:38:19 +0200 (Di, 18 Sep 2012) | 1 line A and B facets of fix analyis are now deactivated by default ........ r5516 | bricks | 2012-09-19 10:45:51 +0200 (Mi, 19 Sep 2012) | 2 lines Add the gauge station to the GaugeOverviewInfoService xml response ........ r5517 | rrenkert | 2012-09-19 10:50:23 +0200 (Mi, 19 Sep 2012) | 3 lines Added CSV export to bed quality calculation. ........ r5518 | bricks | 2012-09-19 11:04:04 +0200 (Mi, 19 Sep 2012) | 2 lines Fix date in changelog entry ........ r5519 | teichmann | 2012-09-19 11:17:14 +0200 (Mi, 19 Sep 2012) | 1 line Removed trailing whitespace. ........ flys-artifacts/tags/2.9.1@5531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 19 Sep 2012 14:58:31 +0000
parents 04309ca24614
children
line wrap: on
line source
package de.intevation.flys.artifacts.resources;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import de.intevation.artifacts.CallMeta;

/**
 * This class provides methods for i18n.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class Resources {

    /** The logger that is used in this class.*/
    private static Logger logger = Logger.getLogger(Resources.class);

    /** The singleton instance.*/
    private static Resources INSTANCE;

    /** The locales supported by this server.*/
    protected Locale[] locales;

    /**
     * No instance of this class is necessary.
     */
    private Resources() {
    }


    /**
     * Returns the locales supported by this server.
     *
     * @return the supported locales.
     */
    public synchronized Locale [] getLocales() {
        if (locales == null) {
            readLocales();
        }

        return locales;
    }


    /**
     * Read the locales configured for this server.
     */
    protected void readLocales() {
        // TODO IMPLEMENT ME

        locales = new Locale[2];
        locales[0] = Locale.GERMANY;
        locales[1] = Locale.ENGLISH;
    }


    private static synchronized void ensureInstance() {
        if (INSTANCE == null) {
            INSTANCE = new Resources();
        }
    }


    public static Locale getLocale(CallMeta meta) {
        ensureInstance();

        Locale[] locales = INSTANCE.getLocales();
        return meta.getPreferredLocale(locales);
    }


    /**
     * This method returns the translated value for <i>key</i> or <i>def</i> if
     * <i>key</i> is not existing in the resource bundle.
     *
     * @param meta The CallMeta object of the request that contains the
     * preferred locale.
     * @param key The key that should be translated.
     * @param def A default value that is returned, if <i>key</i> was not found.
     *
     * @return the translated message.
     */
    public static String getMsg(CallMeta meta, String key, String def) {
        ensureInstance();

        Locale[] locales = INSTANCE.getLocales();
        Locale   locale  = meta.getPreferredLocale(locales);

        return getMsg(locale, key, def);
    }

    public static String getMsg(
            CallMeta meta,
            String   key,
            Object[] args
            ) {
        return getMsg(meta, key, key, args);
    }

    /**
     * Returns a translated message based on a template specified by <i>key</i>
     * that has necessary values to be filled in.
     *
     * @param meta The CallMeta object.
     * @param key The key of the template in the resource bundle.
     * @param def the default value if no template was found with <i>key</i>.
     * @param args The arguments that are necessary for the template.
     *
     * @return a translated string.
     */
    public static String getMsg(
            CallMeta meta,
            String   key,
            String   def,
            Object[] args)
    {
        String template = getMsg(meta, key, (String)null);

        if (template == null) {
            return def;
        }

        return format(meta, template, args);
    }

    public static String format(
            CallMeta   meta,
            String     key,
            String     def,
            Object ... args
            ) {
        String template = getMsg(meta, key, (String)null);

        if (template == null) {
            template = def;
        }

        return format(meta, template, args);
    }

    /**
     * Formats the given template using the arguments with respect of the
     * appropriate locale given by the CallMeta instance.
     */
    public static String format(CallMeta meta, String templ, Object ... args) {
        Locale locale = getLocale(meta);
        MessageFormat mf = new MessageFormat(templ, locale);

        return mf.format(args, new StringBuffer(), null).toString();
    }

    /**
     * This method returns the translated value for <i>key</i> or <i>def</i> if
     * <i>key</i> is not existing in the resource bundle.
     *
     * @param locale The locale.
     * @param key The key that should be translated.
     * @param def A default value that is returned, if <i>key</i> was not found.
     *
     * @return the translated message.
     */
    public static String getMsg(Locale locale, String key, String def) {
        ResourceBundle bundle = ResourceBundle.getBundle("messages", locale);

        try {
            return bundle.getString(key);
        }
        catch (MissingResourceException mre) {
            logger.warn("No message found for key: " + key);

            return def;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org