KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > i18n > Messages


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * Messages.java
67  *
68  * Copyright 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.core.i18n;
71
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
74 import com.jcorporate.expresso.core.misc.ConfigContext;
75 import com.jcorporate.expresso.core.misc.ConfigManager;
76 import com.jcorporate.expresso.core.misc.ConfigurationException;
77 import com.jcorporate.expresso.core.misc.CurrentLogin;
78 import com.jcorporate.expresso.core.misc.StringUtil;
79 import com.jcorporate.expresso.core.security.User;
80 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
81 import com.jcorporate.expresso.services.dbobj.UserPreference;
82 import com.jcorporate.expresso.services.dbobj.UserPreferenceDef;
83 import org.apache.log4j.Logger;
84 import org.apache.log4j.Priority;
85
86 import javax.servlet.ServletException JavaDoc;
87 import javax.servlet.http.HttpServletRequest JavaDoc;
88 import java.util.HashMap JavaDoc;
89 import java.util.ListIterator JavaDoc;
90 import java.util.Locale JavaDoc;
91 import java.util.Map JavaDoc;
92 import java.util.Stack JavaDoc;
93
94
95 /**
96  * Messages.java
97  * The Messages object provides a way to gain access to the appropriate
98  * local-language version of a message, and to provide substitution of
99  * parameters in the message string if necessary.
100  * The appropriate local language is determined by means of the following:
101  * <p/>
102  * Created on January 10, 2001, 8:35 AM
103  *
104  * @author Michael Nash
105  */

