KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > Messages


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email;
31
32 import java.text.DateFormat JavaDoc;
33 import java.text.MessageFormat JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.MissingResourceException JavaDoc;
39 import java.util.ResourceBundle JavaDoc;
40
41 import nextapp.echo2.app.ApplicationInstance;
42
43 /**
44  * A utility class that provides resources for obtaining localized messages.
45  */

46 public class Messages {
47                                                
48     private static final String JavaDoc BUNDLE_NAME = "echo2example.email.resource.localization.Messages";
49     
50     /**
51      * A map which contains <code>DateFormat</code> objects for various
52      * locales.
53      */

54     private static final Map JavaDoc DATE_FORMAT_MEDIUM_MAP = new HashMap JavaDoc();
55     
56     /**
57      * Formats a date with the specified locale.
58      *
59      * @param date the date to be formatted.
60      * @return a localized String representation of the date
61      */

62     public static final String JavaDoc formatDateTimeMedium(Date JavaDoc date) {
63         Locale JavaDoc locale = ApplicationInstance.getActive().getLocale();
64         DateFormat JavaDoc df = (DateFormat JavaDoc) DATE_FORMAT_MEDIUM_MAP.get(locale);
65         if (df == null) {
66             df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
67             DATE_FORMAT_MEDIUM_MAP.put(locale, df);
68         }
69         return date == null ? null : df.format(date);
70     }
71     
72     /**
73      * Returns a localized formatted message. This method conveniently wraps
74      * a call to a MessageFormat object.
75      *
76      * @param key the key of the message to be returned
77      * @param arguments an array of arguments to be inserted into the message
78      */

79     public static String JavaDoc getFormattedString(String JavaDoc key, Object JavaDoc[] arguments) {
80         Locale JavaDoc locale = ApplicationInstance.getActive().getLocale();
81         String JavaDoc template = getString(key);
82         MessageFormat JavaDoc messageFormat = new MessageFormat JavaDoc(template);
83         messageFormat.setLocale(locale);
84         return messageFormat.format(arguments, new StringBuffer JavaDoc(), null).toString();
85     }
86     
87     /**
88      * Returns localized text.
89      *
90      * @param key the key of the text to be returned
91      * @return the appropriate localized text (if the key is not defined,
92      * the string "!key!" is returned)
93      */

94     public static String JavaDoc getString(String JavaDoc key) {
95         try {
96             Locale JavaDoc locale = ApplicationInstance.getActive().getLocale();
97             ResourceBundle JavaDoc resource = ResourceBundle.getBundle(BUNDLE_NAME, locale);
98             return resource.getString(key);
99         } catch (MissingResourceException JavaDoc e) {
100             return '!' + key + '!';
101         }
102     }
103
104     /** Non-instantiable class. */
105     private Messages() { }
106 }
107
Popular Tags