KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > i18n > AbstractMessagesImpl


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13
14 package info.magnolia.cms.i18n;
15
16 import java.text.MessageFormat JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25 /**
26  * Provides localized strings. You should uses the ContextMessages class if you can provide a request object.
27  * AbstractMessagesImpl will do the job as good as possible without to know the session (user) and all the other
28  * contextual things. Endusers will use the MessageManager to resolve messages.
29  * @author Philipp Bracher
30  */

31
32 public abstract class AbstractMessagesImpl implements Messages {
33
34     protected Logger log = LoggerFactory.getLogger(AbstractMessagesImpl.class);
35
36     /**
37      * Name of the javascript object used to make the messages public to the javascripts
38      */

39     public static final String JavaDoc JS_OBJECTNAME = "mgnlMessages"; //$NON-NLS-1$
40

41     /**
42      * The name of the bundle
43      */

44     protected String JavaDoc basename = MessagesManager.DEFAULT_BASENAME;
45
46     /**
47      * The current locale
48      */

49     protected Locale JavaDoc locale;
50
51     /**
52      * The current bundle. Subclasses will overwrite getBundle()
53      */

54     protected ResourceBundle JavaDoc bundle;
55
56     /**
57      * @param basename name of the bundle
58      * @param locale the locale
59      */

60     protected AbstractMessagesImpl(String JavaDoc basename, Locale JavaDoc locale) {
61         this.locale = locale;
62         this.basename = basename;
63     }
64
65     /**
66      * @return current locale
67      */

68     public Locale JavaDoc getLocale() {
69         return locale;
70     }
71
72     /**
73      * If no basename is provided this method returns DEFAULT_BASENAME
74      * @return current basename
75      */

76     public String JavaDoc getBasename() {
77         return basename;
78     }
79
80     /**
81      * Replace the parameters in the string: the entered text {0} is not a valid email
82      * @param key the key
83      * @param args the replacement strings
84      * @return message
85      */

86     public String JavaDoc get(String JavaDoc key, Object JavaDoc[] args) {
87         return MessageFormat.format(get(key), args);
88     }
89
90     /**
91      * You can provide a default value if the key is not found
92      * @param key key
93      * @param defaultMsg the default message
94      * @return the message
95      */

96     public String JavaDoc getWithDefault(String JavaDoc key, String JavaDoc defaultMsg) {
97         String JavaDoc msg = get(key);
98         if (msg.startsWith("???")) { //$NON-NLS-1$
99
msg = defaultMsg;
100         }
101         return msg;
102     }
103
104     /**
105      * With default value and replacement strings
106      * @param key key
107      * @param args replacement strings
108      * @param defaultMsg default message
109      * @return message
110      */

111     public String JavaDoc getWithDefault(String JavaDoc key, Object JavaDoc[] args, String JavaDoc defaultMsg) {
112         return MessageFormat.format(getWithDefault(key, defaultMsg), args);
113     }
114
115     /**
116      * True if the basename and the locale are the same
117      */

118     public boolean equals(Object JavaDoc arg0) {
119         return StringUtils.equals(((AbstractMessagesImpl) arg0).basename, this.basename)
120             && this.locale.equals(((AbstractMessagesImpl) arg0).locale);
121     }
122
123     /**
124      * Nice string
125      */

126     public String JavaDoc toString() {
127         return this.basename + "(" + this.locale.toString() + ")";
128     }
129
130 }
Popular Tags