001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.text.MessageFormat;
019: import java.util.MissingResourceException;
020: import java.util.ResourceBundle;
021:
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.commons.logging.Log;
024:
025: /**
026: * Internationalization for DWR
027: * @author Joe Walker [joe at getahead dot ltd dot uk]
028: */
029: public class Messages {
030: /**
031: * Prevent instansiation
032: */
033: private Messages() {
034: }
035:
036: /**
037: * Internationalize a simple string
038: * @param key The lookup key
039: * @return The internationalized version of the key
040: */
041: public static String getString(String key) {
042: try {
043: return RESOURCE_BUNDLE.getString(key);
044: } catch (MissingResourceException ex) {
045: log.error("Missing I18N string: " + key, ex);
046: return '!' + key + '!';
047: }
048: }
049:
050: /**
051: * Internationalize a parameterized string
052: * @param key The lookup key
053: * @param param Lookup parameter number 1
054: * @return The internationalized version of the key
055: */
056: public static String getString(String key, Object param) {
057: return getString(key, new Object[] { param, });
058: }
059:
060: /**
061: * Internationalize a parameterized string
062: * @param key The lookup key
063: * @param param1 Lookup parameter number 1
064: * @param param2 Lookup parameter number 2
065: * @return The internationalized version of the key
066: */
067: public static String getString(String key, Object param1,
068: Object param2) {
069: return getString(key, new Object[] { param1, param2, });
070: }
071:
072: /**
073: * Internationalize a parameterized string
074: * @param key The lookup key
075: * @param param1 Lookup parameter number 1
076: * @param param2 Lookup parameter number 2
077: * @param param3 Lookup parameter number 3
078: * @return The internationalized version of the key
079: */
080: public static String getString(String key, Object param1,
081: Object param2, Object param3) {
082: return getString(key, new Object[] { param1, param2, param3, });
083: }
084:
085: /**
086: * Internationalize a parameterized string
087: * @param key The lookup key
088: * @param param1 Lookup parameter number 1
089: * @param param2 Lookup parameter number 2
090: * @param param3 Lookup parameter number 3
091: * @param param4 Lookup parameter number 4
092: * @return The internationalized version of the key
093: */
094: public static String getString(String key, Object param1,
095: Object param2, Object param3, Object param4) {
096: return getString(key, new Object[] { param1, param2, param3,
097: param4, });
098: }
099:
100: /**
101: * Internationalize a parameterized string
102: * @param key The lookup key
103: * @param params The parameters to fill in the string
104: * @return The internationalized version of the key
105: */
106: public static String getString(String key, Object[] params) {
107: try {
108: String format = RESOURCE_BUNDLE.getString(key);
109: return MessageFormat.format(format, params);
110: } catch (MissingResourceException ex) {
111: // Create a list of the parameters
112: StringBuffer buffer = new StringBuffer();
113: buffer.append('[');
114: for (int i = 0; i < params.length; i++) {
115: if (i != 0) {
116: buffer.append(',');
117: }
118: buffer.append(params[i].toString());
119: }
120: buffer.append(']');
121:
122: log.error("Missing I18N string: " + key + ". Params: "
123: + buffer.toString());
124: return '!' + key + '!' + buffer.toString() + '!';
125: }
126: }
127:
128: /**
129: * The log stream
130: */
131: private static final Log log = LogFactory.getLog(Messages.class);
132:
133: /**
134: * The lookup bundle
135: */
136: private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
137: .getBundle("org.directwebremoting.messages");
138: }
|