KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > language > Messages


1 /* Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without modification, is permitted
4  * provided that the following conditions are met:
5  * - Redistributions of source code must retain the above copyright notice, this list of conditions
6  * and the following disclaimer.
7  * - Redistributions in binary form must reproduce the above copyright notice, this list of
8  * conditions and the following disclaimer in the documentation and/or other materials
9  * provided with the distribution.
10  * - All advertising materials mentioning features or use of this software must display the
11  * following acknowledgment: "This product includes Djeneric."
12  * - Products derived from this software may not be called "Djeneric" nor may
13  * "Djeneric" appear in their names without prior written permission of Genimen BV.
14  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
15  * product includes Djeneric."
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */

29 package com.genimen.djeneric.language;
30
31 import java.util.Locale JavaDoc;
32 import java.util.MissingResourceException JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import com.genimen.djeneric.util.DjStringReplacer;
36
37 public class Messages
38 {
39
40   public static final String JavaDoc DJENERIC_LANGUAGE = "djeneric.language";
41   private static final String JavaDoc BUNDLE_NAME = "com.genimen.djeneric.language.messages";
42
43   private static ResourceBundle JavaDoc RESOURCE_BUNDLE;
44
45   static
46   {
47     reloadResources();
48   }
49
50   public static void reloadResources()
51   {
52     String JavaDoc language = System.getProperty(DJENERIC_LANGUAGE);
53
54     // Default locale seems to mess up overriding a language setting
55
// Therefore we have no default bundle and revert to "en" if there
56
// is no setting. This should result in the same behaviour
57
if (language == null) language = "en";
58     RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME, new Locale JavaDoc(language, "", ""));
59   }
60
61   private Messages()
62   {
63     // Private because only static access allowed
64
}
65
66   public static String JavaDoc getString(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3, Object JavaDoc p4)
67   {
68     if (p1 == null) p1 = "null";
69     if (p2 == null) p2 = "null";
70     if (p3 == null) p3 = "null";
71     if (p4 == null) p4 = "null";
72
73     return getString(key, p1, p2, p3, p4, null);
74   }
75
76   public static String JavaDoc getString(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3)
77   {
78     if (p1 == null) p1 = "null";
79     if (p2 == null) p2 = "null";
80     if (p3 == null) p3 = "null";
81
82     return getString(key, p1, p2, p3, null, null);
83   }
84
85   public static String JavaDoc getString(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2)
86   {
87     if (p1 == null) p1 = "null";
88     if (p2 == null) p2 = "null";
89
90     return getString(key, p1, p2, null, null, null);
91   }
92
93   public static String JavaDoc getString(String JavaDoc key, Object JavaDoc p1)
94   {
95     if (p1 == null) p1 = "null";
96
97     return getString(key, p1, null, null, null, null);
98   }
99
100   public static String JavaDoc getString(String JavaDoc key, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3, Object JavaDoc p4, Object JavaDoc p5)
101   {
102     DjStringReplacer sr = new DjStringReplacer(getString(key));
103
104     if (p1 != null)
105     {
106       sr.replace("{1}", p1.toString());
107     }
108
109     if (p2 != null)
110     {
111       sr.replace("{2}", p2.toString());
112     }
113
114     if (p3 != null)
115     {
116       sr.replace("{3}", p3.toString());
117     }
118
119     if (p4 != null)
120     {
121       sr.replace("{4}", p4.toString());
122     }
123
124     if (p5 != null)
125     {
126       sr.replace("{5}", p5.toString());
127     }
128
129     return sr.toString();
130   }
131
132   public static String JavaDoc getString(String JavaDoc key)
133   {
134     try
135     {
136       return RESOURCE_BUNDLE.getString(key);
137     }
138     catch (MissingResourceException JavaDoc e)
139     {
140       return '!' + key + '!';
141     }
142   }
143 }
Popular Tags