New pluggable interface for message lookup.

Patch from mharm at google.com.


git-svn-id: svn://10.0.0.236/trunk@255422 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nboyd%atg.com 2008-12-10 18:41:17 +00:00
parent 898879cb3d
commit 956e850cf5

View File

@ -25,6 +25,7 @@
* Patrick Beard
* Norris Boyd
* Igor Bukanov
* Mike Harm
* Ethan Hugg
* Bob Jervis
* Roger Lawrence
@ -3558,37 +3559,63 @@ public class ScriptRuntime {
Object[] arguments = {arg1, arg2, arg3, arg4};
return getMessage(messageId, arguments);
}
/**
* This is an interface defining a message provider. Create your
* own implementation to override the default error message provider.
*
* @author Mike Harm
*/
public interface MessageProvider {
/**
* Returns a textual message identified by the given messageId,
* parameterized by the given arguments.
*
* @param messageId the identifier of the message
* @param arguments the arguments to fill into the message
*/
String getMessage(String messageId, Object[] arguments);
}
public static MessageProvider messageProvider = new DefaultMessageProvider();
public static String getMessage(String messageId, Object[] arguments)
{
return messageProvider.getMessage(messageId, arguments);
}
/* OPT there's a noticable delay for the first error! Maybe it'd
* make sense to use a ListResourceBundle instead of a properties
* file to avoid (synchronized) text parsing.
*/
public static String getMessage(String messageId, Object[] arguments)
{
final String defaultResource
= "org.mozilla.javascript.resources.Messages";
private static class DefaultMessageProvider implements MessageProvider {
public String getMessage(String messageId, Object[] arguments) {
final String defaultResource
= "org.mozilla.javascript.resources.Messages";
Context cx = Context.getCurrentContext();
Locale locale = cx != null ? cx.getLocale() : Locale.getDefault();
Context cx = Context.getCurrentContext();
Locale locale = cx != null ? cx.getLocale() : Locale.getDefault();
// ResourceBundle does cacheing.
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale);
// ResourceBundle does caching.
ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale);
String formatString;
try {
formatString = rb.getString(messageId);
} catch (java.util.MissingResourceException mre) {
throw new RuntimeException
("no message resource found for message property "+ messageId);
String formatString;
try {
formatString = rb.getString(messageId);
} catch (java.util.MissingResourceException mre) {
throw new RuntimeException
("no message resource found for message property "+ messageId);
}
/*
* It's OK to format the string, even if 'arguments' is null;
* we need to format it anyway, to make double ''s collapse to
* single 's.
*/
MessageFormat formatter = new MessageFormat(formatString);
return formatter.format(arguments);
}
/*
* It's OK to format the string, even if 'arguments' is null;
* we need to format it anyway, to make double ''s collapse to
* single 's.
*/
MessageFormat formatter = new MessageFormat(formatString);
return formatter.format(arguments);
}
public static EcmaError constructError(String error, String message)