# HG changeset patch # User Ingo Weinzierl # Date 1300112197 0 # Node ID 25593857b8f8f216ac8f7faddf520f5eb1f2f2fb # Parent b7a9557957d1b61597aca73c18885871439eface Implemented a singleton instance that provides i18n strings. flys-artifacts/trunk@1461 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r b7a9557957d1 -r 25593857b8f8 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Mar 10 13:31:36 2011 +0000 +++ b/flys-artifacts/ChangeLog Mon Mar 14 14:16:37 2011 +0000 @@ -1,3 +1,13 @@ +2011-03-14 Ingo Weinzierl + + * src/main/java/de/intevation/flys/artifacts/resources/Resources.java: + New. This class retrieves the i18n strings from a ResourceBundle. + + * src/main/resources/messages.properties, + src/main/resources/messages_en.properties, + src/main/resources/messages_de.properties: Resource files for german and + english translation. + 2011-03-10 Ingo Weinzierl * src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Appended diff -r b7a9557957d1 -r 25593857b8f8 flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/resources/Resources.java Mon Mar 14 14:16:37 2011 +0000 @@ -0,0 +1,106 @@ +package de.intevation.flys.artifacts.resources; + +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 Ingo Weinzierl + */ +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 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; + } + + + /** + * This method returns the translated value for key or def if + * key 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 key was not found. + * + * @return the translated message. + */ + public static String getMsg(CallMeta meta, String key, String def) { + if (INSTANCE == null) { + INSTANCE = new Resources(); + } + + Locale[] locales = INSTANCE.getLocales(); + Locale locale = meta.getPreferredLocale(locales); + + return getMsg(locale, key, def); + } + + + /** + * This method returns the translated value for key or def if + * key 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 key 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 : diff -r b7a9557957d1 -r 25593857b8f8 flys-artifacts/src/main/resources/messages.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/resources/messages.properties Mon Mar 14 14:16:37 2011 +0000 @@ -0,0 +1,2 @@ +state.winfo.river = River +state.winfo.gauge = Pegelauswahl diff -r b7a9557957d1 -r 25593857b8f8 flys-artifacts/src/main/resources/messages_de.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/resources/messages_de.properties Mon Mar 14 14:16:37 2011 +0000 @@ -0,0 +1,2 @@ +state.winfo.river = Fluss +state.winfo.gauge = Pegelauswahl diff -r b7a9557957d1 -r 25593857b8f8 flys-artifacts/src/main/resources/messages_en.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/resources/messages_en.properties Mon Mar 14 14:16:37 2011 +0000 @@ -0,0 +1,2 @@ +state.winfo.river = River +state.winfo.gauge = Pegelauswahl