KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > L10N


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 import com.caucho.log.Log;
32
33 import java.io.InputStream JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39 /**
40  * Localization
41  */

42 public class L10N {
43   private static Logger JavaDoc _log;
44   
45   static HashMap JavaDoc<String JavaDoc,HashMap JavaDoc<String JavaDoc,String JavaDoc>> l10nMap
46     = new HashMap JavaDoc<String JavaDoc,HashMap JavaDoc<String JavaDoc,String JavaDoc>>();
47   
48   HashMap JavaDoc<String JavaDoc,String JavaDoc> messages;
49   
50   public L10N(Class JavaDoc cl)
51   {
52     String JavaDoc name = cl.getName().replace('.', '/');
53     int p = name.lastIndexOf('/');
54
55     if (p > 0)
56       init(name.substring(0, p) + "/messages");
57     else
58       init("/messages");
59   }
60 /*
61   public L10N(String path)
62   {
63     init(path);
64   }
65 */

66
67   private void init(String JavaDoc path)
68   {
69     if (! path.startsWith("/"))
70       path = "/" + path;
71
72     messages = l10nMap.get(path);
73     
74     InputStream JavaDoc is = null;
75     Locale JavaDoc locale = Locale.getDefault();
76     
77     try {
78       String JavaDoc language = locale.getLanguage();
79
80       String JavaDoc xmlName = path + "_" + language + ".xml";
81
82       /* XXX: punt for now
83       is = getClass().getResourceAsStream(xmlName);
84       
85       if (is != null) {
86     RegistryNode registry = null;
87
88     try {
89       ReadStream rs = Vfs.openRead(is);
90         
91       Registry root = Registry.parse(rs);
92       registry = root.getTop();
93
94       rs.close();
95     } finally {
96       is.close();
97     }
98
99         messages = new HashMap<String,String>();
100         
101         l10nMap.put(path, messages);
102
103         RegistryNode localization = registry.lookup("localization");
104         Iterator iter = localization.select("message");
105         while (iter.hasNext()) {
106           RegistryNode msg = (RegistryNode) iter.next();
107
108           String key = msg.getString("key", null);
109           String value = msg.getString("value", null);
110
111           if (key != null && value != null)
112             messages.put(key, value);
113         }
114       }
115       */

116     } catch (Exception JavaDoc e) {
117       log().log(Level.FINE, e.toString(), e);
118     }
119   }
120   
121   public String JavaDoc l(String JavaDoc msg)
122   {
123     msg = getTranslated(msg);
124     
125     return msg;
126   }
127   
128   public String JavaDoc l(String JavaDoc msg, long l)
129   {
130     return l(msg, String.valueOf(l));
131   }
132   
133   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o)
134   {
135     msg = getTranslated(msg);
136
137     CharBuffer cb = CharBuffer.allocate();
138
139     int length = msg.length();
140     int i = 0;
141
142     while (i < length) {
143       char ch = msg.charAt(i);
144
145       if (ch != '{' || i + 2 >= length) {
146         cb.append(ch);
147         i++;
148       }
149       else {
150         ch = msg.charAt(i + 1);
151         
152         if (ch == '{') {
153           cb.append('{');
154           i += 2;
155         }
156         else if (msg.charAt(i + 2) != '}') {
157           cb.append('{');
158           i++;
159         }
160         else if (ch == '0') {
161           cb.append(o);
162           i += 3;
163         }
164         else {
165           cb.append('{');
166           i++;
167         }
168       }
169     }
170     
171     return cb.close();
172   }
173   
174   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2)
175   {
176     msg = getTranslated(msg);
177     
178     CharBuffer cb = CharBuffer.allocate();
179
180     int length = msg.length();
181     int i = 0;
182
183     while (i < length) {
184       char ch = msg.charAt(i);
185
186       if (ch != '{' || i + 2 >= length) {
187         cb.append(ch);
188         i++;
189       }
190       else {
191         ch = msg.charAt(i + 1);
192         
193         if (ch == '{') {
194           cb.append('{');
195           i += 2;
196         }
197         else if (msg.charAt(i + 2) != '}') {
198           cb.append('{');
199           i++;
200         }
201         else if (ch == '0') {
202           cb.append(o1);
203           i += 3;
204         }
205         else if (ch == '1') {
206           cb.append(o2);
207           i += 3;
208         }
209         else {
210           cb.append('{');
211           i++;
212         }
213       }
214     }
215     
216     return cb.close();
217   }
218
219   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o1, int i2)
220   {
221     return l(msg, o1, String.valueOf(i2));
222   }
223
224   public String JavaDoc l(String JavaDoc msg, int i1, int i2)
225   {
226     return l(msg, String.valueOf(i1), String.valueOf(i2));
227   }
228   
229   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3)
230   {
231     msg = getTranslated(msg);
232     
233     CharBuffer cb = CharBuffer.allocate();
234
235     int length = msg.length();
236     int i = 0;
237
238     while (i < length) {
239       char ch = msg.charAt(i);
240
241       if (ch != '{' || i + 2 >= length) {
242         cb.append(ch);
243         i++;
244       }
245       else {
246         ch = msg.charAt(i + 1);
247         
248         if (ch == '{') {
249           cb.append('{');
250           i += 2;
251         }
252         else if (msg.charAt(i + 2) != '}') {
253           cb.append('{');
254           i++;
255         }
256         else if (ch == '0') {
257           cb.append(o1);
258           i += 3;
259         }
260         else if (ch == '1') {
261           cb.append(o2);
262           i += 3;
263         }
264         else if (ch == '2') {
265           cb.append(o3);
266           i += 3;
267         }
268         else {
269           cb.append('{');
270           i++;
271         }
272       }
273     }
274     
275     return cb.close();
276   }
277   
278   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2, Object JavaDoc o3, Object JavaDoc o4)
279   {
280     msg = getTranslated(msg);
281     
282     CharBuffer cb = CharBuffer.allocate();
283
284     int length = msg.length();
285     int i = 0;
286
287     while (i < length) {
288       char ch = msg.charAt(i);
289
290       if (ch != '{' || i + 2 >= length) {
291         cb.append(ch);
292         i++;
293       }
294       else {
295         ch = msg.charAt(i + 1);
296         
297         if (ch == '{') {
298           cb.append('{');
299           i += 2;
300         }
301         else if (msg.charAt(i + 2) != '}') {
302           cb.append('{');
303           i++;
304         }
305         else if (ch == '0') {
306           cb.append(o1);
307           i += 3;
308         }
309         else if (ch == '1') {
310           cb.append(o2);
311           i += 3;
312         }
313         else if (ch == '2') {
314           cb.append(o3);
315           i += 3;
316         }
317         else if (ch == '3') {
318           cb.append(o4);
319           i += 3;
320         }
321         else {
322           cb.append('{');
323           i++;
324         }
325       }
326     }
327     
328     return cb.close();
329   }
330   
331   public String JavaDoc l(String JavaDoc msg, Object JavaDoc o1, Object JavaDoc o2,
332                   Object JavaDoc o3, Object JavaDoc o4, Object JavaDoc o5)
333   {
334     msg = getTranslated(msg);
335     
336     CharBuffer cb = CharBuffer.allocate();
337
338     int length = msg.length();
339     int i = 0;
340
341     while (i < length) {
342       char ch = msg.charAt(i);
343
344       if (ch != '{' || i + 2 >= length) {
345         cb.append(ch);
346         i++;
347       }
348       else {
349         ch = msg.charAt(i + 1);
350         
351         if (ch == '{') {
352           cb.append('{');
353           i += 2;
354         }
355         else if (msg.charAt(i + 2) != '}') {
356           cb.append('{');
357           i++;
358         }
359         else if (ch == '0') {
360           cb.append(o1);
361           i += 3;
362         }
363         else if (ch == '1') {
364           cb.append(o2);
365           i += 3;
366         }
367         else if (ch == '2') {
368           cb.append(o3);
369           i += 3;
370         }
371         else if (ch == '3') {
372           cb.append(o4);
373           i += 3;
374         }
375         else if (ch == '4') {
376           cb.append(o5);
377           i += 3;
378         }
379         else {
380           cb.append('{');
381           i++;
382         }
383       }
384     }
385     
386     return cb.close();
387   }
388
389   private String JavaDoc getTranslated(String JavaDoc msg)
390   {
391     if (messages == null)
392       return msg;
393     
394     String JavaDoc translated = (String JavaDoc) messages.get(msg);
395
396     if (translated == null) {
397       return msg;
398     }
399     else
400       return translated;
401   }
402
403   private Logger JavaDoc log()
404   {
405     if (_log == null)
406       _log = Log.open(L10N.class);
407
408     return _log;
409   }
410 }
411
Popular Tags