KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > util > Translator


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.util;
6
7 import java.text.MessageFormat JavaDoc;
8
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11
12 import org.apache.commons.logging.Log;
13
14 /**
15  * Utility class for doing i18n translations.
16  *
17  * @author <a HREF="mailto:stevensa@users.sourceforge.net">Andrew Stevens</a>
18  * @created Jan 18, 2002
19  * @version $Revision: 1.11 $
20  */

21 public final class Translator
22 {
23     /**
24      * All methods/fields are static, so prevent creating instances.
25      */

26     private Translator()
27     {
28     }
29
30     /**
31      * Gets the String attribute of the Translator class.
32      *
33      * @param clazz Messages class containing resource key constants, from which the bundle was generated.
34      * @param resourceKey The resource key to look up in the bundle.
35      * @return The localized string
36      */

37     public static java.lang.String JavaDoc getString(Class JavaDoc clazz, String JavaDoc resourceKey)
38     {
39         return getString(clazz, resourceKey, null);
40     }
41
42     /**
43      * Gets the String attribute of the Translator class.
44      *
45      * @param clazz Messages class containing resource key constants, from which the bundle was generated.
46      * @param resourceKey The resource key to look up in the bundle.
47      * @param arguments A string array of the arguments to be substituted for any placeholders ({0}, {1} etc.) in the
48      * resource value string.
49      * @return The localized string
50      */

51     public static java.lang.String JavaDoc getString(Class JavaDoc clazz, String JavaDoc resourceKey, String JavaDoc[] arguments)
52     {
53         return getString(clazz.getName() + "Messages", resourceKey, arguments);
54     }
55
56     /**
57      * Gets the String attribute of the Translator class.
58      *
59      * @param bundleKey Bundle name, without the tailing ".resources.Messages".
60      * @param resourceKey The resource key to look up in the bundle.
61      * @return The localized string
62      */

63     public static java.lang.String JavaDoc getString(String JavaDoc bundleKey, String JavaDoc resourceKey)
64     {
65         return getString(bundleKey + "Messages", resourceKey, null);
66     }
67
68     /**
69      * Gets the String attribute of the Translator class.
70      *
71      * @param bundleKey Full bundle name, including the tailing ".resources.Messages".
72      * @param resourceKey The resource key to look up in the bundle.
73      * @param arguments A string array of the arguments to be substituted for any placeholders ({0}, {1} etc.) in the
74      * resource value string.
75      * @return The localized string
76      */

77     public static java.lang.String JavaDoc getString(String JavaDoc bundleKey, String JavaDoc resourceKey, String JavaDoc[] arguments)
78     {
79         Log log = LogUtil.getLog(Translator.class, "getString");
80
81         if (log.isDebugEnabled()) {
82             log.debug("bundleKey=" + bundleKey);
83             log.debug("resourceKey=" + resourceKey);
84
85             if (arguments == null) {
86                 log.debug("no arguments");
87             }
88             else {
89                 log.debug("#arguments=" + arguments.length);
90                 for (int i = 0; i < arguments.length; i++) {
91                     log.debug("arg #" + i + '=' + arguments[i]);
92                 }
93             }
94         }
95
96         try {
97             ResourceBundle JavaDoc bundle = getBundle(bundleKey);
98             String JavaDoc resource = bundle.getString(resourceKey.toLowerCase());
99
100             log.debug("resource=" + resource);
101
102             String JavaDoc msg = MessageFormat.format(resource, arguments);
103
104             log.debug("return message: " + msg);
105
106             return msg;
107         }
108         catch (MissingResourceException JavaDoc e) {
109             // No i18n for this one, or we risk an infinite recursion...
110
//This message is not very interesting unless you are an
111
//xdoclet developer who is currently working on
112
//translations, in which case you can set the log level for
113
//this class to debug.
114
log.debug("XDoclet MISSING RESOURCE: Can't locate resource '" + resourceKey + "' for bundle '" + bundleKey + "'.");
115             return resourceKey + " arguments: " + arguments;
116         }
117     }
118
119     private static String JavaDoc getPackageNameOfClass(Class JavaDoc clazz)
120     {
121         String JavaDoc class_name = clazz.getName();
122
123         return class_name.substring(0, class_name.lastIndexOf('.'));
124     }
125
126     /**
127      * Gets the Bundle attribute of the Translator class.
128      *
129      * @param bundleKey The key of the Bundle to return
130      * @return The Bundle value
131      * @exception MissingResourceException Describe the exception
132      */

133     private static ResourceBundle JavaDoc getBundle(java.lang.String JavaDoc bundleKey) throws MissingResourceException JavaDoc
134     {
135         Log log = LogUtil.getLog(Translator.class, "getBundle");
136
137         if (log.isDebugEnabled()) {
138             log.debug("bundleKey=" + bundleKey);
139         }
140
141         try {
142             return ResourceBundle.getBundle(bundleKey);
143         }
144         catch (MissingResourceException JavaDoc e) {
145             // only caught in order to have the chance to log it
146
log.debug("bundle not found");
147             throw e;
148         }
149     }
150 }
151
Popular Tags