| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
package org.trails.i18n; |
| 5 |
|
|
| 6 |
|
import java.util.Locale; |
| 7 |
|
|
| 8 |
|
import org.springframework.context.NoSuchMessageException; |
| 9 |
|
import org.trails.descriptor.IClassDescriptor; |
| 10 |
|
import org.trails.descriptor.IDescriptor; |
| 11 |
|
import org.trails.descriptor.IPropertyDescriptor; |
| 12 |
|
|
| 13 |
46 |
public class DefaultTrailsResourceBundleMessageSource implements ResourceBundleMessageSource |
| 14 |
|
{ |
| 15 |
|
|
| 16 |
|
private org.springframework.context.support.ResourceBundleMessageSource messageSource; |
| 17 |
1 |
private static final Object[] EMPTY_ARGUMENTS = new Object[]{}; |
| 18 |
|
|
| 19 |
|
public String getMessage(String key, Locale locale) |
| 20 |
|
{ |
| 21 |
|
try |
| 22 |
|
{ |
| 23 |
52 |
return messageSource.getMessage(key, EMPTY_ARGUMENTS, locale); |
| 24 |
22 |
} catch (NoSuchMessageException nsme) |
| 25 |
|
{ |
| 26 |
22 |
if (locale == null) |
| 27 |
|
{ |
| 28 |
0 |
return getMessageWithNullLocale(key); |
| 29 |
|
} else |
| 30 |
|
{ |
| 31 |
22 |
return getMessageWithLocale(key, locale); |
| 32 |
|
} |
| 33 |
|
} |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
public String getMessage(String key, Object[] args, Locale locale) |
| 37 |
|
{ |
| 38 |
|
try |
| 39 |
|
{ |
| 40 |
22 |
return messageSource.getMessage(key, args, locale); |
| 41 |
9 |
} catch (NoSuchMessageException nsme) |
| 42 |
|
{ |
| 43 |
9 |
if (locale == null) |
| 44 |
|
{ |
| 45 |
0 |
return getMessageWithNullLocale(key, args); |
| 46 |
|
} else |
| 47 |
|
{ |
| 48 |
9 |
return getMessageWithLocale(key, args, locale); |
| 49 |
|
} |
| 50 |
|
} |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
public String getMessageWithDefaultValue(String key, Locale locale, |
| 54 |
|
String defaultMessage) |
| 55 |
|
{ |
| 56 |
6 |
String ret = getMessage(key, EMPTY_ARGUMENTS, locale); |
| 57 |
6 |
return (ret == null) ? defaultMessage : ret; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
public String getMessageWithDefaultValue(String key, Object[] args, Locale locale, String defaultMessage) |
| 61 |
|
{ |
| 62 |
10 |
String ret = getMessage(key, args, locale); |
| 63 |
10 |
return (ret == null) ? defaultMessage : ret; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
public String getDisplayName(IDescriptor descriptor, Locale locale, String defaultMessage) |
| 67 |
|
{ |
| 68 |
86 |
if (locale == null) |
| 69 |
65 |
return defaultMessage; |
| 70 |
|
|
| 71 |
|
String fullName; |
| 72 |
|
String shortName; |
| 73 |
21 |
if (descriptor instanceof IPropertyDescriptor) |
| 74 |
|
{ |
| 75 |
9 |
IPropertyDescriptor property = (IPropertyDescriptor) descriptor; |
| 76 |
9 |
fullName = property.getBeanType().getName() + "." + property.getName(); |
| 77 |
9 |
shortName = property.getName(); |
| 78 |
12 |
} else if (descriptor instanceof IClassDescriptor) |
| 79 |
|
{ |
| 80 |
12 |
IClassDescriptor clazz = (IClassDescriptor) descriptor; |
| 81 |
12 |
fullName = clazz.getType().getName(); |
| 82 |
12 |
shortName = clazz.getType().getSimpleName(); |
| 83 |
|
} else |
| 84 |
|
{ |
| 85 |
0 |
return defaultMessage; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
21 |
return selectDisplayName(fullName, shortName, defaultMessage, locale); |
| 89 |
|
} |
| 90 |
|
|
| 91 |
|
public String getPluralDislayName(IClassDescriptor clazz, Locale locale, String defaultMessage) |
| 92 |
|
{ |
| 93 |
14 |
if (locale == null) |
| 94 |
3 |
return defaultMessage; |
| 95 |
|
|
| 96 |
11 |
String fullName = clazz.getType().getName() + "__plural"; |
| 97 |
11 |
String shortName = clazz.getType().getSimpleName() + "__plural"; |
| 98 |
|
|
| 99 |
11 |
return selectDisplayName(fullName, shortName, defaultMessage, locale); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
private String selectDisplayName(String firstKey, String secondKey, String defValue, Locale locale) |
| 107 |
|
{ |
| 108 |
|
String message; |
| 109 |
|
|
| 110 |
32 |
message = getMessage(firstKey, locale); |
| 111 |
32 |
if (message == null) |
| 112 |
|
{ |
| 113 |
14 |
message = getMessage(secondKey, locale); |
| 114 |
14 |
if (message == null) |
| 115 |
|
{ |
| 116 |
5 |
message = defValue; |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
32 |
return message; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
private String getMessageWithLocale(String key, Object[] args, Locale locale) |
| 123 |
|
{ |
| 124 |
|
|
| 125 |
|
|
| 126 |
|
try |
| 127 |
|
{ |
| 128 |
31 |
Locale languageLocale = new Locale(locale.getLanguage()); |
| 129 |
31 |
return messageSource.getMessage(key, args, languageLocale); |
| 130 |
31 |
} catch (NoSuchMessageException nsme2) |
| 131 |
|
{ |
| 132 |
|
|
| 133 |
|
try |
| 134 |
|
{ |
| 135 |
31 |
return messageSource.getMessage(key, args, Locale.ENGLISH); |
| 136 |
31 |
} catch (NoSuchMessageException nsme3) |
| 137 |
|
{ |
| 138 |
31 |
return null; |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
private String getMessageWithNullLocale(String key, Object[] args) |
| 144 |
|
{ |
| 145 |
|
|
| 146 |
|
try |
| 147 |
|
{ |
| 148 |
0 |
return messageSource.getMessage(key, args, Locale.ENGLISH); |
| 149 |
0 |
} catch (NoSuchMessageException nsme2) |
| 150 |
|
{ |
| 151 |
0 |
return null; |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
private String getMessageWithLocale(String key, Locale locale) |
| 157 |
|
{ |
| 158 |
22 |
return getMessageWithLocale(key, EMPTY_ARGUMENTS, locale); |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
private String getMessageWithNullLocale(String key) |
| 162 |
|
{ |
| 163 |
0 |
return getMessageWithNullLocale(key, EMPTY_ARGUMENTS); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
public void setMessageSource(org.springframework.context.support.ResourceBundleMessageSource messageSource) |
| 168 |
|
{ |
| 169 |
45 |
this.messageSource = messageSource; |
| 170 |
45 |
} |
| 171 |
|
|
| 172 |
|
} |