1 24 package org.riotfamily.common.i18n; 25 26 import java.util.Locale ; 27 28 import org.springframework.context.MessageSourceResolvable; 29 import org.springframework.context.support.DelegatingMessageSource; 30 import org.springframework.util.StringUtils; 31 32 38 public class CodeRevealingMessageSource extends DelegatingMessageSource { 39 40 private boolean revealCodes = true; 41 42 public boolean isRevealCodes() { 43 return this.revealCodes; 44 } 45 46 public void setRevealCodes(boolean revealAllCodes) { 47 this.revealCodes = revealAllCodes; 48 } 49 50 protected String revealCode(String message, String code) { 51 StringBuffer sb = new StringBuffer (); 52 if (message != null) { 53 sb.append(message); 54 } 55 sb.append("<span class=\"messageCode\" title=\"") 56 .append(code).append("\"></span>"); 57 58 return sb.toString(); 59 } 60 61 public String getMessage(String code, Object [] args, String defaultMessage, 62 Locale locale) { 63 64 if (revealCodes) { 65 return revealCode(super.getMessage(code, args, 66 defaultMessage, locale), code); 67 } 68 return super.getMessage(code, args, defaultMessage, locale); 69 } 70 71 public String getMessage(String code, Object [] args, Locale locale) { 72 return getMessage(code, args, null, locale); 73 } 74 75 public String getMessage(MessageSourceResolvable resolvable, Locale locale) { 76 if (revealCodes) { 77 return revealCode(super.getMessage(resolvable, locale), 78 StringUtils.arrayToDelimitedString( 79 resolvable.getCodes(), " | ")); 80 } 81 return super.getMessage(resolvable, locale); 82 } 83 } 84 | Popular Tags |