1 package org.apache.turbine.tool; 2 3 56 57 import java.util.List ; 58 import java.util.Locale ; 59 import java.util.MissingResourceException ; 60 61 import org.apache.fulcrum.TurbineServices; 62 import org.apache.fulcrum.localization.LocalizationService; 63 import org.apache.turbine.RunData; 64 import org.apache.turbine.services.pull.ApplicationTool; 65 import org.apache.turbine.services.yaaficomponent.YaafiComponentService; 66 67 89 public class LocalizationTool implements ApplicationTool 90 { 91 95 private Locale locale; 96 97 100 private String bundleName; 101 102 105 private String keyPrefix; 106 107 110 public LocalizationTool() 111 { 112 } 113 114 130 public String get(String key) 131 { 132 return get(null, key); 133 } 134 135 143 public String get(String prefix, String key) 144 { 145 key = applyPrefix(prefix, key); 147 148 try 149 { 150 return getLocalizationService().getString(getBundleName(), getLocale(), key); 151 } 152 catch (MissingResourceException noKey) 153 { 154 return formatErrorMessage(key, noKey); 155 } 156 } 157 158 163 public Locale getLocale() 164 { 165 return locale; 166 } 167 168 176 public String format(String key, Object arg1) 177 { 178 key = applyPrefix(null, key); 179 return getLocalizationService().format(getBundleName(), getLocale(), key, arg1); 180 } 181 182 191 public String format(String key, Object arg1, Object arg2) 192 { 193 key = applyPrefix(null, key); 194 return getLocalizationService().format(getBundleName(), getLocale(), key, 195 arg1, arg2); 196 } 197 198 207 public String format(String key, Object [] args) 208 { 209 key = applyPrefix(null, key); 210 return getLocalizationService().format(getBundleName(), getLocale(), key, args); 211 } 212 213 236 public String format(String key, List args) 237 { 238 key = applyPrefix(null, key); 239 return getLocalizationService().format(getBundleName(), getLocale(), key, 240 args.toArray()); 241 } 242 243 252 protected String determineBundleName(Object data) 253 { 254 return getLocalizationService().getDefaultBundleName(); 255 } 256 257 262 protected String getBundleName() 263 { 264 return bundleName; 265 } 266 267 276 protected String getPrefix(String override) 277 { 278 return (override == null ? keyPrefix : override); 279 } 280 281 288 private String applyPrefix(String prefix, String key) 289 { 290 prefix = getPrefix(prefix); 291 if (prefix != null) 292 { 293 key = prefix + key; 294 } 295 return key; 296 } 297 298 307 public void setPrefix(String keyPrefix) 308 { 309 this.keyPrefix = keyPrefix; 310 } 311 312 326 protected String formatErrorMessage(String key, MissingResourceException e) 327 { 328 return null; 329 } 330 331 332 334 338 public void init(Object data) 339 { 340 if (data instanceof RunData) 341 { 342 locale = getLocalizationService().getLocale( ((RunData) data).getRequest() ); 346 } 347 bundleName = determineBundleName(data); 348 } 349 350 353 public void refresh() 354 { 355 locale = null; 356 bundleName = null; 357 setPrefix(null); 358 } 359 360 365 protected static LocalizationService getLocalizationService() { 366 try { 367 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService( 368 YaafiComponentService.SERVICE_NAME); 369 return (LocalizationService) yaafi.lookup(LocalizationService.class.getName()); 370 } catch (Exception e) { 371 throw new RuntimeException ("Problem looking up localization service", e); 372 } 373 } 374 } 375 | Popular Tags |