KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > i18n > I18N


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

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

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

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

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

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

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

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

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