106 public class Messages
107         extends java.lang.Object JavaDoc {
108
109     /**
110      * Schema to use by default if none is specified
111      */

112     private static String JavaDoc defaultSchema = "com.jcorporate.expresso.core.ExpressoSchema";
113
114     private static Logger log = Logger.getLogger(Messages.class);
115
116     private static Map JavaDoc messageBundlesBySchema = new HashMap JavaDoc();
117     private static Map JavaDoc messageBundlesByPath = new HashMap JavaDoc();
118
119     public final static String JavaDoc LOCALE_KEY = "expresso.LOCALE";
120
121     /**
122      * Creates new Class
123      */

124     public Messages() {
125     } /* Messages() */
126
127     /**
128      * The getBundleBySchema method fetches the MessageBundle object
129      * for the specified schema name, language and country. It tries
130      * the cache of MessageBundle objects first, and adds the object
131      * to the cache if it must be created
132      *
133      * @param schemaName
134      * @param l locale to use
135      * @return
136      */

137     private static synchronized MessageBundle getBundleBySchema(String JavaDoc schemaName,
138                                                                 Locale JavaDoc l) {
139         if (l == null) {
140             l = Locale.getDefault();
141         }
142
143         String JavaDoc language = l.getLanguage();
144         String JavaDoc country = l.getCountry();
145         FastStringBuffer bundleKey = new FastStringBuffer(schemaName);
146         bundleKey.append("|");
147         bundleKey.append(language);
148         bundleKey.append("|");
149         bundleKey.append(country);
150
151         MessageBundle myBundle = (MessageBundle) messageBundlesBySchema.get(bundleKey.toString());
152
153         if (myBundle == null) {
154             System.out.println("Loading Messagebundle: " + schemaName);
155             myBundle = new MessageBundle();
156             myBundle.setSchema(schemaName);
157             myBundle.setLanguage(language);
158             myBundle.setCountry(country);
159             messageBundlesBySchema.put(bundleKey.toString(), myBundle);
160
161             FastStringBuffer pathKey = new FastStringBuffer(myBundle.getBundlePath());
162             pathKey.append("|");
163             pathKey.append(language);
164             pathKey.append("|");
165             pathKey.append(country);
166             messageBundlesByPath.put(pathKey.toString(), myBundle);
167         }
168
169         return myBundle;
170     } /* getBundleBySchema(String, String, String) */
171
172     /**
173      * Like the getBundleBySchema object, this method retrieves a MessageBundle
174      * object, but in this case for a specified bundle path.
175      *
176      * @param bundlePath
177      * @param language
178      * @param country
179      * @return
180      */

181     private static synchronized MessageBundle getBundleByPath(String JavaDoc bundlePath,
182                                                               String JavaDoc language,
183                                                               String JavaDoc country) {
184         FastStringBuffer bundleKey = new FastStringBuffer(bundlePath);
185         bundleKey.append("|");
186         bundleKey.append(language);
187         bundleKey.append("|");
188         bundleKey.append(country);
189
190         MessageBundle myBundle = (MessageBundle) messageBundlesByPath.get(bundleKey.toString());
191
192         if (myBundle == null) {
193             System.out.println("Loading Messagebundle: " + bundlePath);
194             myBundle = new MessageBundle();
195             myBundle.setBundlePath(bundlePath);
196             myBundle.setLanguage(language);
197             myBundle.setCountry(country);
198             messageBundlesByPath.put(myBundle.getBundlePath() + "|" + language + "|" + country,
199                     myBundle);
200         }
201
202         return myBundle;
203     } /* getBundleByPath(String, String, String) */
204
205     /**
206      * The "full" form of getString must specify a schema, language and
207      * country. If we don't have one of these things, we use the convenience
208      * methods to determine them
209      *
210      * @param schemaClass the schema class
211      * @param l the Locale
212      * @param stringCode the string code
213      * @param args any arguments.
214      * @return translated string, or if not found, the stringCode
215      */

216     public static String JavaDoc getString(String JavaDoc schemaClass, Locale JavaDoc l,
217                                    String JavaDoc stringCode, Object JavaDoc[] args) {
218
219         if (l == null) {
220             l = Locale.getDefault();
221         }
222
223         MessageBundle mb = getBundleBySchema(schemaClass, l);
224
225         return mb.getString(stringCode, args);
226     } /* getString(String, String, String, String, Object[] */
227
228     /**
229      * Return a string out of our message bundle associated with the specified schemaClass,
230      * throwing an exception if it's not found
231      *
232      * @param schemaClass the schema class
233      * @param l the Locale
234      * @param stringCode the string code
235      * @param args any arguments.
236      * @return java.lang.String
237      * @throws IllegalArgumentException if the given string code doesn't exist in the given bundle.
238      */

239     public static String JavaDoc getStringRequired(String JavaDoc schemaClass, Locale JavaDoc l,
240                                            String JavaDoc stringCode, Object JavaDoc[] args) throws IllegalArgumentException JavaDoc {
241
242         if (l == null) {
243             l = Locale.getDefault();
244         }
245
246         MessageBundle mb = getBundleBySchema(schemaClass, l);
247
248         return mb.getStringRequired(stringCode, args);
249     } /* getString(String, String, String, String, Object[] */
250
251     /**
252      * Return a string out of our message bundle associated with the specified schemaClass,
253      * throwing an exception if it's not found
254      *
255      * @param schemaClass the schema class
256      * @param stringCode the string code
257      * @return java.lang.String
258      * @throws IllegalArgumentException if the given string code doesn't exist in the given bundle.
259      */

260     public static String JavaDoc getStringRequired(String JavaDoc schemaClass, String JavaDoc stringCode) {
261         MessageBundle mb = getBundleBySchema(schemaClass, Locale.getDefault());
262         return mb.getStringRequired(stringCode, null);
263     }
264
265     /**
266      * Convenience method that gets a string using the schemaStack, looping
267      * through it until the key is found
268      *
269      * @param schemaStack A Stack of schemas
270      * @param ourLocale The specified Locale Object
271      * @param stringCode The string code to retrieve
272      * @param args the formatting arguments for the String
273      * @return The properly formatted string as read from the messages bundle or the key if not found
274      * @throws IllegalArgumentException if the given string code doesn't exist in any schema in stack.
275      */

276     public static String JavaDoc getStringRequired(Stack JavaDoc schemaStack, Locale JavaDoc ourLocale, String JavaDoc stringCode, Object JavaDoc[] args) throws IllegalArgumentException JavaDoc {
277         String JavaDoc result = null;
278
279         for (ListIterator JavaDoc li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) {
280             String JavaDoc schema = (String JavaDoc) li.previous();
281             result = getStringOrNull(schema, ourLocale, stringCode, args);
282             if (result != null) {
283                 break;
284             }
285         }
286
287         // stack might have been empty, or not included default
288
if (result == null) {
289             // last chance; will throw
290
result = getStringRequired(defaultSchema, ourLocale, stringCode, args);
291         }
292
293         return result;
294     }
295
296     /**
297      * Convenience method that gets a string using the schemaStack, looping
298      * through it until the key is found
299      *
300      * @param schemaStack A Stack of schemas
301      * @param ourLocale The specified Locale Object
302      * @param stringCode The string code to retrieve
303      * @param args the formatting arguments for the String
304      * @return The properly formatted string as read from the messages bundle or the key if not found
305      */

306     public static String JavaDoc getStringUnrequired(Stack JavaDoc schemaStack, Locale JavaDoc ourLocale, String JavaDoc stringCode, Object JavaDoc[] args) throws IllegalArgumentException JavaDoc {
307         String JavaDoc result = null;
308
309         for (ListIterator JavaDoc li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) {
310             String JavaDoc schema = (String JavaDoc) li.previous();
311             result = getStringOrNull(schema, ourLocale, stringCode, args);
312             if (result != null) {
313                 break;
314             }
315         }
316
317         // stack might have been empty, or not included default
318
if (result == null) {
319             // last chance; will throw
320
result = getStringOrNull(defaultSchema, ourLocale, stringCode, args);
321         }
322
323         if (result == null) {
324             result = stringCode;
325         }
326
327         return result;
328     }
329
330     /**
331      * Variation on the above gets a string from a specified message bundle
332      * path
333      *
334      * @param bundlePath
335      * @param language
336      * @param country
337      * @param stringCode
338      * @param args
339      * @return
340      */

341     public static String JavaDoc getStringByPath(String JavaDoc bundlePath, String JavaDoc language,
342                                          String JavaDoc country, String JavaDoc stringCode,
343                                          Object JavaDoc[] args) {
344         MessageBundle mb = getBundleByPath(bundlePath, language, country);
345
346         return mb.getString(stringCode, args);
347     } /* getStringByPath(String, String, String, String, Object[]) */
348
349     /**
350      * Set the default schema to a class name of a Schema object
351      *
352      * @param newDefault
353      */

354     public static void setDefaultSchema(String JavaDoc newDefault) {
355         defaultSchema = newDefault;
356     } /* setDefaultSchema(String) */
357
358     /**
359      * Convenience method to return a specified string from the default
360      * schema, with no arguments
361      *
362      * @param stringCode
363      * @return
364      */

365     public static String JavaDoc getString(String JavaDoc stringCode) {
366         Object JavaDoc[] args = {};
367
368         return getString(defaultSchema, stringCode, args);
369     } /* getString(String) */
370
371     /**
372      * Convenience method that gets a string with arguments
373      * using the default schema (e.g. no specific schema specified)
374      * but with the specified arguments
375      *
376      * @param stringCode
377      * @param args
378      * @return
379      */

380     public static String JavaDoc getString(String JavaDoc stringCode, Object JavaDoc[] args) {
381         return getString(defaultSchema, stringCode, args);
382     } /* getString(String, Object[]) */
383
384     /**
385      * get string translation
386      *
387      * @param schemaClass name of schema
388      * @param stringCode The string code to retrieve
389      * @return translated string, or if not found, the stringCode
390      */

391     public static String JavaDoc getString(String JavaDoc schemaClass, String JavaDoc stringCode) {
392         return getString(schemaClass, stringCode, null);
393     } /* getString(String, String) */
394
395     /**
396      * Convenience method that gets a string using the schemaStack, looping
397      * through stack until the key is found
398      *
399      * @param schemaStack A Stack of schemas
400      * @param ourLocale The specified Locale Object
401      * @param stringCode The string code to retrieve
402      * @param args the formatting arguments for the String
403      * @return The properly formatted string as read from the messages bundle or the key if not found
404      */

405     public static String JavaDoc getString(Stack JavaDoc schemaStack, Locale JavaDoc ourLocale, String JavaDoc stringCode, Object JavaDoc[] args) {
406         String JavaDoc result = null;
407         for (ListIterator JavaDoc li = schemaStack.listIterator(schemaStack.size()); li.hasPrevious();) {
408             String JavaDoc schema = (String JavaDoc) li.previous();
409             result = getStringOrNull(schema, ourLocale, stringCode, args);
410             if (result != null) {
411                 break;
412             }
413         }
414
415         if (result == null) {
416             result = stringCode;
417             // log warning
418
if (log.isEnabledFor(Priority.WARN)) {
419                 String JavaDoc msg = "No such key '" +
420                         stringCode + "' in bundle for schemas: '" + schemaStack.toString() + "'";
421                 if (log.isDebugEnabled()) {
422                     msg += " with language '" + ourLocale.getLanguage() + "', " + "Country '" +
423                             ourLocale.getCountry() + "'";
424                 }
425                 log.warn(msg);
426             }
427         }
428
429         return result;
430     } /* getString(String, String) */
431
432     /**
433      * @return translation string, or null if not found
434      */

435     private static String JavaDoc getStringOrNull(String JavaDoc schema, Locale JavaDoc ourLocale, String JavaDoc stringCode, Object JavaDoc[] args) {
436         if (ourLocale == null) {
437             ourLocale = Locale.getDefault();
438         }
439
440         MessageBundle mb = getBundleBySchema(schema, ourLocale);
441         return mb.getStringOrNull(stringCode, args);
442     }
443
444     /**
445      * Convenience method to use the current default schema
446      *
447      * @param schemaClass
448      * @param req
449      * @param stringCode
450      * @param args
451      * @return translated string, or if not found, the stringCode
452      */

453     public static String JavaDoc getString(String JavaDoc schemaClass, HttpServletRequest JavaDoc req,
454                                    String JavaDoc stringCode, Object JavaDoc[] args)
455             throws ServletException JavaDoc {
456         Locale JavaDoc l = getLocale(req);
457
458         return getString(schemaClass, l, stringCode, args);
459     } /* getString(String, HttpServletRequest, String, Object[]) */
460
461
462     public static String JavaDoc getString(Locale JavaDoc l, String JavaDoc stringCode) {
463         Object JavaDoc[] args = {};
464
465         return getString(defaultSchema, l, stringCode, args);
466     }
467
468     public static String JavaDoc getString(Locale JavaDoc l, String JavaDoc stringCode, Object JavaDoc[] args) {
469         return getString(defaultSchema, l, stringCode, args);
470     }
471
472     /**
473      * getString(schema, locale, stringCode)
474      *
475      * @return translated string, or if not found, the stringCode
476      */

477     public static String JavaDoc getString(String JavaDoc schemaClass, Locale JavaDoc l, String JavaDoc stringCode) {
478         Object JavaDoc[] args = {};
479         return getString(schemaClass, l, stringCode, args);
480     } /* getString(String, String, String) */
481
482     /**
483      * Figure out a Locale by reading the current request.
484      * If there is a currently logged-in user, use that users UserPreference
485      * settings to determine the language.
486      * If there is no logged-in user, use the browser settings if available.
487      * If no user and no browser settings, use the context's default language.
488      * If there is no context default setting, use english.
489      * At the end of calling this method, the session will contain a locale
490      * object for this user, one way or another :-)
491      */

492     public static void establishLocale(HttpServletRequest JavaDoc req)
493             throws ServletException JavaDoc {
494         if (req == null) {
495             throw new IllegalArgumentException JavaDoc("Request may not be null here");
496         }
497         if (GenericSession.getAttribute(req, LOCALE_KEY) != null) {
498             return;
499         }
500
501         Locale JavaDoc newLocale = null;
502
503         /* If there is a currently logged-in user, use that users UserPreference */
504         User myUser = getLoggedInUser(req);
505         String JavaDoc db = GenericSession.getAttributeString(req, "db");
506
507
508         if (myUser != null) {
509             newLocale = setLocaleFromPreferences(myUser, db);
510         }
511         /* If we haven't found a preference (or there was no user) */
512         if (newLocale == null) {
513             newLocale = req.getLocale();
514         }
515
516         GenericSession.setAttribute(req, LOCALE_KEY, newLocale);
517     } /* establishLocale(HttpServletRequest) */
518
519
520     /**
521      * Return a user object if there is a currently logged-in user
522      * defined in the session, else return 'null'
523      */

524     private static User getLoggedInUser(HttpServletRequest JavaDoc req)
525             throws ServletException JavaDoc {
526         String JavaDoc db = GenericSession.getAttributeString(req, "db");
527
528         if (db.equals("")) {
529             db = "default";
530         }
531
532 // String userName = GenericSession.getAttributeString(req, "UserName");
533
CurrentLogin login = (CurrentLogin) GenericSession.getAttribute(req, "CurrentLogin");
534         String JavaDoc userName = "";
535         if (login != null) {
536             userName = login.getUserName();
537         }
538
539         if (userName.equals("")) {
540             userName = User.UNKNOWN_USER;
541         }
542         if (userName.equals(User.UNKNOWN_USER)) {
543             return null;
544         }
545         try {
546             User myUser = new User();
547             myUser.setDataContext(db);
548
549             //Clyde - Fixed bug
550
//before - myUser.setField("LoginName", userName);
551
myUser.setLoginName(userName);
552             myUser.find();
553
554             //Clyde - Fixed bug
555
return myUser;
556         } catch (DBException de) {
557             return null;
558         }
559     } /* getLoggedInUser */
560
561
562     /**
563      * Return the local the user specified in his/her UserPreferences,
564      * or the default if one is set. If none, or if "browser" is set,
565      * return null
566      */

567     private static Locale JavaDoc setLocaleFromPreferences(User myUser, String JavaDoc dbcontext)
568             throws ServletException JavaDoc {
569         String JavaDoc language = ("");
570         String JavaDoc country = ("");
571
572         try {
573
574             /* try the preferences for this user */
575             UserPreference up = new UserPreference();
576             up.setDataContext(dbcontext);
577
578             //Clyde - Fixed the following bug
579
//before - up.setField("UserName", myUser.getUserName());
580
up.setField("ExpUid", myUser.getUid());
581             up.setField("ClassName",
582                     "com.jcorporate.expresso.core.servlet.CheckLogin");
583             up.setField("PrefCode", "language");
584
585             if (up.find()) {
586                 language = up.getField("PrefValue");
587
588                 if (log.isDebugEnabled()) {
589                     log.debug("Language preference for user '" +
590                             myUser.getDisplayName() + "' was '" + language +
591                             "'");
592                 }
593                 if (language.equals("Browser")) {
594                     return null;
595                 }
596
597                 up.clear();
598
599                 //Clyde - Fixed the following bug
600
//before - up.setField("UserName", myUser.getField("LoginName"));
601
up.setField("ExpUid", myUser.getUid());
602                 up.setField("ClassName",
603                         "com.jcorporate.expresso.core.servlet.CheckLogin");
604                 up.setField("PrefCode", "country");
605
606                 if (up.find()) {
607                     country = up.getField("PrefValue");
608
609                     if (log.isDebugEnabled()) {
610                         log.debug("Country preference for user '" +
611                                 myUser.getDisplayName() + "' was '" + country +
612                                 "'");
613                     }
614                 } else {
615                     if (log.isDebugEnabled()) {
616                         log.debug("No country preference found for '" +
617                                 myUser.getDisplayName() + "'");
618                     }
619                 }
620
621                 return new Locale JavaDoc(language, country);
622             }
623
624             UserPreferenceDef upd = new UserPreferenceDef();
625             upd.setDataContext(dbcontext);
626             upd.setField("ClassName",
627                     "com.jcorporate.expresso.core.servlet.CheckLogin");
628             upd.setField("PrefCode", "language");
629
630             if (!upd.find()) {
631                 return null;
632             }
633             if (upd.getField("DefaultVal").equals("Browser")) {
634                 return null;
635             } else {
636                 language = upd.getField("DefaultVal");
637                 upd.clear();
638                 upd.setField("ClassName",
639                         "com.jcorporate.expresso.core.servlet.CheckLogin");
640                 upd.setField("PrefCode", "language");
641
642                 if (upd.find()) {
643                     country = upd.getField("DefaultVal");
644                 }
645             }
646         } catch (DBException de) {
647             log.error(de);
648             throw new ServletException JavaDoc("Unable to retrive language preferences");
649         }
650
651         return new Locale JavaDoc(language, country);
652     } /* establishLocale */
653
654
655     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc req)
656             throws ServletException JavaDoc {
657         if (req == null) {
658             throw new IllegalArgumentException JavaDoc("Request may not be null here");
659         }
660
661         establishLocale(req);
662
663         Object JavaDoc o = GenericSession.getAttribute(req, LOCALE_KEY);
664
665         if (o != null) {
666             return (Locale JavaDoc) o;
667         } else {
668             return Locale.getDefault();
669         }
670     } /* getLocale(HttpServletRequest) */
671
672
673     /**
674      * If we have no information about what language we should be using, we
675      * use the defaults out of the properties file if present. If there are no
676      * defaults in the property file, we use english
677      *
678      * @param schemaClass
679      * @param stringCode
680      * @param args
681      * @return translated string, or if not found, the stringCode
682      */

