KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > i18n > CacheResourceBundle


1 /*
2  * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/i18n/CacheResourceBundle.java,v 1.8 2006/04/15 02:59:19 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
5  * $Date: 2006/04/15 02:59:19 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding MyVietnam and MyVietnam CoreLib
12  * MUST remain intact in the scripts and source code.
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  *
28  * Correspondence and Marketing Questions can be sent to:
29  * info at MyVietnam net
30  *
31  * @author: Minh Nguyen
32  */

33 package net.myvietnam.mvncore.i18n;
34
35 import java.text.MessageFormat JavaDoc;
36 import java.util.*;
37
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 public class CacheResourceBundle {
43
44     private static Log log = LogFactory.getLog(CacheResourceBundle.class);
45     
46     private String JavaDoc bundleName = null;
47
48     private Hashtable cacheResourceBundle = new Hashtable();
49
50     public CacheResourceBundle(String JavaDoc bundleName) {
51         if (bundleName == null) {
52             throw new IllegalArgumentException JavaDoc("bundleName cannot be null.");
53         }
54         this.bundleName = bundleName;
55     }
56
57     /**
58      * Get the ResourceBundle from a locale, if locale is null, then get from locale English
59      * @param locale Locale
60      * @return ResourceBundle
61      */

62     public ResourceBundle getResourceBundle(Locale locale) {
63         if (locale == null) {
64             locale = Locale.ENGLISH;
65         }
66         ResourceBundle resourceBundle = (ResourceBundle)cacheResourceBundle.get(locale);
67         if (resourceBundle == null) {
68             try {
69                 resourceBundle = ResourceBundle.getBundle(bundleName, locale);
70             } catch (MissingResourceException e) {
71                 log.error("Cannot load the ResourceBundle = " + bundleName);
72                 
73                 log.info("Using EmptyResourceBundle because cannot load ResourceBundle = " + bundleName);
74                 resourceBundle = new EmptyResourceBundle();
75             }
76             cacheResourceBundle.put(locale, resourceBundle);
77         }
78         return resourceBundle;
79     }
80
81     public String JavaDoc getString(Locale locale, String JavaDoc key) {
82         ResourceBundle resourceBundle = getResourceBundle(locale);
83         try {
84             return resourceBundle.getString(key);
85         } catch (Exception JavaDoc ex) {
86             return "[[" + key + "]]";
87         }
88     }
89
90     public String JavaDoc getString(Locale locale, String JavaDoc key, Object JavaDoc[] args) {
91         ResourceBundle resourceBundle = getResourceBundle(locale);
92         try {
93             String JavaDoc message = resourceBundle.getString(key);
94
95             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc(message);
96             if (locale != null) {
97                 formatter.setLocale(locale);
98             }
99             message = formatter.format(args);
100             return message;
101         } catch (Exception JavaDoc ex) {
102             return "[[[" + key + "]]]";
103         }
104     }
105 }
106
Popular Tags