teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.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: teichmann@5831: import org.dive4elements.artifacts.CallMeta; ingo@125: ingo@125: /** ingo@125: * This class provides methods for i18n. ingo@125: * ingo@125: * @author Ingo Weinzierl ingo@125: */ ingo@125: public class Resources { ingo@125: teichmann@8202: /** The log that is used in this class.*/ teichmann@8202: private static Logger log = 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 key or def if ingo@125: * key 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 key 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 key 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 key. 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 key or def if ingo@125: * key 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 key 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) { teichmann@8202: log.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 :