001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.util;
030:
031: import java.io.InputStream;
032: import java.util.HashMap;
033: import java.util.Locale;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: /**
038: * Localization
039: */
040: public class L10N {
041: private static Logger _log;
042:
043: private static HashMap<String, HashMap<String, String>> l10nMap = new HashMap<String, HashMap<String, String>>();
044:
045: HashMap<String, String> messages;
046:
047: public L10N(Class cl) {
048: String name = cl.getName().replace('.', '/');
049: int p = name.lastIndexOf('/');
050:
051: if (p > 0)
052: init(name.substring(0, p) + "/messages");
053: else
054: init("/messages");
055: }
056:
057: /*
058: public L10N(String path)
059: {
060: init(path);
061: }
062: */
063:
064: private void init(String path) {
065: if (!path.startsWith("/"))
066: path = "/" + path;
067:
068: messages = l10nMap.get(path);
069:
070: InputStream is = null;
071: Locale locale = Locale.getDefault();
072:
073: try {
074: String language = locale.getLanguage();
075:
076: String xmlName = path + "_" + language + ".xml";
077:
078: /* XXX: punt for now
079: is = getClass().getResourceAsStream(xmlName);
080:
081: if (is != null) {
082: RegistryNode registry = null;
083:
084: try {
085: ReadStream rs = Vfs.openRead(is);
086:
087: Registry root = Registry.parse(rs);
088: registry = root.getTop();
089:
090: rs.close();
091: } finally {
092: is.close();
093: }
094:
095: messages = new HashMap<String,String>();
096:
097: l10nMap.put(path, messages);
098:
099: RegistryNode localization = registry.lookup("localization");
100: Iterator iter = localization.select("message");
101: while (iter.hasNext()) {
102: RegistryNode msg = (RegistryNode) iter.next();
103:
104: String key = msg.getString("key", null);
105: String value = msg.getString("value", null);
106:
107: if (key != null && value != null)
108: messages.put(key, value);
109: }
110: }
111: */
112: } catch (Exception e) {
113: log().log(Level.FINE, e.toString(), e);
114: }
115: }
116:
117: public String l(String msg) {
118: msg = getTranslated(msg);
119:
120: return msg;
121: }
122:
123: public String l(String msg, long l) {
124: return l(msg, String.valueOf(l));
125: }
126:
127: public String l(String msg, Object o) {
128: msg = getTranslated(msg);
129:
130: CharBuffer cb = CharBuffer.allocate();
131:
132: int length = msg.length();
133: int i = 0;
134:
135: while (i < length) {
136: char ch = msg.charAt(i);
137:
138: if (ch != '{' || i + 2 >= length) {
139: cb.append(ch);
140: i++;
141: } else {
142: ch = msg.charAt(i + 1);
143:
144: if (ch == '{') {
145: cb.append('{');
146: i += 2;
147: } else if (msg.charAt(i + 2) != '}') {
148: cb.append('{');
149: i++;
150: } else if (ch == '0') {
151: cb.append(o);
152: i += 3;
153: } else {
154: cb.append('{');
155: i++;
156: }
157: }
158: }
159:
160: return cb.close();
161: }
162:
163: public String l(String msg, Object o1, Object o2) {
164: msg = getTranslated(msg);
165:
166: CharBuffer cb = CharBuffer.allocate();
167:
168: int length = msg.length();
169: int i = 0;
170:
171: while (i < length) {
172: char ch = msg.charAt(i);
173:
174: if (ch != '{' || i + 2 >= length) {
175: cb.append(ch);
176: i++;
177: } else {
178: ch = msg.charAt(i + 1);
179:
180: if (ch == '{') {
181: cb.append('{');
182: i += 2;
183: } else if (msg.charAt(i + 2) != '}') {
184: cb.append('{');
185: i++;
186: } else if (ch == '0') {
187: cb.append(o1);
188: i += 3;
189: } else if (ch == '1') {
190: cb.append(o2);
191: i += 3;
192: } else {
193: cb.append('{');
194: i++;
195: }
196: }
197: }
198:
199: return cb.close();
200: }
201:
202: public String l(String msg, Object o1, int i2) {
203: return l(msg, o1, String.valueOf(i2));
204: }
205:
206: public String l(String msg, int i1, int i2) {
207: return l(msg, String.valueOf(i1), String.valueOf(i2));
208: }
209:
210: public String l(String msg, Object o1, Object o2, Object o3) {
211: msg = getTranslated(msg);
212:
213: CharBuffer cb = CharBuffer.allocate();
214:
215: int length = msg.length();
216: int i = 0;
217:
218: while (i < length) {
219: char ch = msg.charAt(i);
220:
221: if (ch != '{' || i + 2 >= length) {
222: cb.append(ch);
223: i++;
224: } else {
225: ch = msg.charAt(i + 1);
226:
227: if (ch == '{') {
228: cb.append('{');
229: i += 2;
230: } else if (msg.charAt(i + 2) != '}') {
231: cb.append('{');
232: i++;
233: } else if (ch == '0') {
234: cb.append(o1);
235: i += 3;
236: } else if (ch == '1') {
237: cb.append(o2);
238: i += 3;
239: } else if (ch == '2') {
240: cb.append(o3);
241: i += 3;
242: } else {
243: cb.append('{');
244: i++;
245: }
246: }
247: }
248:
249: return cb.close();
250: }
251:
252: public String l(String msg, Object o1, Object o2, Object o3,
253: Object o4) {
254: msg = getTranslated(msg);
255:
256: CharBuffer cb = CharBuffer.allocate();
257:
258: int length = msg.length();
259: int i = 0;
260:
261: while (i < length) {
262: char ch = msg.charAt(i);
263:
264: if (ch != '{' || i + 2 >= length) {
265: cb.append(ch);
266: i++;
267: } else {
268: ch = msg.charAt(i + 1);
269:
270: if (ch == '{') {
271: cb.append('{');
272: i += 2;
273: } else if (msg.charAt(i + 2) != '}') {
274: cb.append('{');
275: i++;
276: } else if (ch == '0') {
277: cb.append(o1);
278: i += 3;
279: } else if (ch == '1') {
280: cb.append(o2);
281: i += 3;
282: } else if (ch == '2') {
283: cb.append(o3);
284: i += 3;
285: } else if (ch == '3') {
286: cb.append(o4);
287: i += 3;
288: } else {
289: cb.append('{');
290: i++;
291: }
292: }
293: }
294:
295: return cb.close();
296: }
297:
298: public String l(String msg, Object o1, Object o2, Object o3,
299: Object o4, Object o5) {
300: msg = getTranslated(msg);
301:
302: CharBuffer cb = CharBuffer.allocate();
303:
304: int length = msg.length();
305: int i = 0;
306:
307: while (i < length) {
308: char ch = msg.charAt(i);
309:
310: if (ch != '{' || i + 2 >= length) {
311: cb.append(ch);
312: i++;
313: } else {
314: ch = msg.charAt(i + 1);
315:
316: if (ch == '{') {
317: cb.append('{');
318: i += 2;
319: } else if (msg.charAt(i + 2) != '}') {
320: cb.append('{');
321: i++;
322: } else if (ch == '0') {
323: cb.append(o1);
324: i += 3;
325: } else if (ch == '1') {
326: cb.append(o2);
327: i += 3;
328: } else if (ch == '2') {
329: cb.append(o3);
330: i += 3;
331: } else if (ch == '3') {
332: cb.append(o4);
333: i += 3;
334: } else if (ch == '4') {
335: cb.append(o5);
336: i += 3;
337: } else {
338: cb.append('{');
339: i++;
340: }
341: }
342: }
343:
344: return cb.close();
345: }
346:
347: public String l(String msg, Object o1, Object o2, Object o3,
348: Object o4, Object o5, Object o6) {
349: msg = getTranslated(msg);
350:
351: CharBuffer cb = CharBuffer.allocate();
352:
353: int length = msg.length();
354: int i = 0;
355:
356: while (i < length) {
357: char ch = msg.charAt(i);
358:
359: if (ch != '{' || i + 2 >= length) {
360: cb.append(ch);
361: i++;
362: } else {
363: ch = msg.charAt(i + 1);
364:
365: if (ch == '{') {
366: cb.append('{');
367: i += 2;
368: } else if (msg.charAt(i + 2) != '}') {
369: cb.append('{');
370: i++;
371: } else if (ch == '0') {
372: cb.append(o1);
373: i += 3;
374: } else if (ch == '1') {
375: cb.append(o2);
376: i += 3;
377: } else if (ch == '2') {
378: cb.append(o3);
379: i += 3;
380: } else if (ch == '3') {
381: cb.append(o4);
382: i += 3;
383: } else if (ch == '4') {
384: cb.append(o5);
385: i += 3;
386: } else if (ch == '5') {
387: cb.append(o6);
388: i += 3;
389: } else {
390: cb.append('{');
391: i++;
392: }
393: }
394: }
395:
396: return cb.close();
397: }
398:
399: private String getTranslated(String msg) {
400: if (messages == null)
401: return msg;
402:
403: String translated = (String) messages.get(msg);
404:
405: if (translated == null) {
406: return msg;
407: } else
408: return translated;
409: }
410:
411: private Logger log() {
412: if (_log == null)
413: _log = Log.open(L10N.class);
414:
415: return _log;
416: }
417: }
|