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 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: */
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:
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(
sascha@2166: CallMeta meta,
sascha@2166: String key,
sascha@2166: Object[] args
sascha@2166: ) {
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(
ingo@413: CallMeta meta,
ingo@413: String key,
ingo@413: String def,
ingo@413: 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:
ingo@413: return MessageFormat.format(template, args);
ingo@413: }
ingo@413:
sascha@3170: public static String format(
sascha@3170: CallMeta meta,
sascha@3170: String key,
sascha@3170: String def,
sascha@3170: Object ... args
sascha@3170: ) {
sascha@3170: String template = getMsg(meta, key, (String)null);
sascha@3170:
sascha@3170: if (template == null) {
sascha@3170: return template = def;
sascha@3170: }
sascha@3170:
sascha@3170: return MessageFormat.format(template, args);
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) {
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 :