001: /*
002: * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/i18n/CacheResourceBundle.java,v 1.9 2007/01/15 10:31:13 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.9 $
005: * $Date: 2007/01/15 10:31:13 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding MyVietnam and MyVietnam CoreLib
012: * MUST remain intact in the scripts and source code.
013: *
014: * This library is free software; you can redistribute it and/or
015: * modify it under the terms of the GNU Lesser General Public
016: * License as published by the Free Software Foundation; either
017: * version 2.1 of the License, or (at your option) any later version.
018: *
019: * This library is distributed in the hope that it will be useful,
020: * but WITHOUT ANY WARRANTY; without even the implied warranty of
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
022: * Lesser General Public License for more details.
023: *
024: * You should have received a copy of the GNU Lesser General Public
025: * License along with this library; if not, write to the Free Software
026: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
027: *
028: * Correspondence and Marketing Questions can be sent to:
029: * info at MyVietnam net
030: *
031: * @author: Minh Nguyen
032: */
033: package net.myvietnam.mvncore.i18n;
034:
035: import java.text.MessageFormat;
036: import java.util.*;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: public class CacheResourceBundle {
042:
043: private static Log log = LogFactory
044: .getLog(CacheResourceBundle.class);
045:
046: private String bundleName = null;
047:
048: private Hashtable cacheResourceBundle = new Hashtable();
049:
050: public CacheResourceBundle(String bundleName) {
051: if (bundleName == null) {
052: throw new IllegalArgumentException(
053: "bundleName cannot be null.");
054: }
055: this .bundleName = bundleName;
056: }
057:
058: /**
059: * Get the ResourceBundle from a locale, if locale is null, then get from locale English
060: * @param locale Locale
061: * @return ResourceBundle
062: */
063: public ResourceBundle getResourceBundle(Locale locale) {
064: if (locale == null) {
065: locale = Locale.ENGLISH;
066: }
067: ResourceBundle resourceBundle = (ResourceBundle) cacheResourceBundle
068: .get(locale);
069: if (resourceBundle == null) {
070: try {
071: resourceBundle = ResourceBundle.getBundle(bundleName,
072: locale);
073: } catch (MissingResourceException e) {
074: log.error("Cannot load the ResourceBundle = "
075: + bundleName);
076:
077: log
078: .info("Using EmptyResourceBundle because cannot load ResourceBundle = "
079: + bundleName);
080: resourceBundle = new EmptyResourceBundle();
081: }
082: cacheResourceBundle.put(locale, resourceBundle);
083: }
084: return resourceBundle;
085: }
086:
087: public String getString(Locale locale, String key) {
088: ResourceBundle resourceBundle = getResourceBundle(locale);
089: try {
090: return resourceBundle.getString(key);
091: } catch (Exception ex) {
092: return "[[" + key + "]]";
093: }
094: }
095:
096: public String getString(Locale locale, String key, Object[] args) {
097: ResourceBundle resourceBundle = getResourceBundle(locale);
098: try {
099: String message = resourceBundle.getString(key);
100:
101: MessageFormat formatter = new MessageFormat(message);
102: if (locale != null) {
103: formatter.setLocale(locale);
104: }
105: message = formatter.format(args);
106: return message;
107: } catch (Exception ex) {
108: return "[[[" + key + "]]]";
109: }
110: }
111: }
|