001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.open.i18n;
031:
032: import java.util.Collection;
033: import java.util.Iterator;
034:
035: /**
036: * This is a wrapper class around I18NHelper
037: *
038: * @author Jeff Tassin
039: */
040: public class I18N {
041: public static int compareToIgnoreCase(String str1, String str2) {
042: if (str1 != null)
043: return str1.compareToIgnoreCase(str2);
044: else
045: return -1;
046: }
047:
048: /**
049: * Compares two string arrays in a Locale independent way.
050: *
051: * @param str1
052: * the first string to compare
053: * @param str2
054: * the second string to compare
055: * @return true if the two strings are equals
056: */
057: public static boolean equals(char[] str1, char[] str2) {
058: if (str1 == null)
059: return false;
060:
061: if (str2 == null)
062: return false;
063:
064: if (str1.length == 0)
065: return false;
066:
067: if (str1.length != str2.length)
068: return false;
069:
070: for (int index = 0; index < str1.length; index++) {
071: if (str1[index] != str2[index])
072: return false;
073: }
074: return true;
075: }
076:
077: /**
078: * Compares two strings in a Locale independent way.
079: *
080: * @param str1
081: * the first string to compare
082: * @param str2
083: * the second string to compare
084: * @return true if the two strings are equals
085: */
086: public static boolean equals(String str1, String str2) {
087: if (str1 == null)
088: return false;
089: // @todo this currently does not work correctly for I18N
090: return str1.equals(str2);
091: }
092:
093: /**
094: * Compares two strings in a Locale independent way.
095: *
096: * @param str1
097: * the first string to compare
098: * @param str2
099: * the second string to compare
100: * @return true if the two strings are equals
101: */
102: public static boolean equalsIgnoreCase(String str1, String str2) {
103: if (str1 == null)
104: return false;
105: // @todo this currently does not work correctly for I18N
106: return str1.equalsIgnoreCase(str2);
107: }
108:
109: /**
110: * A wrapper routine around I18NHelper for formatting locale specific
111: * messages.
112: *
113: * @param template
114: * the template name of the message
115: * @param arguments
116: * an array or arguments required for the message
117: * @return the formatted message.
118: */
119: public static String format(String template, Object[] arguments) {
120: return I18NHelper.getInstance().format(template, arguments);
121: }
122:
123: /**
124: * This is a helper routine that formats a message string that takes 1
125: * argument in the current locale
126: *
127: * @param template
128: * the name of the message template to use
129: * @param arg1
130: * the argument value for the message tempalte
131: * @return the formatted message
132: */
133: public static String format(String template, Object arg1) {
134: Object[] args = new Object[1];
135: if (arg1 == null)
136: args[0] = "";
137: else
138: args[0] = arg1;
139:
140: return I18NHelper.getInstance().format(template, args);
141: }
142:
143: /**
144: * This is a helper routine that formats a message string that takes 2
145: * arguments in the current locale
146: *
147: * @param template
148: * the name of the message template to use
149: * @param arg1
150: * the first argument value for the message tempalte
151: * @param arg2
152: * the second argument value for the message tempalte
153: * @return the formatted message
154: */
155: public static String format(String template, Object arg1,
156: Object arg2) {
157: Object[] args = new Object[2];
158: args[0] = arg1;
159: args[1] = arg2;
160: return I18NHelper.getInstance().format(template, args);
161: }
162:
163: /**
164: * This is a helper routine that formats a message string that takes 3
165: * arguments in the current locale
166: *
167: * @param template
168: * the name of the message template to use
169: * @param arg1
170: * the first argument value for the message tempalte
171: * @param arg2
172: * the second argument value for the message tempalte
173: * @param arg3
174: * the third argument value for the message tempalte
175: * @return the formatted message
176: */
177: public static String format(String template, Object arg1,
178: Object arg2, Object arg3) {
179: Object[] args = new Object[3];
180: args[0] = arg1;
181: args[1] = arg2;
182: args[2] = arg3;
183: return I18NHelper.getInstance().format(template, args);
184: }
185:
186: /**
187: * iterates over the collection of objects and creates a string that
188: * contains the objects separated by a comma (or the locale version of a CSV
189: * list). If an item in the array is null it is skipped.
190: *
191: * @return the generated string
192: */
193: public static String generateCSVList(String[] strs) {
194: if (strs == null)
195: return null;
196:
197: StringBuffer buffer = new StringBuffer();
198: boolean bfirst = true;
199: for (int index = 0; index < strs.length; index++) {
200: String str = strs[index];
201: if (str != null) {
202:
203: if (bfirst)
204: bfirst = false;
205: else
206: buffer.append(", ");
207:
208: buffer.append(str);
209: }
210: }
211: return buffer.toString();
212: }
213:
214: /**
215: * iterates over the collection of objects and creates a string that
216: * contains the objects separated by a comma (or the locale version of a CSV
217: * list)
218: *
219: * @return the generated string
220: */
221: public static String generateCSVList(Collection objs) {
222: if (objs == null)
223: return null;
224:
225: StringBuffer result = new StringBuffer();
226: Iterator iter = objs.iterator();
227: while (iter.hasNext()) {
228: Object obj = iter.next();
229: if (obj != null)
230: result.append(obj.toString());
231:
232: if (iter.hasNext())
233: result.append(",");
234: }
235: return result.toString();
236: }
237:
238: /**
239: * Returns the locale string for the given resource name with the addition
240: * of a colon on the end (for western languages). Dialog labels generally
241: * have a colon at the end of the label.
242: *
243: * @param resourceName
244: * the name of the resource to lookup in the properties files.
245: * @return the localized string. If the resourceName is not found, it is
246: * returned.
247: */
248: public static String getLocalizedDialogLabel(String resourceName) {
249: return getLocalizedMessage(resourceName) + ":";
250: }
251:
252: /**
253: * Gets the locale string for the given resource name.
254: *
255: * @param resourceName
256: * the name of the resource to lookup in the properties files.
257: * @return the localized string. If the resourceName is not found, it is
258: * returned.
259: */
260: public static String getLocalizedMessage(String resourceName) {
261: return I18NHelper.getInstance().getLocalizedMessage(
262: resourceName);
263: }
264:
265: /**
266: * Gets the locale string for the given resource name.
267: *
268: * @param resourceName
269: * the name of the resource to lookup in the properties files.
270: * @return the localized string. If the resourceName is not found, it is
271: * returned.
272: * @deprecated
273: */
274: public static String getResource(String resourceName) {
275: return I18NHelper.getInstance().getLocalizedMessage(
276: resourceName);
277: }
278:
279: }
|