1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.context;
  17. import java.util.Locale;
  18. /**
  19. * Interface to be implemented by objects that can resolve messages.
  20. * This enables parameterization and internationalization of messages.
  21. *
  22. * <p>Spring provides two out-of-the-box implementations for production:
  23. * <ul>
  24. * <li><b>ResourceBundleMessageSource</b>, built on standard java.util.ResourceBundle
  25. * <li><b>ReloadableResourceBundleMessageSource</b>, being able to reload message
  26. * definitions without restarting the VM
  27. * </ul>
  28. *
  29. * @author Rod Johnson
  30. * @author Juergen Hoeller
  31. * @see org.springframework.context.support.ResourceBundleMessageSource
  32. * @see org.springframework.context.support.ReloadableResourceBundleMessageSource
  33. */
  34. public interface MessageSource {
  35. /**
  36. * Try to resolve the message. Return default message if no message was found.
  37. * @param code the code to lookup up, such as 'calculator.noRateSet'. Users of
  38. * this class are encouraged to base message names on the relevant fully
  39. * qualified class name, thus avoiding conflict and ensuring maximum clarity.
  40. * @param args array of arguments that will be filled in for params within
  41. * the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
  42. * or null if none.
  43. * @param locale the Locale in which to do the lookup
  44. * @param defaultMessage String to return if the lookup fails
  45. * @return the resolved message if the lookup was successful;
  46. * otherwise the default message passed as a parameter
  47. * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/text/MessageFormat.html">java.text.MessageFormat</a>
  48. */
  49. String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
  50. /**
  51. * Try to resolve the message. Treat as an error if the message can't be found.
  52. * @param code the code to lookup up, such as 'calculator.noRateSet'
  53. * @param args Array of arguments that will be filled in for params within
  54. * the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
  55. * or null if none.
  56. * @param locale the Locale in which to do the lookup
  57. * @return the resolved message
  58. * @throws NoSuchMessageException if the message wasn't found
  59. * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/text/MessageFormat.html">java.text.MessageFormat</a>
  60. */
  61. String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException;
  62. /**
  63. * Try to resolve the message using all the attributes contained within the
  64. * <code>MessageSourceResolvable</code> argument that was passed in.
  65. * <p>NOTE: We must throw a <code>NoSuchMessageException</code> on this method
  66. * since at the time of calling this method we aren't able to determine if the
  67. * <code>defaultMessage</code> property of the resolvable is null or not.
  68. * @param resolvable value object storing attributes required to properly resolve a message
  69. * @param locale the Locale in which to do the lookup
  70. * @return the resolved message
  71. * @throws NoSuchMessageException if the message wasn't found
  72. * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/text/MessageFormat.html">java.text.MessageFormat</a>
  73. */
  74. String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
  75. }