683     public static String JavaDoc getString(String JavaDoc schemaClass, String JavaDoc stringCode,
684                                    Object JavaDoc[] args) {
685         Locale JavaDoc theLocale = getDefaultLocale();
686
687         return getString(schemaClass, theLocale,
688                 stringCode, args);
689     }
690
691     /**
692      * get default locale (much better to get it from user's request!!)
693      *
694      * @return locale based on 'default' config context
695      * @see #getLocale(javax.servlet.http.HttpServletRequest)
696      */

697     public static Locale JavaDoc getDefaultLocale() {
698         String JavaDoc language = "en";
699         String JavaDoc country = "US";
700
701         try {
702             ConfigContext myContext = ConfigManager.getContext("default");
703             language = StringUtil.notNull(myContext.getLanguage());
704             country = StringUtil.notNull(myContext.getCountry());
705         } catch (ConfigurationException ce) {
706             log.error(ce);
707         }
708
709         Locale JavaDoc theLocale = new Locale JavaDoc(language, country);
710         return theLocale;
711     }
712
713     /**
714      * use locale based on user ID, which is set as user preference;
715      * use with discrimination--it may be better to get locale from request
716      *
717      * @param uid
718      * @param dbName
719      * @param schemaClass
720      * @param stringCode
721      * @param args
722      * @return translated string, or if not found, the stringCode
723      * @see #getString(java.util.Locale, String)
724      */

