BAEL-4209: Difference between e.getMessage() and e.getLocalizedMessage() (#10222)
* - initial commit of sample code * BAEL-4209: Moving the localized exception module into core-java-exceptions-3. * BAEL-4209: Removed the old files for localizing exception messages.
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.exceptions.localization;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocalizedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String messageKey;
|
||||
private final Locale locale;
|
||||
|
||||
public LocalizedException(String messageKey) {
|
||||
this(messageKey, Locale.getDefault());
|
||||
}
|
||||
|
||||
public LocalizedException(String messageKey, Locale locale) {
|
||||
this.messageKey = messageKey;
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a localized message based on the messageKey provided at instantiation.
|
||||
*/
|
||||
public String getMessage() {
|
||||
|
||||
/*
|
||||
* This is a deliberate role reversal of the default implementation of getLocalizedMessage.
|
||||
* some logging frameworks like Log4J 1 & 2 and Logback will use getMessage instead of
|
||||
* getLocalizedMessage when logging Throwables. If we want to use these frameworks in client
|
||||
* applications to log localized messages, then we'll need to override getMessage in a
|
||||
* similar fashion to return the appropriate content. Or, you can call getLocalizedMessage
|
||||
* on your own to create the log content.
|
||||
*/
|
||||
return getLocalizedMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a localized message based on the messageKey provided at instantiation.
|
||||
*/
|
||||
public String getLocalizedMessage() {
|
||||
|
||||
/*
|
||||
* java.util.logging uses getLocalizedMessage when logging Throwables.
|
||||
*/
|
||||
return Messages.getMessageForLocale(messageKey, locale);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.exceptions.localization;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class Messages {
|
||||
|
||||
/**
|
||||
* Retrieves the value for the messageKey from the locale-specific messages.properties, or from
|
||||
* the base messages.properties for unsupported locales.
|
||||
*
|
||||
* @param messageKey The key for the message in the messages.properties ResourceBundle.
|
||||
* @param locale The locale to search the message key.
|
||||
* @return The value defined for the messageKey in the provided locale.
|
||||
*/
|
||||
public static String getMessageForLocale(String messageKey, Locale locale) {
|
||||
|
||||
/*
|
||||
* For more complex implementations, you will want a var-args parameter for MessageFormat
|
||||
* substitutions. Then we can read the value from the bundle and pass the value with the
|
||||
* substitutions to MessageFormat to create the final message value.
|
||||
*/
|
||||
return ResourceBundle.getBundle("messages", locale)
|
||||
.getString(messageKey);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
message.exception = I am an exception.
|
||||
@@ -0,0 +1 @@
|
||||
message.exception = Je suis une exception.
|
||||
Reference in New Issue
Block a user