1 package org.tigris.scarab.tools.localization; 2 3 4 49 50 51 import org.tigris.scarab.tools.ScarabLocalizationTool; 52 53 75 public class L10NMessage implements Localizable 76 { 77 80 LocalizationKey l10nKey; 81 82 86 private Object [] parameters; 87 88 92 public L10NMessage(LocalizationKey theKey) 93 { 94 l10nKey = theKey; 95 this.parameters = null; 96 } 97 98 105 public L10NMessage(LocalizationKey theKey, Object [] theParameters) 106 { 107 l10nKey = theKey; 108 this.parameters = theParameters; 109 } 110 111 116 public L10NMessage(LocalizationKey theKey, Object p1) 117 { 118 this(theKey, new Object []{p1}); 119 } 120 121 127 public L10NMessage(LocalizationKey theKey, Object p1, Object p2) 128 { 129 this(theKey, new Object []{p1, p2}); 130 } 131 132 139 public L10NMessage(LocalizationKey theKey, Object p1, Object p2, Object p3) 140 { 141 this(theKey, new Object []{p1, p2, p3}); 142 } 143 144 151 public String getMessage() 152 { 153 ScarabLocalizationTool l10n = new ScarabLocalizationTool(); 154 l10n.init(ScarabLocalizationTool.DEFAULT_LOCALE); 155 return getMessage(l10n); 156 } 157 158 163 public String getMessage(final ScarabLocalizationTool l10n) 164 { 165 final int nbParameters = (parameters == null ? 0 : parameters.length); 166 final Object [] formatedParameters = new Object [nbParameters]; 167 for (int index = 0; index < nbParameters; index++) 168 { 169 Object param = parameters[index]; 170 if (param instanceof Localizable) 171 { 172 formatedParameters[index] = 173 ((Localizable) param).getMessage(l10n); 174 } 175 else if (param instanceof Throwable ) 176 { 177 Throwable t = (Throwable ) param; 188 formatedParameters[index] = t.getLocalizedMessage(); 189 } 190 else 191 { 192 formatedParameters[index] = param; 193 } 194 } 195 return l10n.format(l10nKey.toString(), formatedParameters); 196 } 197 198 } 199 | Popular Tags |