ingo@125: package de.intevation.flys.artifacts.resources; ingo@125: 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 Ingo Weinzierl 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@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) { 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@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) { 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 :