1 16 17 package org.springframework.context.support; 18 19 import java.util.Locale ; 20 21 import org.springframework.context.MessageSource; 22 import org.springframework.context.MessageSourceResolvable; 23 import org.springframework.context.NoSuchMessageException; 24 import org.springframework.context.i18n.LocaleContextHolder; 25 26 37 public class MessageSourceAccessor { 38 39 private final MessageSource messageSource; 40 41 private final Locale defaultLocale; 42 43 49 public MessageSourceAccessor(MessageSource messageSource) { 50 this.messageSource = messageSource; 51 this.defaultLocale = null; 52 } 53 54 59 public MessageSourceAccessor(MessageSource messageSource, Locale defaultLocale) { 60 this.messageSource = messageSource; 61 this.defaultLocale = defaultLocale; 62 } 63 64 72 protected Locale getDefaultLocale() { 73 return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale()); 74 } 75 76 82 public String getMessage(String code, String defaultMessage) { 83 return this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale()); 84 } 85 86 93 public String getMessage(String code, String defaultMessage, Locale locale) { 94 return this.messageSource.getMessage(code, null, defaultMessage, locale); 95 } 96 97 104 public String getMessage(String code, Object [] args, String defaultMessage) { 105 return this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale()); 106 } 107 108 116 public String getMessage(String code, Object [] args, String defaultMessage, Locale locale) { 117 return this.messageSource.getMessage(code, args, defaultMessage, locale); 118 } 119 120 126 public String getMessage(String code) throws NoSuchMessageException { 127 return this.messageSource.getMessage(code, null, getDefaultLocale()); 128 } 129 130 137 public String getMessage(String code, Locale locale) throws NoSuchMessageException { 138 return this.messageSource.getMessage(code, null, locale); 139 } 140 141 148 public String getMessage(String code, Object [] args) throws NoSuchMessageException { 149 return this.messageSource.getMessage(code, args, getDefaultLocale()); 150 } 151 152 160 public String getMessage(String code, Object [] args, Locale locale) throws NoSuchMessageException { 161 return this.messageSource.getMessage(code, args, locale); 162 } 163 164 171 public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException { 172 return this.messageSource.getMessage(resolvable, getDefaultLocale()); 173 } 174 175 183 public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException { 184 return this.messageSource.getMessage(resolvable, locale); 185 } 186 187 } 188 | Popular Tags |