001: /*--
002: $Id: Localizer.java,v 1.2 2004/08/20 05:59:57 wolfpaulus Exp $
003:
004: Copyright (C) 2003-2007 Wolf Paulus.
005: All rights reserved.
006:
007: Redistribution and use in source and binary forms, with or without
008: modification, are permitted provided that the following conditions
009: are met:
010:
011: 1. Redistributions of source code must retain the above copyright
012: notice, this list of conditions, and the following disclaimer.
013:
014: 2. Redistributions in binary form must reproduce the above copyright
015: notice, this list of conditions, and the disclaimer that follows
016: these conditions in the documentation and/or other materials provided
017: with the distribution.
018:
019: 3. The end-user documentation included with the redistribution,
020: if any, must include the following acknowledgment:
021: "This product includes software developed by the
022: SWIXML Project (http://www.swixml.org/)."
023: Alternately, this acknowledgment may appear in the software itself,
024: if and wherever such third-party acknowledgments normally appear.
025:
026: 4. The name "Swixml" must not be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact <info_AT_swixml_DOT_org>
029:
030: 5. Products derived from this software may not be called "Swixml",
031: nor may "Swixml" appear in their name, without prior written
032: permission from the Swixml Project Management.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: DISCLAIMED. IN NO EVENT SHALL THE SWIXML PROJECT OR ITS
038: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: SUCH DAMAGE.
046: ====================================================================
047:
048: This software consists of voluntary contributions made by many
049: individuals on behalf of the Swixml Project and was originally
050: created by Wolf Paulus <wolf_AT_swixml_DOT_org>. For more information
051: on the Swixml Project, please see <http://www.swixml.org/>.
052: */
053:
054: package org.swixml;
055:
056: import java.util.Locale;
057: import java.util.MissingResourceException;
058: import java.util.ResourceBundle;
059:
060: /**
061: * The <code>Localizer</code> class provides consumers with a simple localization tools: <code>getString(key)</code>.
062: * Locale and ResourceBundle need to be set to use it.
063: * Since some setters accept comma separated lists of Strings, e.g. a TabbedPane's setTitles methods, the Localizer will
064: * try to split a given key by <i>commas</i> if the key doesn't resolve, i.e. a <code>MissingResourceException</code> is
065: * thrown. <br />
066: * For example, if the resource bundle contains strings for the following single keys: <br />
067: * a = Alpha <br />
068: * b = Bravo <br />
069: * c = Charlie<br />
070: * then calling getString("a,b,c") will result in a String containing the comma sepearted values, like
071: * "Alpha,Brave,Charlie" <br />
072: * Look at the provided testcase for more details.
073: *
074: * @author <a href="mailto:wolf@paulus.com">Wolf Paulus</a>
075: * @version $Revision: 1.2 $
076: * @see XTabbedPane#setTitles(String)
077: */
078: public class Localizer {
079: private static String SEPARATOR = ",";
080: private Locale locale = Locale.getDefault();
081: private String bundleName;
082: private ResourceBundle bundle;
083: private ClassLoader cl = Localizer.class.getClassLoader();
084:
085: /**
086: * Sets the regular expression used to split a key, that could not be found in the resource bundle.
087: *
088: * @param regExp <code>String</code>
089: * @see String#split
090: * @see #getString
091: */
092: public static void setSeparator(String regExp) {
093: Localizer.SEPARATOR = regExp;
094: }
095:
096: /**
097: * Returns the localized String baseed on the given key.
098: * If the key cannot be found, the key is returned insstead.
099: *
100: * @param key <code>String</code>
101: * @return <code>String</code> - localized String , or key , if no lacalization is found.
102: */
103: public String getString(final String key) {
104: if (!isUsable())
105: return key;
106: String s = "";
107: try {
108: s = bundle.getString(key);
109: } catch (MissingResourceException e) {
110: String[] keys = key.split(Localizer.SEPARATOR);
111: if (2 <= keys.length) {
112: for (int i = 0; i < keys.length; i++) {
113: s += (i == 0) ? getString(keys[i]) : ","
114: + getString(keys[i]);
115: }
116: } else
117: s = key;
118: } catch (Exception e) {
119: s = key; // key not found, return key
120: }
121: return s;
122: }
123:
124: /**
125: * Sets this Localizer's locale.
126: *
127: * @param locale <code>Locale</code>
128: */
129: public void setLocale(Locale locale) {
130: if (locale == null) {
131: this .locale = null;
132: this .bundle = null;
133: this .bundleName = null;
134: } else if (this .locale != locale) {
135: this .locale = locale;
136: setResourceBundle(bundleName);
137: }
138: }
139:
140: /**
141: * Sets this Localizer's ResourceBundle.
142: *
143: * @param bundleName <code>String</code>ResourceBundle file / class name
144: * @throws java.util.MissingResourceException
145: * - if no resource bundle for the specified base name can be found
146: */
147: public void setResourceBundle(String bundleName)
148: throws MissingResourceException {
149: this .bundleName = bundleName;
150: if (locale == null) {
151: locale = Locale.getDefault();
152: }
153: if (bundleName != null) {
154: bundle = ResourceBundle.getBundle(bundleName, locale, cl);
155: } else {
156: bundle = null;
157: }
158: }
159:
160: /**
161: * Informs about the usablility of this Localizer.
162: *
163: * @return <code>boolean</code> - true if Localizer is setup with Locale and ResourceBundle.
164: */
165: public boolean isUsable() {
166: return (locale != null && bundle != null);
167: }
168:
169: /**
170: * @return <code>ClassLoader</code> returns the classloader attribute, which has probably been set by the SwingEngine
171: */
172: public ClassLoader getClassLoader() {
173: return cl;
174: }
175:
176: /**
177: * Sets the ClassLoader attribute.
178: * The Localizer does not use the provided classloader directly but return it when asked for.
179: *
180: * @param cl <code>ClassLoader</code> - custom classloader
181: */
182: void setClassLoader(ClassLoader cl) {
183: this.cl = cl;
184: }
185: }
|