KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > util > Localizer


1 package com.thaiopensource.util;
2
3 import java.util.ResourceBundle JavaDoc;
4 import java.text.MessageFormat JavaDoc;
5
6 public class Localizer {
7   private final Class JavaDoc cls;
8   private ResourceBundle JavaDoc bundle;
9
10   public Localizer(Class JavaDoc cls) {
11     this.cls = cls;
12   }
13
14   public String JavaDoc message(String JavaDoc key) {
15     return MessageFormat.format(getBundle().getString(key), new Object JavaDoc[]{});
16   }
17
18   public String JavaDoc message(String JavaDoc key, Object JavaDoc arg) {
19     return MessageFormat.format(getBundle().getString(key),
20                 new Object JavaDoc[]{arg});
21   }
22
23   public String JavaDoc message(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2) {
24     return MessageFormat.format(getBundle().getString(key),
25                 new Object JavaDoc[]{arg1, arg2});
26   }
27
28   public String JavaDoc message(String JavaDoc key, Object JavaDoc[] args) {
29     return MessageFormat.format(getBundle().getString(key), args);
30   }
31
32   private ResourceBundle JavaDoc getBundle() {
33     if (bundle == null){
34       String JavaDoc 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