1 24 package org.riotfamily.common.i18n; 25 26 import java.util.Locale ; 27 28 import org.riotfamily.common.beans.PropertyUtils; 29 import org.riotfamily.common.util.FormatUtils; 30 import org.springframework.context.MessageSource; 31 import org.springframework.context.MessageSourceResolvable; 32 import org.springframework.context.support.DefaultMessageSourceResolvable; 33 import org.springframework.util.Assert; 34 35 public class MessageResolver { 36 37 private static final String EMPTY_MESSAGE = new String (); 38 39 private AdvancedMessageCodesResolver messageCodesResolver; 40 41 private MessageSource messageSource; 42 43 private Locale locale; 44 45 46 public MessageResolver(MessageSource source, 47 AdvancedMessageCodesResolver resolver, Locale locale) { 48 49 Assert.notNull(source, "MessageSource must not be null"); 50 Assert.notNull(resolver, "MessageCodesResolver must not be null"); 51 Assert.notNull(locale, "Locale must not be null"); 52 53 this.messageSource = source; 54 this.messageCodesResolver = resolver; 55 this.locale = locale; 56 } 57 58 public String getObjectError(String objectName, Class clazz, 59 String errorCode, Object [] args, String defaultMessage) { 60 61 String [] codes = messageCodesResolver.resolveMessageCodes(errorCode, objectName); 62 return getMessage(codes, args, defaultMessage); 63 } 64 65 public String getPropertyError( 66 String objectName, Class clazz, String property, String errorCode, 67 Object [] args, String defaultMessage) { 68 69 Class fieldType = PropertyUtils.getPropertyType(clazz, property); 70 String [] codes = messageCodesResolver.resolveMessageCodes(errorCode, objectName, 71 property, fieldType); 72 73 return getMessage(codes, args, defaultMessage); 74 } 75 76 77 public String getPropertyLabel(String objectName, 78 Class clazz, String property) { 79 80 String [] codes = messageCodesResolver.resolveLabel(objectName, clazz, property); 81 String defaultMessage = FormatUtils.propertyToTitleCase(property); 82 return getMessage(codes, null, defaultMessage); 83 } 84 85 public String getClassLabel(String objectName, Class clazz) { 86 String [] codes = messageCodesResolver.resolveLabel(objectName, clazz); 87 String defaultMessage = FormatUtils.camelToTitleCase(objectName); 88 return getMessage(codes, null, defaultMessage); 89 } 90 91 public String getPropertyHint(String objectName, 92 Class clazz, String property) { 93 94 String [] codes = messageCodesResolver.resolveHint(objectName, clazz, property); 95 return getMessage(codes, null, null); 96 } 97 98 public String getMessage(String code) { 99 return getMessage(code, null, null); 100 } 101 102 public String getMessage(String code, Object [] args) { 103 return getMessage(code, args, null); 104 } 105 106 109 public String getMessage(String code, String defaultMessage) { 110 return getMessage(code, null, defaultMessage); 111 } 112 113 public String getMessage(String code, Object [] args, String defaultMessage) { 114 return messageSource.getMessage(code, args, defaultMessage, locale); 115 } 116 117 public String getMessage(MessageSourceResolvable resolvable) { 118 return getMessage(resolvable.getCodes(), resolvable.getArguments(), 119 resolvable.getDefaultMessage()); 120 } 121 122 public String getMessage(String [] codes, Object [] args, 123 String defaultMessage) { 124 125 if (defaultMessage == null) { 126 defaultMessage = EMPTY_MESSAGE; 127 } 128 MessageSourceResolvable resolvable = 129 new DefaultMessageSourceResolvable(codes, args, defaultMessage); 130 131 String message = messageSource.getMessage(resolvable, locale); 132 if (message == EMPTY_MESSAGE) { 133 return null; 134 } 135 return message; 136 } 137 138 public AdvancedMessageCodesResolver getMessageCodesResolver() { 139 return this.messageCodesResolver; 140 } 141 142 145 public Locale getLocale() { 146 return locale; 147 } 148 149 152 public MessageSource getMessageSource() { 153 return messageSource; 154 } 155 156 } 157 | Popular Tags |