• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *****************************************************************************
3  * Copyright (C) 2000-2004, International Business Machines Corporation and  *
4  * others. All Rights Reserved.                                              *
5  *****************************************************************************
6  */
7 package com.ibm.rbm;
8 
9 
10 import java.io.*;
11 import java.net.URL;
12 import java.net.JarURLConnection;
13 import java.text.MessageFormat;
14 import java.util.*;
15 import java.util.zip.ZipEntry;
16 
17 import com.ibm.rbm.gui.RBManagerGUI;
18 
19 /**
20  * A class not to be instantiated. Provides methods for translating items from a resource bundle. To use this class
21  * make sure you first call initBundle(). Once this is done, calling any of the getTranslation() methods will return
22  * the appropriate String.
23  *
24  * @author Jared Jackson
25  * @see com.ibm.rbm.RBManager
26  */
27 public class Resources {
28     private static ResourceBundle resource_bundle;
29     private static Locale locale;
30 
31     /**
32      * Initialize the properties of this resource bundle. This method must be called once before any
33      * other method can be called (unless that method performs a check that calls this method). This is
34      * also the method to use in case the resource data on the data store has changed and those changes
35      * need to be reflected in future use of this class.
36      */
37 
initBundle()38     public static void initBundle() {
39         try {
40             if (locale == null) locale = Locale.getDefault();
41             resource_bundle = ResourceBundle.getBundle("com/ibm/rbm/resources/RBManager", locale);
42         } catch(MissingResourceException mre) {
43             System.err.println("Missing Resource for default locale, " + Locale.getDefault().toString());
44             mre.printStackTrace(System.err);
45         }
46     }
47 
48     /**
49      * Set the locale to be used when making a query on the resource
50      */
51 
setLocale(Locale locale)52     public static void setLocale(Locale locale) {
53         try {
54             Resources.locale = locale;
55             Resources.resource_bundle = ResourceBundle.getBundle("com/ibm/rbm/resources/RBManager", locale);
56         } catch (MissingResourceException mre) {
57             System.err.println("Missing Resource for locale, " + locale.toString());
58             mre.printStackTrace(System.err);
59         }
60     }
61 
62     /**
63      * Returns the currently set Locales object.
64      */
65 
getLocale()66     public static Locale getLocale() {
67         if (Resources.locale == null) Resources.initBundle();
68         return Resources.locale;
69     }
70 
71     /**
72      * Returns an array of strings containing the locale encoding (e.g. 'en_US', 'de', etc.)
73      * of the locales defined in the current resource bundle. These are all of the locales for
74      * which unique translation of resource bundle items are possible. If a locale encoding is
75      * used to query on that is not in this array, the base class translation will be returned.
76      */
77 
getAvailableLocales()78     public static String[] getAvailableLocales() {
79         //Locale loc[] = null;
80         String list[] = new String[0];
81         Vector locVect = new Vector();
82         try {
83             URL resURL = ClassLoader.getSystemResource("com/ibm/rbm/resources/RBManager.properties");
84             JarURLConnection resConnection = (JarURLConnection)resURL.openConnection();
85             Enumeration enumRes = resConnection.getJarFile().entries();
86             String baseName = "com/ibm/rbm/resources/RBManager";
87             while (enumRes.hasMoreElements()) {
88                 String entryName = ((ZipEntry)enumRes.nextElement()).getName();
89                 if (entryName.startsWith(baseName)) {
90                     entryName = entryName.substring(baseName.length(), entryName.lastIndexOf('.'));
91                     if (entryName.startsWith("_")) {
92                         entryName = entryName.substring(1);
93                     }
94                     else if (entryName.length() == 0) {
95                         /* For our resources we consider root as English */
96                         entryName = "en";
97                     }
98                     locVect.add(entryName);
99                 }
100             }
101 
102 /*            File locDir = new File("resources");
103             list = locDir.list(new FilenameFilter() {
104                 public boolean accept(File dir, String name) {
105                     boolean accept = true;
106                     if (!name.toLowerCase().endsWith(".properties")) accept = false; // Must be property file
107                     if (!name.startsWith("RBManager")) accept = false;               // Must be a RBManager file
108                     if (name.equals("RBManager.properties")) accept = false;         // Base class does not count
109                     return accept;
110                 }
111             });
112 */
113             int listSize = locVect.size();
114             list = new String[listSize];
115             for (int i=0; i < listSize; i++) {
116                 list[i] = (String)locVect.get(i);
117             }
118         }
119         catch (IOException ioe) {
120             System.err.println("Can't get resources");
121             ioe.printStackTrace(System.err);
122         }
123         catch (ClassCastException cce) {
124             System.err.println("Can't get resources");
125             cce.printStackTrace(System.err);
126         }
127         return list;
128     }
129 
130     /**
131      * Looks up a translation in the currently set Locale by the NLS lookup key provided.
132      * If no resource by that key is found, the key itself is returned.
133      */
134 
getTranslation(String key)135     public static String getTranslation(String key) {
136         if (key == null || resource_bundle == null) return "";
137         try {
138             String retStr = resource_bundle.getString(key);
139             return retStr;
140         } catch (Exception e) {
141             return key;
142         }
143     }
144 
145     /**
146      * Given a locale encoding, returns the language portion of that encoding.
147      *<br>
148      * For instance 'en', 'en_US', 'en_GB', all return 'en'
149      */
150 
getLanguage(String encoding)151     public static String getLanguage(String encoding) {
152         if (encoding == null) return null;
153         if (encoding.indexOf("_") < 0) return encoding.trim();
154         return encoding.substring(0, encoding.indexOf("_"));
155     }
156 
157     /**
158      * Given a locale encoding, returns the country portion of that encoding.
159      * <br>
160      * For instance 'en_US', 'sp_US', both return 'US'
161      * <br>
162      * Returns null if no country is specified (e.g. 'en' or 'de')
163      */
164 
getCountry(String encoding)165     public static String getCountry(String encoding) {
166         if (encoding == null) return null;
167         if (encoding.indexOf("_") < 0) return null;
168         String result = encoding.substring(encoding.indexOf("_")+1, encoding.length());
169         if (result.indexOf("_") < 0) return result.trim();
170         return result.substring(0, encoding.indexOf("_"));
171     }
172 
173     /**
174      * Given a locale encoding, returns the variant portion of that encoding.
175      * <br>
176      * For instance 'en_GB_EURO', 'de_DE_EURO', both return 'EURO'
177      * <br>
178      * Returns null if no variant is specified (e.g. 'en' or 'en_US')
179      */
180 
getVariant(String encoding)181     public static String getVariant(String encoding) {
182         RBManagerGUI.debugMsg(encoding);
183         if (encoding == null) return null;
184         if (encoding.indexOf("_") < 0) return null;
185         String result = encoding.substring(encoding.indexOf("_")+1, encoding.length());
186         if (result.indexOf("_") < 0) return null;
187         result = result.substring(result.indexOf("_")+1, result.length());
188         return result.trim();
189     }
190 
191     /**
192      * Gets a translation given the currently set locale, a lookup key, and a single lookup item replacement.
193      * For an understanding of the lookup item replacement see getTranslation(String key, String[] lookup).
194      */
195 
getTranslation(String key, String lookup)196     public static String getTranslation(String key, String lookup) {
197         if (key == null || resource_bundle == null) return "";
198         try {
199             Object objects[] = {lookup};
200             String retStr = resource_bundle.getString(key);
201             retStr = MessageFormat.format(retStr, objects);
202             return retStr;
203         } catch (Exception e) {
204             return key;
205         }
206     }
207 
208     /**
209      * Gets a translation given the currently set locale, a lookup key, and zero or more lookup item replacements.
210      * Lookup items are contextual translation material stored in the resource according to the format dictated in
211      * the java.text package. In short, numbered markers are replaced by strings passed in as parameters.
212      * <p>
213      * For example, suppose you have the following resource:
214      * <p>
215      * myResource = Hello {1}, Isn't this a great {2}?
216      * <p>
217      * You want to replace the '{1}' witht the current user name and the '{2}' with the name of the day today.
218      * You can do this by calling:
219      * <p>
220      * String lookups[] = { "Joe", "Friday" };
221      * <br>
222      * Resources.getTranslation("myResource", lookups);
223      * <p>
224      * The result would be:
225      * <p>
226      * 'Hello Joe, Isn't this a great Friday?'
227      * <p>
228      * This method (as well as the getTranslation(String key, String lookup) method) is useful for using nested
229      * lookups from the resource bundle. For instance, the above line could be replaced with:
230      * <p>
231      * String lookups[] = { getUserName(), Resources.getTranslation("Friday") };
232      */
233 
getTranslation(String key, String[] lookup)234     public static String getTranslation(String key, String[] lookup) {
235         if (key == null || resource_bundle == null) return "";
236         try {
237             Object objects[] = new Object[lookup.length];
238             for (int i=0; i < lookup.length; i++) objects[i] = lookup[i];
239             String retStr = resource_bundle.getString(key);
240             retStr = MessageFormat.format(retStr, lookup);
241             return retStr;
242         } catch (Exception e) {
243             return key;
244         }
245     }
246 }