From dae31fbab8aa26fe3f22c5d3716e31f398cf8f98 Mon Sep 17 00:00:00 2001 From: mguessan Date: Wed, 29 Apr 2009 21:02:06 +0000 Subject: [PATCH] I18N: remove Locale.ROOT not available under Java 1.5 git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@546 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/BundleMessage.java | 63 ++++++++++++++----- .../davmail/exception/DavMailException.java | 2 +- src/java/davmail/ui/tray/DavGatewayTray.java | 23 +------ 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/java/davmail/BundleMessage.java b/src/java/davmail/BundleMessage.java index 1e58fab0..945fef7f 100644 --- a/src/java/davmail/BundleMessage.java +++ b/src/java/davmail/BundleMessage.java @@ -11,6 +11,7 @@ import java.util.ResourceBundle; * Internationalization message. */ public class BundleMessage { + public static final Locale ROOT_LOCALE = new Locale("", ""); protected static final String MESSAGE_BUNDLE_NAME = "davmailmessages"; protected final String key; private final Object[] arguments; @@ -20,7 +21,19 @@ public class BundleMessage { this.arguments = arguments; } - protected ResourceBundle getBundle(Locale locale) { + public String format() { + return format(null); + } + + public String format(Locale locale) { + return BundleMessage.format(locale, key, arguments); + } + + public String formatLog() { + return format(ROOT_LOCALE); + } + + protected static ResourceBundle getBundle(Locale locale) { if (locale == null) { return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME); } else { @@ -28,11 +41,11 @@ public class BundleMessage { } } - public String format() { - return format(null); + public static String format(String key, Object... arguments) { + return format(null, key, arguments); } - public String format(Locale locale) { + public static String format(Locale locale, String key, Object... arguments) { Object[] formattedArguments = null; if (arguments != null) { formattedArguments = new Object[arguments.length]; @@ -41,14 +54,14 @@ public class BundleMessage { formattedArguments[i] = ((BundleMessage) arguments[i]).format(locale); } else if (arguments[i] instanceof BundleMessageList) { StringBuilder buffer = new StringBuilder(); - for (BundleMessage bundleMessage:(BundleMessageList)arguments[i]) { + for (BundleMessage bundleMessage : (BundleMessageList) arguments[i]) { buffer.append(bundleMessage.format(locale)); } formattedArguments[i] = buffer.toString(); } else if (arguments[i] instanceof DavMailException) { - formattedArguments[i] = ((DavMailException)arguments[i]).getMessage(locale); + formattedArguments[i] = ((DavMailException) arguments[i]).getMessage(locale); } else if (arguments[i] instanceof Throwable) { - formattedArguments[i] = ((Throwable)arguments[i]).getMessage(); + formattedArguments[i] = ((Throwable) arguments[i]).getMessage(); if (formattedArguments[i] == null) { formattedArguments[i] = arguments[i].toString(); } @@ -60,17 +73,33 @@ public class BundleMessage { return MessageFormat.format(getBundle(locale).getString(key), formattedArguments); } - public static String format(String key, Object... arguments) { - return MessageFormat.format(ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME).getString(key), arguments); - } - - public static String format(Locale locale, String key, Object... arguments) { - return MessageFormat.format(ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME, locale).getString(key), arguments); - } - public static String formatLog(String key, Object... arguments) { - return MessageFormat.format(ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME, Locale.ROOT).getString(key), arguments); + return format(ROOT_LOCALE, key, arguments); } - public static class BundleMessageList extends ArrayList{} + public static String getExceptionLogMessage(BundleMessage message, Exception e) { + return getExceptionMessage(message, e, ROOT_LOCALE); + } + + public static String getExceptionMessage(BundleMessage message, Exception e) { + return getExceptionMessage(message, e, null); + } + + public static String getExceptionMessage(BundleMessage message, Exception e, Locale locale) { + StringBuilder buffer = new StringBuilder(); + if (message != null) { + buffer.append(message.format(locale)).append(' '); + } + if (e instanceof DavMailException) { + buffer.append(((DavMailException) e).getMessage(locale)); + } else if (e.getMessage() != null) { + buffer.append(e.getMessage()); + } else { + buffer.append(e.toString()); + } + return buffer.toString(); + } + + public static class BundleMessageList extends ArrayList { + } } diff --git a/src/java/davmail/exception/DavMailException.java b/src/java/davmail/exception/DavMailException.java index e416976d..1483017f 100644 --- a/src/java/davmail/exception/DavMailException.java +++ b/src/java/davmail/exception/DavMailException.java @@ -25,7 +25,7 @@ public class DavMailException extends IOException { } public String getLogMessage() { - return message.format(Locale.ROOT); + return message.formatLog(); } public BundleMessage getBundleMessage() { diff --git a/src/java/davmail/ui/tray/DavGatewayTray.java b/src/java/davmail/ui/tray/DavGatewayTray.java index 1db70b01..5f2a8106 100644 --- a/src/java/davmail/ui/tray/DavGatewayTray.java +++ b/src/java/davmail/ui/tray/DavGatewayTray.java @@ -2,7 +2,6 @@ package davmail.ui.tray; import davmail.Settings; import davmail.BundleMessage; -import davmail.exception.DavMailException; import davmail.exchange.NetworkDownException; import org.apache.log4j.Logger; import org.apache.log4j.Priority; @@ -11,7 +10,6 @@ import javax.imageio.ImageIO; import java.awt.*; import java.io.IOException; import java.net.URL; -import java.util.Locale; /** @@ -50,32 +48,17 @@ public class DavGatewayTray { } protected static void displayMessage(BundleMessage message, Priority priority) { - LOGGER.log(priority, message.format(Locale.ROOT)); + LOGGER.log(priority, message.formatLog()); if (davGatewayTray != null) { davGatewayTray.displayMessage(message.format(), priority); } } - protected static String getExceptionMessage(BundleMessage message, Exception e, Locale locale) { - StringBuilder buffer = new StringBuilder(); - if (message != null) { - buffer.append(message.format(locale)).append(' '); - } - if (e instanceof DavMailException) { - buffer.append(((DavMailException)e).getMessage(locale)); - } else if (e.getMessage() != null) { - buffer.append(e.getMessage()); - } else { - buffer.append(e.toString()); - } - return buffer.toString(); - } - protected static void displayMessage(BundleMessage message, Exception e, Priority priority) { - LOGGER.log(priority, getExceptionMessage(message, e, Locale.ROOT), e); + LOGGER.log(priority, BundleMessage.getExceptionLogMessage(message, e), e); if (davGatewayTray != null && (!(e instanceof NetworkDownException) || isActive())) { - davGatewayTray.displayMessage(getExceptionMessage(message, e, null), priority); + davGatewayTray.displayMessage(BundleMessage.getExceptionMessage(message, e), priority); } if (davGatewayTray != null && e instanceof NetworkDownException) { davGatewayTray.inactiveIcon();