Source Code Cross Referenced for Resources.java in  » Internationalization-Localization » RBManager » com » ibm » rbm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » RBManager » com.ibm.rbm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *****************************************************************************
003:         * Copyright (C) 2000-2004, International Business Machines Corporation and  *
004:         * others. All Rights Reserved.                                              *
005:         *****************************************************************************
006:         */
007:        package com.ibm.rbm;
008:
009:        import java.io.*;
010:        import java.net.URL;
011:        import java.net.JarURLConnection;
012:        import java.text.MessageFormat;
013:        import java.util.*;
014:        import java.util.zip.ZipEntry;
015:
016:        import com.ibm.rbm.gui.RBManagerGUI;
017:
018:        /**
019:         * A class not to be instantiated. Provides methods for translating items from a resource bundle. To use this class
020:         * make sure you first call initBundle(). Once this is done, calling any of the getTranslation() methods will return
021:         * the appropriate String.
022:         * 
023:         * @author Jared Jackson
024:         * @see com.ibm.rbm.RBManager
025:         */
026:        public class Resources {
027:            private static ResourceBundle resource_bundle;
028:            private static Locale locale;
029:
030:            /**
031:             * Initialize the properties of this resource bundle. This method must be called once before any
032:             * other method can be called (unless that method performs a check that calls this method). This is
033:             * also the method to use in case the resource data on the data store has changed and those changes
034:             * need to be reflected in future use of this class.
035:             */
036:
037:            public static void initBundle() {
038:                try {
039:                    if (locale == null)
040:                        locale = Locale.getDefault();
041:                    resource_bundle = ResourceBundle.getBundle(
042:                            "com/ibm/rbm/resources/RBManager", locale);
043:                } catch (MissingResourceException mre) {
044:                    System.err.println("Missing Resource for default locale, "
045:                            + Locale.getDefault().toString());
046:                    mre.printStackTrace(System.err);
047:                }
048:            }
049:
050:            /**
051:             * Set the locale to be used when making a query on the resource
052:             */
053:
054:            public static void setLocale(Locale locale) {
055:                try {
056:                    Resources.locale = locale;
057:                    Resources.resource_bundle = ResourceBundle.getBundle(
058:                            "com/ibm/rbm/resources/RBManager", locale);
059:                } catch (MissingResourceException mre) {
060:                    System.err.println("Missing Resource for locale, "
061:                            + locale.toString());
062:                    mre.printStackTrace(System.err);
063:                }
064:            }
065:
066:            /**
067:             * Returns the currently set Locales object.
068:             */
069:
070:            public static Locale getLocale() {
071:                if (Resources.locale == null)
072:                    Resources.initBundle();
073:                return Resources.locale;
074:            }
075:
076:            /**
077:             * Returns an array of strings containing the locale encoding (e.g. 'en_US', 'de', etc.)
078:             * of the locales defined in the current resource bundle. These are all of the locales for
079:             * which unique translation of resource bundle items are possible. If a locale encoding is
080:             * used to query on that is not in this array, the base class translation will be returned.
081:             */
082:
083:            public static String[] getAvailableLocales() {
084:                //Locale loc[] = null;
085:                String list[] = new String[0];
086:                Vector locVect = new Vector();
087:                try {
088:                    URL resURL = ClassLoader
089:                            .getSystemResource("com/ibm/rbm/resources/RBManager.properties");
090:                    JarURLConnection resConnection = (JarURLConnection) resURL
091:                            .openConnection();
092:                    Enumeration enumRes = resConnection.getJarFile().entries();
093:                    String baseName = "com/ibm/rbm/resources/RBManager";
094:                    while (enumRes.hasMoreElements()) {
095:                        String entryName = ((ZipEntry) enumRes.nextElement())
096:                                .getName();
097:                        if (entryName.startsWith(baseName)) {
098:                            entryName = entryName.substring(baseName.length(),
099:                                    entryName.lastIndexOf('.'));
100:                            if (entryName.startsWith("_")) {
101:                                entryName = entryName.substring(1);
102:                            } else if (entryName.length() == 0) {
103:                                /* For our resources we consider root as English */
104:                                entryName = "en";
105:                            }
106:                            locVect.add(entryName);
107:                        }
108:                    }
109:
110:                    /*            File locDir = new File("resources");
111:                     list = locDir.list(new FilenameFilter() {
112:                     public boolean accept(File dir, String name) {
113:                     boolean accept = true;
114:                     if (!name.toLowerCase().endsWith(".properties")) accept = false; // Must be property file
115:                     if (!name.startsWith("RBManager")) accept = false;               // Must be a RBManager file
116:                     if (name.equals("RBManager.properties")) accept = false;         // Base class does not count
117:                     return accept;
118:                     }
119:                     });
120:                     */
121:                    int listSize = locVect.size();
122:                    list = new String[listSize];
123:                    for (int i = 0; i < listSize; i++) {
124:                        list[i] = (String) locVect.get(i);
125:                    }
126:                } catch (IOException ioe) {
127:                    System.err.println("Can't get resources");
128:                    ioe.printStackTrace(System.err);
129:                } catch (ClassCastException cce) {
130:                    System.err.println("Can't get resources");
131:                    cce.printStackTrace(System.err);
132:                }
133:                return list;
134:            }
135:
136:            /**
137:             * Looks up a translation in the currently set Locale by the NLS lookup key provided.
138:             * If no resource by that key is found, the key itself is returned.
139:             */
140:
141:            public static String getTranslation(String key) {
142:                if (key == null || resource_bundle == null)
143:                    return "";
144:                try {
145:                    String retStr = resource_bundle.getString(key);
146:                    return retStr;
147:                } catch (Exception e) {
148:                    return key;
149:                }
150:            }
151:
152:            /**
153:             * Given a locale encoding, returns the language portion of that encoding.
154:             *<br>
155:             * For instance 'en', 'en_US', 'en_GB', all return 'en'
156:             */
157:
158:            public static String getLanguage(String encoding) {
159:                if (encoding == null)
160:                    return null;
161:                if (encoding.indexOf("_") < 0)
162:                    return encoding.trim();
163:                return encoding.substring(0, encoding.indexOf("_"));
164:            }
165:
166:            /**
167:             * Given a locale encoding, returns the country portion of that encoding.
168:             * <br>
169:             * For instance 'en_US', 'sp_US', both return 'US'
170:             * <br>
171:             * Returns null if no country is specified (e.g. 'en' or 'de')
172:             */
173:
174:            public static String getCountry(String encoding) {
175:                if (encoding == null)
176:                    return null;
177:                if (encoding.indexOf("_") < 0)
178:                    return null;
179:                String result = encoding.substring(encoding.indexOf("_") + 1,
180:                        encoding.length());
181:                if (result.indexOf("_") < 0)
182:                    return result.trim();
183:                return result.substring(0, encoding.indexOf("_"));
184:            }
185:
186:            /**
187:             * Given a locale encoding, returns the variant portion of that encoding.
188:             * <br>
189:             * For instance 'en_GB_EURO', 'de_DE_EURO', both return 'EURO'
190:             * <br>
191:             * Returns null if no variant is specified (e.g. 'en' or 'en_US')
192:             */
193:
194:            public static String getVariant(String encoding) {
195:                RBManagerGUI.debugMsg(encoding);
196:                if (encoding == null)
197:                    return null;
198:                if (encoding.indexOf("_") < 0)
199:                    return null;
200:                String result = encoding.substring(encoding.indexOf("_") + 1,
201:                        encoding.length());
202:                if (result.indexOf("_") < 0)
203:                    return null;
204:                result = result.substring(result.indexOf("_") + 1, result
205:                        .length());
206:                return result.trim();
207:            }
208:
209:            /**
210:             * Gets a translation given the currently set locale, a lookup key, and a single lookup item replacement.
211:             * For an understanding of the lookup item replacement see getTranslation(String key, String[] lookup).
212:             */
213:
214:            public static String getTranslation(String key, String lookup) {
215:                if (key == null || resource_bundle == null)
216:                    return "";
217:                try {
218:                    Object objects[] = { lookup };
219:                    String retStr = resource_bundle.getString(key);
220:                    retStr = MessageFormat.format(retStr, objects);
221:                    return retStr;
222:                } catch (Exception e) {
223:                    return key;
224:                }
225:            }
226:
227:            /**
228:             * Gets a translation given the currently set locale, a lookup key, and zero or more lookup item replacements.
229:             * Lookup items are contextual translation material stored in the resource according to the format dictated in
230:             * the java.text package. In short, numbered markers are replaced by strings passed in as parameters.
231:             * <p>
232:             * For example, suppose you have the following resource:
233:             * <p>
234:             * myResource = Hello {1}, Isn't this a great {2}?
235:             * <p>
236:             * You want to replace the '{1}' witht the current user name and the '{2}' with the name of the day today.
237:             * You can do this by calling:
238:             * <p>
239:             * String lookups[] = { "Joe", "Friday" };
240:             * <br>
241:             * Resources.getTranslation("myResource", lookups);
242:             * <p>
243:             * The result would be:
244:             * <p>
245:             * 'Hello Joe, Isn't this a great Friday?'
246:             * <p>
247:             * This method (as well as the getTranslation(String key, String lookup) method) is useful for using nested
248:             * lookups from the resource bundle. For instance, the above line could be replaced with:
249:             * <p>
250:             * String lookups[] = { getUserName(), Resources.getTranslation("Friday") };
251:             */
252:
253:            public static String getTranslation(String key, String[] lookup) {
254:                if (key == null || resource_bundle == null)
255:                    return "";
256:                try {
257:                    Object objects[] = new Object[lookup.length];
258:                    for (int i = 0; i < lookup.length; i++)
259:                        objects[i] = lookup[i];
260:                    String retStr = resource_bundle.getString(key);
261:                    retStr = MessageFormat.format(retStr, lookup);
262:                    return retStr;
263:                } catch (Exception e) {
264:                    return key;
265:                }
266:            }
267:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.