001: /*
002: * $Id: GWLocale.java,v 1.6 2005/11/30 11:28:15 ss150821 Exp $
003: * $Source: /m/portal/ps/srap/src/com/sun/portal/util/GWLocale.java,v $
004: * $Log: GWLocale.java,v $
005: * Revision 1.6 2005/11/30 11:28:15 ss150821
006: * 6356996 - Srap Code base needs to save files in the unix file format and not windows
007: *
008: * Revision 1.5 2005/02/23 11:39:12 ss150821
009: * RFE 6223490 - SRA Should use JDK based logging
010: *
011: * Revision 1.4 2005/02/21 08:03:15 ss150821
012: * 6223490 - JDK Logging move for SRA
013: *
014: * Revision 1.3 2005/02/17 08:27:37 ss150821
015: * RFE 6223490 - Add JDK Logging Support to SRA
016: *
017: * Revision 1.2 2004/08/25 14:25:46 ss150821
018: * Fix for 5069425
019: *
020: * Revision 1.1 2002/06/14 09:04:22 rt130506
021: * SRAP rebranding
022: *
023: * Revision 1.4 2002/06/12 07:56:00 bv131302
024: * more rebranding - filenames
025: *
026: * Revision 1.3 2002/06/11 16:02:12 bv131302
027: * new branded
028: *
029: * Revision 1.2 2002/02/27 13:27:39 mm132998
030: * Bug ID # 4643783 , CRT : 378 , Desc : If connection is denied , the portal must display a customizable error message.
031: *
032: *
033: */
034: /*
035: * @(#)GWLocale.java 1.2 00/04/10
036: *
037: * Copyright (c) 04/10/00 Sun Microsystems, Inc. All Rights Reserved.
038: */
039:
040: package com.sun.portal.util;
041:
042: import java.text.MessageFormat;
043: import java.util.Hashtable;
044: import java.util.MissingResourceException;
045: import java.util.ResourceBundle;
046: import java.util.StringTokenizer;
047:
048: import com.sun.portal.rproxy.configservlet.client.PlatformProfile;
049:
050: public class GWLocale {
051:
052: public static final String RESOURCENAME = "srapGateway";
053:
054: private static ResourceBundle platformRB;
055:
056: private static ResourceBundle userRB;
057:
058: private static Hashtable rbHash = new Hashtable();
059:
060: private static java.util.Locale platformLocale;
061:
062: private static java.util.Locale userLocale;
063:
064: /* Taken from Locale.java - begin */
065:
066: /**
067: * <P>
068: * Gets locale object based on locale string.
069: *
070: * <P>
071: * The expected format of the locale string is of the form:
072: * <UL>
073: * <LI>language_country_variant
074: * <LI>language_country <I>-or- </I>
075: * <LI>language
076: * </UL>
077: *
078: * <P>
079: * If locale string is <CODE>null</CODE>, the locale is determined by the
080: * <CODE>java.util.Locale.getDefault()</CODE> method.
081: *
082: * @param stringformat
083: * string format of locale
084: * @return locale object
085: */
086: public static java.util.Locale getLocale(String stringformat) {
087: if (stringformat == null)
088: return java.util.Locale.getDefault();
089: StringTokenizer tk = new StringTokenizer(stringformat, "_");
090: String lang = "";
091: String country = "";
092: String variant = "";
093: if (tk.hasMoreTokens())
094: lang = tk.nextToken();
095: if (tk.hasMoreTokens())
096: country = tk.nextToken();
097: if (tk.hasMoreTokens())
098: variant = tk.nextToken();
099: return new java.util.Locale(lang, country, variant);
100: }
101:
102: /**
103: * <P>
104: * Gets a resource bundle.
105: *
106: * <P>
107: * Uses <A HREF="#getLocale(java.lang.String)"> <CODE>getLocale()</CODE>
108: * </A> as defined here to resolve string format of locale.
109: *
110: * @param bundle
111: * bundle name
112: * @param stringformat
113: * string format of locale
114: * @return resource bundle
115: */
116: public static ResourceBundle getResourceBundle(String bundle,
117: String stringformat) {
118: return ResourceBundle
119: .getBundle(bundle, getLocale(stringformat));
120: }
121:
122: /**
123: * <P>
124: * Gets resource bundle based on installed system locale.
125: *
126: * <P>
127: * Reads <CODE>gateway.locale</CODE> portal server system property (see <A
128: * HREF="SystemProperties.html">SystemProperties </A>) and returns resource
129: * bundle.
130: *
131: * @param bundle
132: * bundle name
133: * @return resource bundle
134: */
135: public static ResourceBundle getInstallResourceBundle(String bundle) {
136: String loc = null;
137: loc = SystemProperties.get("gateway.locale", "en_US");
138: return ResourceBundle.getBundle(bundle, getLocale(loc));
139: }
140:
141: /* Taken from Locale.java - end */
142:
143: public static void createDefault() {
144: // Open resource bundle for localized messages. Exit if not found.
145: String platformLocaleStr;
146: String userLocaleStr;
147:
148: try {
149: platformLocaleStr = PlatformProfile.getString("locale",
150: "en_US");
151: platformRB = getResourceBundle(RESOURCENAME,
152: platformLocaleStr);
153: platformLocale = getLocale(platformLocaleStr);
154:
155: rbHash.put(platformLocaleStr, platformRB);
156:
157: // Fix for #5069425 -- Sandeep Soni
158: userLocaleStr = SystemProperties.get("gateway.locale",
159: "en_US");
160:
161: userRB = getResourceBundle(RESOURCENAME, userLocaleStr);
162: rbHash.put(userLocaleStr, userRB);
163: userLocale = getLocale(userLocaleStr);
164:
165: } catch (MissingResourceException mre) {
166: System.out.println(platformRB
167: .getString("ResourceFileNotFound"));
168: System.exit(1);
169: }
170: }
171:
172: public static String getPFString(String key) {
173: try {
174: return (platformRB.getString(key));
175: } catch (Exception ex) {
176: return key;
177: }
178: }
179:
180: public static String getPFString(String key, Object[] args) {
181: try {
182: MessageFormat mf = new MessageFormat("");
183: mf.setLocale(platformLocale);
184: mf.applyPattern(getPFString(key));
185: return (mf.format(args, new StringBuffer(), null)
186: .toString());
187: } catch (Exception ex) {
188: return key;
189: }
190: }
191:
192: // Lihue PRD - 8.2
193: public static String getPFString(String key, Object[] args,
194: String locale) {
195: try {
196: MessageFormat mf = new MessageFormat("");
197: mf.setLocale(getLocale(locale));
198: mf.applyPattern(getString(key, locale));
199: return (mf.format(args, new StringBuffer(), null)
200: .toString());
201: } catch (Exception ex) {
202: return key;
203: }
204: }
205:
206: // End of Code : Lihue PRD - 8.2
207:
208: public static String getUserString(String key) {
209: try {
210: return (userRB.getString(key));
211: } catch (Exception ex) {
212: return key;
213: }
214: }
215:
216: public static String getUserString(String key, Object[] args) {
217: try {
218: MessageFormat mf = new MessageFormat("");
219: mf.setLocale(userLocale);
220: mf.applyPattern(getUserString(key));
221: return (mf.format(args, new StringBuffer(), null)
222: .toString());
223: } catch (Exception ex) {
224: return key;
225: }
226: }
227:
228: public static String getString(String key, String localeStr) {
229: if (localeStr == null) {
230: localeStr = "en_US";
231: }
232:
233: ResourceBundle rb;
234:
235: if (rbHash.contains(localeStr)) {
236: rb = (ResourceBundle) (rbHash.get(localeStr));
237: } else {
238: try {
239: rb = getResourceBundle(RESOURCENAME, localeStr);
240: } catch (Exception ex) {
241: return key;
242: }
243:
244: rbHash.put(localeStr, rb);
245: }
246:
247: return (rb.getString(key));
248: }
249: }
|