001 package com.thaiopensource.util;
002
003 import java.util.ResourceBundle;
004 import java.text.MessageFormat;
005
006 public class Localizer {
007 private final Class cls;
008 private ResourceBundle bundle;
009
010 public Localizer(Class cls) {
011 this.cls = cls;
012 }
013
014 public String message(String key) {
015 return MessageFormat.format(getBundle().getString(key), new Object[]{});
016 }
017
018 public String message(String key, Object arg) {
019 return MessageFormat.format(getBundle().getString(key),
020 new Object[]{arg});
021 }
022
023 public String message(String key, Object arg1, Object arg2) {
024 return MessageFormat.format(getBundle().getString(key),
025 new Object[]{arg1, arg2});
026 }
027
028 public String message(String key, Object[] args) {
029 return MessageFormat.format(getBundle().getString(key), args);
030 }
031
032 private ResourceBundle getBundle() {
033 if (bundle == null){
034 String s = cls.getName();
035 int i = s.lastIndexOf('.');
036 if (i > 0)
037 s = s.substring(0, i + 1);
038 else
039 s = "";
040 bundle = ResourceBundle.getBundle(s + "resources.Messages");
041 }
042 return bundle;
043 }
044 }