001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Nicolas Modrzyk
020: * Contributor(s): ______________________.
021: */package org.continuent.sequoia.common.i18n;
022:
023: import java.text.MessageFormat;
024: import java.util.ResourceBundle;
025:
026: /**
027: * This class defines a I18N
028: *
029: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
030: * @version 1.0
031: */
032: public abstract class I18N {
033: /**
034: * Returns associated sentence to that key
035: *
036: * @param key the key to find in the translation file
037: * @param bundle then translation bundle to use
038: * @return the corresponding sentence of the key if not found
039: */
040: public static String get(ResourceBundle bundle, String key) {
041: try {
042: return bundle.getString(key);
043: } catch (Exception e) {
044: return key;
045: }
046: }
047:
048: /**
049: * Returns translated key with instanciated parameters
050: *
051: * @param bundle then translation bundle to use
052: * @param key the key to find in translation file.
053: * @param parameter the parameter value
054: * @return the corresponding sentence with key and parameters
055: */
056: public static String get(ResourceBundle bundle, String key,
057: boolean parameter) {
058: return MessageFormat.format(get(bundle, key),
059: new Object[] { String.valueOf(parameter) });
060: }
061:
062: /**
063: * Returns translated key with instanciated parameters
064: *
065: * @param bundle then translation bundle to use
066: * @param key the key to find in translation file.
067: * @param parameter the parameter value
068: * @return the corresponding sentence with key and parameters
069: */
070: public static String get(ResourceBundle bundle, String key,
071: int parameter) {
072: return MessageFormat.format(get(bundle, key),
073: new Object[] { String.valueOf(parameter) });
074: }
075:
076: /**
077: * Returns translated key with instanciated parameters
078: *
079: * @param bundle then translation bundle to use
080: * @param key the key to find in translation file.
081: * @param parameter the parameter value
082: * @return the corresponding sentence with key and parameters
083: */
084: public static String get(ResourceBundle bundle, String key,
085: long parameter) {
086: return MessageFormat.format(get(bundle, key),
087: new Object[] { String.valueOf(parameter) });
088: }
089:
090: /**
091: * Replace <code>REPLACE_CHAR</code> in the translated message with
092: * parameters. If you have more parameters than charaters to replace,
093: * remaining parameters are appended as a comma separated list at the end of
094: * the message.
095: *
096: * @param bundle then translation bundle to use
097: * @param key the key to find in the translation file
098: * @param parameters to put inside square braquets
099: * @return the corresponding sentence of the key if not found
100: */
101: public static String get(ResourceBundle bundle, String key,
102: Object[] parameters) {
103: return MessageFormat.format(get(bundle, key), parameters);
104: }
105:
106: /**
107: * Same as above but implies creation of an array for the parameter
108: *
109: * @param bundle then translation bundle to use
110: * @param key to translate
111: * @param parameter to put in translation
112: * @return translated message
113: */
114: public static String get(ResourceBundle bundle, String key,
115: Object parameter) {
116: return MessageFormat.format(get(bundle, key),
117: new Object[] { parameter });
118: }
119:
120: }
|