1 package com.thaiopensource.util; 2 3 import java.util.ResourceBundle ; 4 import java.text.MessageFormat ; 5 6 public class Localizer { 7 private final Class cls; 8 private ResourceBundle bundle; 9 10 public Localizer(Class cls) { 11 this.cls = cls; 12 } 13 14 public String message(String key) { 15 return MessageFormat.format(getBundle().getString(key), new Object []{}); 16 } 17 18 public String message(String key, Object arg) { 19 return MessageFormat.format(getBundle().getString(key), 20 new Object []{arg}); 21 } 22 23 public String message(String key, Object arg1, Object arg2) { 24 return MessageFormat.format(getBundle().getString(key), 25 new Object []{arg1, arg2}); 26 } 27 28 public String message(String key, Object [] args) { 29 return MessageFormat.format(getBundle().getString(key), args); 30 } 31 32 private ResourceBundle getBundle() { 33 if (bundle == null){ 34 String s = cls.getName(); 35 int i = s.lastIndexOf('.'); 36 if (i > 0) 37 s = s.substring(0, i + 1); 38 else 39 s = ""; 40 bundle = ResourceBundle.getBundle(s + "resources.Messages"); 41 } 42 return bundle; 43 } 44 } 45 | Popular Tags |