01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.i18n;
19:
20: import java.text.MessageFormat;
21: import java.util.Locale;
22: import java.util.MissingResourceException;
23:
24: import de.finix.contelligent.core.ContelligentImpl;
25: import de.finix.contelligent.exception.ContelligentException;
26: import de.finix.contelligent.exception.ContelligentRuntimeException;
27: import de.finix.contelligent.logging.LoggingService;
28:
29: public abstract class LocalizedMessage {
30:
31: static final org.apache.log4j.Logger log = LoggingService
32: .getLogger(LocalizedMessage.class);
33:
34: public static final String getMessage(String id, Object[] info,
35: Locale locale) {
36: try {
37: String message = ContelligentImpl.getInstance()
38: .getLocalizedString(id, locale);
39:
40: if (info == null) {
41: return message;
42: } else {
43: Object[] localizedInfo = new Object[info.length];
44: for (int i = 0; i < info.length; i++) {
45: if (info[i] instanceof LocalizedObject) {
46: localizedInfo[i] = ((LocalizedObject) info[i])
47: .getLocalizedMessage(locale);
48: } else {
49: localizedInfo[i] = info[i];
50: }
51: }
52: return MessageFormat.format(message, info);
53: }
54: } catch (MissingResourceException e) {
55: if (log.isDebugEnabled()) {
56: log.debug("No resource for id " + id + " (locale="
57: + locale + ")");
58: }
59: return null;
60: }
61: }
62:
63: public static String getShortMessage(ContelligentException e,
64: Locale locale) {
65: return getMessage(e.getMessage() + ".short", e.getInfo(),
66: locale);
67: }
68:
69: public static String getLongMessage(ContelligentException e,
70: Locale locale) {
71: return getMessage(e.getMessage() + ".long", e.getInfo(), locale);
72: }
73:
74: public static String getShortMessage(
75: ContelligentRuntimeException e, Locale locale) {
76: return getMessage(e.getMessage() + ".short", e.getInfo(),
77: locale);
78: }
79:
80: public static String getLongMessage(ContelligentRuntimeException e,
81: Locale locale) {
82: return getMessage(e.getMessage() + ".long", e.getInfo(), locale);
83: }
84: }
|