725     public static String JavaDoc getStringForUser(int uid, String JavaDoc dbName,
726                                           String JavaDoc schemaClass,
727                                           String JavaDoc stringCode, Object JavaDoc[] args) {
728         if (log.isDebugEnabled()) {
729             log.debug("Getting string for user '" + uid + "', db '" + dbName +
730                     "', key '" + stringCode + "'");
731         }
732
733         StringUtil.assertNotBlank(schemaClass,
734                 "Schema class name may not be blank here");
735         StringUtil.assertNotBlank(stringCode,
736                 "String code may not be blank here");
737         StringUtil.assertNotBlank(dbName,
738                 "DB name/context must not be blank here");
739
740         String JavaDoc language = "";
741         String JavaDoc country = "";
742
743         /* try the preferences for this user */
744         try {
745             UserPreference up = new UserPreference();
746             up.setDataContext(dbName);
747             up.setField("ExpUid", uid);
748             up.setField("PrefCode", "language");
749
750             if (up.find()) {
751                 language = up.getField("PrefValue");
752                 up.setField("ExpUid", uid);
753                 up.setField("PrefCode", "country");
754
755                 if (up.find()) {
756                     country = up.getField("PrefValue");
757                 }
758             } else {
759
760                 /* If we have no specific settings for the user, then use the */
761
762                 /* defaults out of the property file */
763                 try {
764                     ConfigContext myContext = ConfigManager.getContext(dbName);
765                     language = StringUtil.notNull(myContext.getLanguage());
766                     country = StringUtil.notNull(myContext.getCountry());
767                 } catch (ConfigurationException ce) {
768                     log.error(ce);
769                 }
770             }
771         } catch (DBException de) {
772             log.error(de);
773             language = "";
774         }
775         /* If we *still* have no language, then use english */
776         if (language.equals("")) {
777             language = "en";
778             country = "US";
779         }
780
781         return getString(schemaClass, new Locale JavaDoc(language, country),
782                 stringCode, args);
783     } /* getStringForUser(String, String, String, String, Object[]) */
784
785     /**
786      * @param req
787      * @param stringCode
788      * @return translated string, or if not found, the stringCode
789      */

