1 |
2778
|
jones
|
<%@ page import="java.util.*" %>
|
2 |
|
|
<%
|
3 |
|
|
/*
|
4 |
|
|
* Copyright 2005 The Apache Software Foundation.
|
5 |
|
|
*
|
6 |
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
|
|
* you may not use this file except in compliance with the License.
|
8 |
|
|
* You may obtain a copy of the License at
|
9 |
|
|
*
|
10 |
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
|
*
|
12 |
|
|
* Unless required by applicable law or agreed to in writing, software
|
13 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
|
|
* See the License for the specific language governing permissions and
|
16 |
|
|
* limitations under the License.
|
17 |
|
|
*/
|
18 |
|
|
%>
|
19 |
|
|
|
20 |
|
|
<%!
|
21 |
|
|
/*
|
22 |
|
|
* A library file to produce i18n web applications. This can be easily
|
23 |
|
|
* reused from your jsp(s) - just include and call any methods.
|
24 |
|
|
* @author toshi
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
// private variable
|
28 |
|
|
HttpServletRequest _req = null;
|
29 |
|
|
|
30 |
|
|
// private variable
|
31 |
|
|
String _strResourceName = null;
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* Set a HttpServletRequest to a private variable.
|
35 |
|
|
* @param request HttpServletRequest
|
36 |
|
|
*/
|
37 |
|
|
void setRequest(HttpServletRequest request) {
|
38 |
|
|
_req = request;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* Get the private variable of the HttpServletRequest.
|
43 |
|
|
* @return HttpServletRequest
|
44 |
|
|
*/
|
45 |
|
|
HttpServletRequest getRequest() {
|
46 |
|
|
return _req;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Set a resouce base name to a private variable.
|
51 |
|
|
* @param resouce The resouce base name
|
52 |
|
|
*/
|
53 |
|
|
void setResouceBase(String resource) {
|
54 |
|
|
_strResourceName = resource;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Get the private variable of the resouce base name.
|
59 |
|
|
* @return resouce The resouce base name
|
60 |
|
|
*/
|
61 |
|
|
String getResouceBase() {
|
62 |
|
|
return _strResourceName;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* Get a ResourceBundle object.
|
67 |
|
|
* @return a ResourceBundle object
|
68 |
|
|
*/
|
69 |
|
|
ResourceBundle getRB() {
|
70 |
|
|
String strLocale = getRequest().getParameter("locale");
|
71 |
|
|
ResourceBundle objRb = null;
|
72 |
|
|
Locale objLcl = null;
|
73 |
|
|
|
74 |
|
|
if (strLocale!=null) {
|
75 |
|
|
objLcl=new Locale(strLocale,"");
|
76 |
|
|
} else {
|
77 |
|
|
objLcl=getRequest().getLocale();
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
Locale.setDefault(objLcl);
|
81 |
|
|
objRb = ResourceBundle.getBundle(getResouceBase(),objLcl);
|
82 |
|
|
|
83 |
|
|
return objRb;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* Get a list of locale choice
|
88 |
|
|
* @return a list of supported locales
|
89 |
|
|
*/
|
90 |
|
|
String getLocaleChoice() {
|
91 |
|
|
String choice = getMessage("locales");
|
92 |
|
|
StringBuffer buf = new StringBuffer();
|
93 |
|
|
|
94 |
|
|
buf.append("<div align=\"right\">\n");
|
95 |
|
|
buf.append(getMessage("language"));
|
96 |
|
|
buf.append(": ");
|
97 |
|
|
|
98 |
|
|
StringTokenizer st = new StringTokenizer(choice);
|
99 |
|
|
String locale = null;
|
100 |
|
|
while (st.hasMoreTokens()) {
|
101 |
|
|
locale = st.nextToken();
|
102 |
|
|
buf.append("[<a href=\"?locale="+ locale +"\">"+ locale +"</a>] ");
|
103 |
|
|
}
|
104 |
|
|
buf.append("\n</div>\n");
|
105 |
|
|
|
106 |
|
|
return buf.toString();
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Get a message from i18n.properties with several arguments.
|
111 |
|
|
* @param key The resource key
|
112 |
|
|
* @return The formatted message
|
113 |
|
|
*/
|
114 |
|
|
String getMessage(String key) {
|
115 |
|
|
return getMessage(key, null, null, null, null, null);
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/**
|
119 |
|
|
* Get a message from i18n.properties with several arguments.
|
120 |
|
|
* @param key The resource key
|
121 |
|
|
* @param arg0 The argument to place in variable {0}
|
122 |
|
|
* @return The formatted message
|
123 |
|
|
*/
|
124 |
|
|
String getMessage(String key, String arg0) {
|
125 |
|
|
return getMessage(key, arg0, null, null, null, null);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* Get a message from i18n.properties with several arguments.
|
130 |
|
|
* @param key The resource key
|
131 |
|
|
* @param arg0 The argument to place in variable {0}
|
132 |
|
|
* @param arg1 The argument to place in variable {1}
|
133 |
|
|
* @return The formatted message
|
134 |
|
|
*/
|
135 |
|
|
String getMessage(String key, String arg0, String arg1) {
|
136 |
|
|
return getMessage(key, arg0, arg1, null, null, null);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/**
|
140 |
|
|
* Get a message from i18n.properties with several arguments.
|
141 |
|
|
* @param key The resource key
|
142 |
|
|
* @param arg0 The argument to place in variable {0}
|
143 |
|
|
* @param arg1 The argument to place in variable {1}
|
144 |
|
|
* @param arg2 The argument to place in variable {2}
|
145 |
|
|
* @return The formatted message
|
146 |
|
|
*/
|
147 |
|
|
String getMessage(String key, String arg0, String arg1, String arg2) {
|
148 |
|
|
return getMessage(key, arg0, arg1, arg2, null, null);
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/**
|
152 |
|
|
* Get a message from i18n.properties with several arguments.
|
153 |
|
|
* @param key The resource key
|
154 |
|
|
* @param arg0 The argument to place in variable {0}
|
155 |
|
|
* @param arg1 The argument to place in variable {1}
|
156 |
|
|
* @param arg2 The argument to place in variable {2}
|
157 |
|
|
* @param arg3 The argument to place in variable {3}
|
158 |
|
|
* @return The formatted message
|
159 |
|
|
*/
|
160 |
|
|
String getMessage(String key, String arg0, String arg1,
|
161 |
|
|
String arg2, String arg3) {
|
162 |
|
|
return getMessage(key, arg0, arg1, arg2, arg3, null);
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
/**
|
166 |
|
|
* Get a message from i18n.properties with several arguments.
|
167 |
|
|
* @param key The resource key
|
168 |
|
|
* @param arg0 The argument to place in variable {0}
|
169 |
|
|
* @param arg1 The argument to place in variable {1}
|
170 |
|
|
* @param arg2 The argument to place in variable {2}
|
171 |
|
|
* @param arg3 The argument to place in variable {3}
|
172 |
|
|
* @param arg4 The argument to place in variable {4}
|
173 |
|
|
* @return The formatted message
|
174 |
|
|
*/
|
175 |
|
|
String getMessage(String key, String arg0, String arg1,
|
176 |
|
|
String arg2, String arg3, String arg4) {
|
177 |
|
|
String strPattern = getRB().getString(key);
|
178 |
|
|
|
179 |
|
|
String [] params = { arg0, arg1, arg2, arg3, arg4 };
|
180 |
|
|
for (int i=0; i<5; i++) {
|
181 |
|
|
if (params[i]!=null) params[i]=replaceAll(params[i],"%20"," ");
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
if (arg0!=null) strPattern = replaceAll(strPattern,"{0}",params[0]);
|
185 |
|
|
if (arg1!=null) strPattern = replaceAll(strPattern,"{1}",params[1]);
|
186 |
|
|
if (arg2!=null) strPattern = replaceAll(strPattern,"{2}",params[2]);
|
187 |
|
|
if (arg3!=null) strPattern = replaceAll(strPattern,"{3}",params[3]);
|
188 |
|
|
if (arg4!=null) strPattern = replaceAll(strPattern,"{4}",params[4]);
|
189 |
|
|
|
190 |
|
|
return strPattern;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
/**
|
194 |
|
|
* Get a replaced string by the specified message.
|
195 |
|
|
* @param source The original message
|
196 |
|
|
* @param pattern The key message for replacing
|
197 |
|
|
* @param replace The message to place in the key variable - 'pattern'
|
198 |
|
|
* @return The replaced message
|
199 |
|
|
*/
|
200 |
|
|
String replaceAll(String source, String pattern, String replace)
|
201 |
|
|
{
|
202 |
|
|
int i=0;
|
203 |
|
|
boolean ret = false;
|
204 |
|
|
StringBuffer buf = new StringBuffer();
|
205 |
|
|
|
206 |
|
|
int lenSource = source.length();
|
207 |
|
|
int lenPattern = pattern.length();
|
208 |
|
|
|
209 |
|
|
for (i=0; i<lenSource; i++) {
|
210 |
|
|
ret = source.regionMatches(i, pattern, 0, lenPattern);
|
211 |
|
|
if (ret) {
|
212 |
|
|
buf.append(source.substring(0,i));
|
213 |
|
|
buf.append(replace);
|
214 |
|
|
buf.append(source.substring(i+lenPattern));
|
215 |
|
|
source = replaceAll(buf.toString(), pattern, replace);
|
216 |
|
|
break;
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
return source;
|
220 |
|
|
}
|
221 |
|
|
%>
|