KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > i18n > I18N


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Nicolas Modrzyk
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.common.i18n;
24
25 import java.text.MessageFormat JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 /**
29  * This class defines a I18N
30  *
31  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
32  * @version 1.0
33  */

34 public abstract class I18N
35 {
36   /**
37    * Returns associated sentence to that key
38    *
39    * @param key the key to find in the translation file
40    * @param bundle then translation bundle to use
41    * @return the corresponding sentence of the key if not found
42    */

43   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key)
44   {
45     try
46     {
47       return bundle.getString(key);
48     }
49     catch (Exception JavaDoc e)
50     {
51       return key;
52     }
53   }
54
55   /**
56    * Returns translated key with instanciated parameters
57    *
58    * @param bundle then translation bundle to use
59    * @param key the key to find in translation file.
60    * @param parameter the parameter value
61    * @return the corresponding sentence with key and parameters
62    */

63   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key, boolean parameter)
64   {
65     return MessageFormat.format(get(bundle, key), new Object JavaDoc[]{String
66         .valueOf(parameter)});
67   }
68
69   /**
70    * Returns translated key with instanciated parameters
71    *
72    * @param bundle then translation bundle to use
73    * @param key the key to find in translation file.
74    * @param parameter the parameter value
75    * @return the corresponding sentence with key and parameters
76    */

77   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key, int parameter)
78   {
79     return MessageFormat.format(get(bundle, key), new Object JavaDoc[]{String
80         .valueOf(parameter)});
81   }
82
83   /**
84    * Returns translated key with instanciated parameters
85    *
86    * @param bundle then translation bundle to use
87    * @param key the key to find in translation file.
88    * @param parameter the parameter value
89    * @return the corresponding sentence with key and parameters
90    */

91   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key, long parameter)
92   {
93     return MessageFormat.format(get(bundle, key), new Object JavaDoc[]{String
94         .valueOf(parameter)});
95   }
96
97   /**
98    * Replace <code>REPLACE_CHAR</code> in the translated message with
99    * parameters. If you have more parameters than charaters to replace,
100    * remaining parameters are appended as a comma separated list at the end of
101    * the message.
102    *
103    * @param bundle then translation bundle to use
104    * @param key the key to find in the translation file
105    * @param parameters to put inside square braquets
106    * @return the corresponding sentence of the key if not found
107    */

108   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key,
109       Object JavaDoc[] parameters)
110   {
111     return MessageFormat.format(get(bundle, key), parameters);
112   }
113
114   /**
115    * Same as above but implies creation of an array for the parameter
116    *
117    * @param bundle then translation bundle to use
118    * @param key to translate
119    * @param parameter to put in translation
120    * @return translated message
121    */

122   public static String JavaDoc get(ResourceBundle JavaDoc bundle, String JavaDoc key, Object JavaDoc parameter)
123   {
124     return MessageFormat.format(get(bundle, key), new Object JavaDoc[]{parameter});
125   }
126
127 }
Popular Tags