001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: LocalizableSupport.java 447277 2006-09-18 06:19:34Z jeremias $ */
019:
020: package org.apache.xmlgraphics.util.i18n;
021:
022: import java.text.MessageFormat;
023: import java.util.Locale;
024: import java.util.ResourceBundle;
025:
026: /**
027: * This class provides a default implementation of the Localizable interface.
028: * You can use it as a base class or as a member field and delegates various
029: * work to it.<p>
030: * For example, to implement Localizable, the following code can be used:
031: * <pre>
032: * package mypackage;
033: * ...
034: * public class MyClass implements Localizable {
035: * // This code fragment requires a file named
036: * // 'mypackage/resources/Messages.properties', or a
037: * // 'mypackage.resources.Messages' class which extends
038: * // java.util.ResourceBundle, accessible using the current
039: * // classpath.
040: * LocalizableSupport localizableSupport =
041: * new LocalizableSupport("mypackage.resources.Messages");
042: *
043: * public void setLocale(Locale l) {
044: * localizableSupport.setLocale(l);
045: * }
046: * public Local getLocale() {
047: * return localizableSupport.getLocale();
048: * }
049: * public String formatMessage(String key, Object[] args) {
050: * return localizableSupport.formatMessage(key, args);
051: * }
052: * }
053: * </pre>
054: * The algorithm for the Locale lookup in a LocalizableSupport object is:
055: * <ul>
056: * <li>
057: * if a Locale has been set by a call to setLocale(), use this Locale,
058: * else,
059: * <li/>
060: * <li>
061: * if a Locale has been set by a call to the setDefaultLocale() method
062: * of a LocalizableSupport object in the current LocaleGroup, use this
063: * Locale, else,
064: * </li>
065: * <li>
066: * use the object returned by Locale.getDefault() (and set by
067: * Locale.setDefault()).
068: * <li/>
069: * </ul>
070: * This offers the possibility to have a different Locale for each object,
071: * a Locale for a group of object and/or a Locale for the JVM instance.
072: * <p>
073: * Note: if no group is specified a LocalizableSupport object belongs to a
074: * default group common to each instance of LocalizableSupport.
075: *
076: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
077: * @version $Id: LocalizableSupport.java 447277 2006-09-18 06:19:34Z jeremias $
078: */
079: public class LocalizableSupport implements Localizable {
080: /**
081: * The locale group to which this object belongs.
082: */
083: protected LocaleGroup localeGroup = LocaleGroup.DEFAULT;
084:
085: /**
086: * The resource bundle classname.
087: */
088: protected String bundleName;
089:
090: /**
091: * The classloader to use to create the resource bundle.
092: */
093: protected ClassLoader classLoader;
094:
095: /**
096: * The current locale.
097: */
098: protected Locale locale;
099:
100: /**
101: * The locale in use.
102: */
103: protected Locale usedLocale;
104:
105: /**
106: * The resources
107: */
108: protected ResourceBundle resourceBundle;
109:
110: /**
111: * Same as LocalizableSupport(s, null).
112: */
113: public LocalizableSupport(String s) {
114: this (s, null);
115: }
116:
117: /**
118: * Creates a new Localizable object.
119: * The resource bundle class name is required allows the use of custom
120: * classes of resource bundles.
121: * @param s must be the name of the class to use to get the appropriate
122: * resource bundle given the current locale.
123: * @param cl is the classloader used to create the resource bundle,
124: * or null.
125: * @see java.util.ResourceBundle
126: */
127: public LocalizableSupport(String s, ClassLoader cl) {
128: bundleName = s;
129: classLoader = cl;
130: }
131:
132: /**
133: * Implements {@link org.apache.xmlgraphics.util.i18n.Localizable#setLocale(Locale)}.
134: */
135: public void setLocale(Locale l) {
136: if (locale != l) {
137: locale = l;
138: resourceBundle = null;
139: }
140: }
141:
142: /**
143: * Implements {@link org.apache.xmlgraphics.util.i18n.Localizable#getLocale()}.
144: */
145: public Locale getLocale() {
146: return locale;
147: }
148:
149: /**
150: * Implements {@link
151: * org.apache.xmlgraphics.util.i18n.ExtendedLocalizable#setLocaleGroup(LocaleGroup)}.
152: */
153: public void setLocaleGroup(LocaleGroup lg) {
154: localeGroup = lg;
155: }
156:
157: /**
158: * Implements {@link
159: * org.apache.xmlgraphics.util.i18n.ExtendedLocalizable#getLocaleGroup()}.
160: */
161: public LocaleGroup getLocaleGroup() {
162: return localeGroup;
163: }
164:
165: /**
166: * Implements {@link
167: * org.apache.xmlgraphics.util.i18n.ExtendedLocalizable#setDefaultLocale(Locale)}.
168: * Later invocations of the instance methods will lead to update the
169: * resource bundle used.
170: */
171: public void setDefaultLocale(Locale l) {
172: localeGroup.setLocale(l);
173: }
174:
175: /**
176: * Implements {@link
177: * org.apache.xmlgraphics.util.i18n.ExtendedLocalizable#getDefaultLocale()}.
178: */
179: public Locale getDefaultLocale() {
180: return localeGroup.getLocale();
181: }
182:
183: /**
184: * Implements {@link
185: * org.apache.xmlgraphics.util.i18n.Localizable#formatMessage(String,Object[])}.
186: */
187: public String formatMessage(String key, Object[] args) {
188: getResourceBundle();
189: return MessageFormat
190: .format(resourceBundle.getString(key), args);
191: }
192:
193: /**
194: * Implements {@link
195: * org.apache.xmlgraphics.util.i18n.ExtendedLocalizable#getResourceBundle()}.
196: */
197: public ResourceBundle getResourceBundle() {
198: Locale l;
199:
200: if (resourceBundle == null) {
201: if (locale == null) {
202: if ((l = localeGroup.getLocale()) == null) {
203: usedLocale = Locale.getDefault();
204: } else {
205: usedLocale = l;
206: }
207: } else {
208: usedLocale = locale;
209: }
210: if (classLoader == null) {
211: resourceBundle = ResourceBundle.getBundle(bundleName,
212: usedLocale);
213: } else {
214: resourceBundle = ResourceBundle.getBundle(bundleName,
215: usedLocale, classLoader);
216: }
217: } else if (locale == null) {
218: // Check for group Locale and JVM default locale changes.
219: if ((l = localeGroup.getLocale()) == null) {
220: if (usedLocale != (l = Locale.getDefault())) {
221: usedLocale = l;
222: if (classLoader == null) {
223: resourceBundle = ResourceBundle.getBundle(
224: bundleName, usedLocale);
225: } else {
226: resourceBundle = ResourceBundle.getBundle(
227: bundleName, usedLocale, classLoader);
228: }
229: }
230: } else if (usedLocale != l) {
231: usedLocale = l;
232: if (classLoader == null) {
233: resourceBundle = ResourceBundle.getBundle(
234: bundleName, usedLocale);
235: } else {
236: resourceBundle = ResourceBundle.getBundle(
237: bundleName, usedLocale, classLoader);
238: }
239: }
240: }
241:
242: return resourceBundle;
243: }
244: }
|