001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.email;
031:
032: import java.text.DateFormat;
033: import java.text.MessageFormat;
034: import java.util.Date;
035: import java.util.Locale;
036: import java.util.HashMap;
037: import java.util.Map;
038: import java.util.MissingResourceException;
039: import java.util.ResourceBundle;
040:
041: import nextapp.echo2.app.ApplicationInstance;
042:
043: /**
044: * A utility class that provides resources for obtaining localized messages.
045: */
046: public class Messages {
047:
048: private static final String BUNDLE_NAME = "echo2example.email.resource.localization.Messages";
049:
050: /**
051: * A map which contains <code>DateFormat</code> objects for various
052: * locales.
053: */
054: private static final Map DATE_FORMAT_MEDIUM_MAP = new HashMap();
055:
056: /**
057: * Formats a date with the specified locale.
058: *
059: * @param date the date to be formatted.
060: * @return a localized String representation of the date
061: */
062: public static final String formatDateTimeMedium(Date date) {
063: Locale locale = ApplicationInstance.getActive().getLocale();
064: DateFormat df = (DateFormat) DATE_FORMAT_MEDIUM_MAP.get(locale);
065: if (df == null) {
066: df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
067: DateFormat.MEDIUM, locale);
068: DATE_FORMAT_MEDIUM_MAP.put(locale, df);
069: }
070: return date == null ? null : df.format(date);
071: }
072:
073: /**
074: * Returns a localized formatted message. This method conveniently wraps
075: * a call to a MessageFormat object.
076: *
077: * @param key the key of the message to be returned
078: * @param arguments an array of arguments to be inserted into the message
079: */
080: public static String getFormattedString(String key,
081: Object[] arguments) {
082: Locale locale = ApplicationInstance.getActive().getLocale();
083: String template = getString(key);
084: MessageFormat messageFormat = new MessageFormat(template);
085: messageFormat.setLocale(locale);
086: return messageFormat
087: .format(arguments, new StringBuffer(), null).toString();
088: }
089:
090: /**
091: * Returns localized text.
092: *
093: * @param key the key of the text to be returned
094: * @return the appropriate localized text (if the key is not defined,
095: * the string "!key!" is returned)
096: */
097: public static String getString(String key) {
098: try {
099: Locale locale = ApplicationInstance.getActive().getLocale();
100: ResourceBundle resource = ResourceBundle.getBundle(
101: BUNDLE_NAME, locale);
102: return resource.getString(key);
103: } catch (MissingResourceException e) {
104: return '!' + key + '!';
105: }
106: }
107:
108: /** Non-instantiable class. */
109: private Messages() {
110: }
111: }
|