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.text.MessageFormat;
033: import java.util.Iterator;
034: import java.util.LinkedList;
035: import java.util.Locale;
036: import java.util.MissingResourceException;
037: import java.util.ResourceBundle;
038:
039: import com.jeta.open.registry.JETARegistry;
040: import com.jeta.open.resources.ResourceLoader;
041:
042: /**
043: * This is a utility class for dealing with internationalization issues
044: *
045: * @author Jeff Tassin
046: */
047: public class I18NHelper {
048: private Locale m_locale;
049: private static I18NHelper m_singleton;
050: private LinkedList m_bundles = new LinkedList();
051:
052: private I18NHelper() {
053:
054: }
055:
056: public boolean equals(String arg1, String arg2) {
057: if (arg1 == null || arg2 == null)
058: return false;
059:
060: return arg1.equals(arg2);
061: }
062:
063: public static I18NHelper getInstance() {
064: if (m_singleton == null)
065: m_singleton = new I18NHelper();
066:
067: return m_singleton;
068: }
069:
070: public void loadBundle(String bundleName) {
071: ResourceBundle currentbundle = null;
072: ResourceLoader loader = (ResourceLoader) JETARegistry
073: .lookup(ResourceLoader.COMPONENT_ID);
074: currentbundle = ResourceBundle.getBundle(bundleName, m_locale,
075: loader.getClassLoader());
076: m_bundles.add(currentbundle);
077: }
078:
079: public void setLocale(Locale locale) {
080: Locale.setDefault(locale);
081: m_locale = locale;
082: }
083:
084: /**
085: * @return a locale specific message string for a message with a variable
086: * number of arguments. Allows string messages to be composed
087: * dynamically.
088: */
089: public String format(String template, Object[] arguments) {
090: MessageFormat formatter = new MessageFormat("");
091: formatter.setLocale(m_locale);
092: formatter.applyPattern(getLocalizedMessage(template));
093: return formatter.format(arguments);
094: }
095:
096: public String _getLocalizedMessage(ResourceBundle bundle,
097: String messageId) {
098: String result = null;
099: try {
100: result = bundle.getString(messageId);
101: } catch (MissingResourceException mre) {
102: }
103: return result;
104: }
105:
106: public String getLocalizedMessage(String messageId) {
107: if (messageId == null)
108: return null;
109:
110: String result = null;
111: Iterator iter = m_bundles.iterator();
112: while (iter.hasNext()) {
113: ResourceBundle bundle = (ResourceBundle) iter.next();
114: result = _getLocalizedMessage(bundle, messageId);
115: if (result != null)
116: break;
117:
118: }
119: if (result == null) {
120: result = messageId;
121: }
122: return result;
123: }
124:
125: public boolean toBoolean(String strVal) {
126: if (strVal == null)
127: return false;
128: return strVal.compareToIgnoreCase(getLocalizedMessage("true")) == 0;
129: }
130:
131: // //////////////////////////////////////////////////////////////////////////////////////////////
132: // Helpers
133:
134: public char closeParenthesis() {
135: return ')';
136: }
137:
138: public char comma() {
139: return ',';
140: }
141:
142: public char newline() {
143: return '\n';
144: }
145:
146: public char openParenthesis() {
147: return '(';
148: }
149:
150: public char semicolon() {
151: return ';';
152: }
153:
154: public char space() {
155: return ' ';
156: }
157: }
|