001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)I18NBundle.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import java.text.MessageFormat;
032: import java.util.Locale;
033: import java.util.MissingResourceException;
034: import java.util.ResourceBundle;
035:
036: /** This class reads the i18n strings from locale specific bundle from the
037: * Bundle[locale].properties or bundle[locale].properties file in a specified
038: * package. This class has methods for formating the messages.
039: *
040: * @author Sun Microsystems, Inc.
041: */
042: public class I18NBundle {
043: /** package name */
044: private String mBundlePackageName = null;
045: /** resource bundle */
046: private ResourceBundle mBundle = null;
047:
048: /**
049: * constructor
050: * @param packageName packe name ( e.g. com.sun.mypackage ) in which
051: * to look for Bundle.properties file
052: */
053: public I18NBundle(String packageName) {
054: this .mBundlePackageName = packageName;
055: this .mBundle = null;
056:
057: }
058:
059: /**
060: * loads the bundle
061: * @param bundleName bundle name
062: * @param packageName packe name ( e.g. com.sun.mypackage ) in which
063: * to look for Bundle.properties file
064: */
065: private void loadBundle(String packageName, String bundleName) {
066:
067: String bundleBaseName = packageName + "." + bundleName;
068: ResourceBundle resBundle = null;
069: try {
070: resBundle = ResourceBundle.getBundle(bundleBaseName);
071: } catch (MissingResourceException ex) {
072: // Try with locale independent defaultBundle
073: try {
074: resBundle = ResourceBundle.getBundle(bundleBaseName,
075: new Locale(""));
076: } catch (Exception anyEx) {
077: Util.logDebug(anyEx);
078: }
079: }
080:
081: if (resBundle != null) {
082: this .mBundle = resBundle;
083: }
084: }
085:
086: /**
087: * gets the loaded resource bundle
088: * @return resource bundle
089: */
090: public ResourceBundle getBundle() {
091: // lazzy init
092: if (this .mBundle == null) {
093: loadBundle(this .mBundlePackageName, "Bundle");
094: // try to load the bundle with lower case first letter
095: if (this .mBundle == null) {
096: loadBundle(this .mBundlePackageName, "bundle");
097: }
098: }
099: return this .mBundle;
100: }
101:
102: /** gets the i18n message
103: * @param aI18NMsg String.
104: * @param aArgs Object[]
105: * @return formated i18n string.
106: */
107: public static String getFormattedMessage(String aI18NMsg,
108: Object[] aArgs) {
109: String formattedI18NMsg = aI18NMsg;
110: try {
111: MessageFormat mf = new MessageFormat(aI18NMsg);
112: formattedI18NMsg = mf.format(aArgs);
113: } catch (Exception ex) {
114: }
115: return formattedI18NMsg;
116: }
117:
118: /** gets the i18n message
119: * @param aI18NKey i18n key
120: * @param anArgsArray array of arguments for the formatted string
121: * @return formatted i18n string
122: */
123: public String getMessage(String aI18NKey, Object[] anArgsArray) {
124: String i18nMessage = getBundle().getString(aI18NKey);
125: if (anArgsArray != null) {
126: return getFormattedMessage(i18nMessage, anArgsArray);
127: } else {
128: return i18nMessage;
129: }
130: }
131:
132: /** gets the i18n message
133: * @param aI18NKey i18n key
134: * @return i18n string
135: */
136: public String getMessage(String aI18NKey) {
137: return getMessage(aI18NKey, null);
138: }
139:
140: /** gets the i18n message
141: * @param aI18NKey i18n key
142: * @param arg1 argrument object to message
143: * @return i18n string
144: */
145: public String getMessage(String aI18NKey, Object arg1) {
146: Object[] args = { arg1 };
147: return getMessage(aI18NKey, args);
148: }
149:
150: /** gets the i18n message
151: * @param aI18NKey i18n key
152: * @param arg1 argrument object to message
153: * @param arg2 argrument object to message
154: * @return i18n string
155: */
156: public String getMessage(String aI18NKey, Object arg1, Object arg2) {
157: Object[] args = { arg1, arg2 };
158: return getMessage(aI18NKey, args);
159: }
160:
161: /** gets the i18n message
162: * @param aI18NKey i18n key
163: * @param arg1 argrument object to message
164: * @param arg2 argrument object to message
165: * @param arg3 argrument object to message
166: * @return i18n string
167: */
168: public String getMessage(String aI18NKey, Object arg1, Object arg2,
169: Object arg3) {
170: Object[] args = { arg1, arg2, arg3 };
171: return getMessage(aI18NKey, args);
172: }
173:
174: /**
175: * main
176: * @param args string array.
177: */
178: public static void main(String[] args) {
179: Locale l = new Locale("");
180: System.out.println("Locale : " + l);
181: System.out.println("Default Locale : " + Locale.getDefault());
182: I18NBundle bundle = new I18NBundle("com.sun.jbi.ui.ant");
183: System.out.println(bundle.getMessage(
184: "jbi.ui.ant.jmx.msg.jmxmp.connected",
185: new String[] { "xyz" }));
186: }
187:
188: }
|