790     public static String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode)
791             throws ServletException JavaDoc {
792         Object JavaDoc[] args = {};
793
794         return getString(defaultSchema, req, stringCode, args);
795     } /* getString(HttpServletRequest, String) */
796
797
798     /**
799      * Return a string out of our message bundle
800      *
801      * @param req
802      * @param stringCode
803      * @param firstReplace
804      * @return
805      */

806     public static String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode,
807                                    String JavaDoc firstReplace)
808             throws ServletException JavaDoc {
809         Object JavaDoc[] args = {firstReplace};
810
811         return getString(defaultSchema, req, stringCode, args);
812     } /* getString(HttpServletRequest, String, String) */
813
814
815     /**
816      * Return a string out of our message bundle
817      *
818      * @param req
819      * @param stringCode
820      * @param firstReplace
821      * @param secondReplace
822      * @return
823      */

824     public String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode,
825                             String JavaDoc firstReplace, String JavaDoc secondReplace)
826             throws ServletException JavaDoc {
827         Object JavaDoc[] args = {firstReplace, secondReplace};
828
829         return getString(defaultSchema, req, stringCode, args);
830     } /* getString(HttpServletRequest, String, String, String) */
831
832
833     /**
834      * Convenience method to return a string out of our message bundle, using the thres
835      * string arguments as the replacement arguments
836      *
837      * @param req
838      * @param stringCode
839      * @param firstReplace
840      * @param secondReplace
841      * @param thirdReplace
842      * @return
843      */

844     public String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc stringCode,
845                             String JavaDoc firstReplace, String JavaDoc secondReplace,
846                             String JavaDoc thirdReplace)
847             throws ServletException JavaDoc {
848         Object JavaDoc[] args = {firstReplace, secondReplace, thirdReplace};
849
850         return getString(defaultSchema, req, stringCode, args);
851     } /* getString(HttpServletRequest, String, String, String, String) */
852
853
854 } /* Messages */
855
Popular Tags