This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Messages.java
227 lines (200 loc) · 7.34 KB
/
Messages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright 2010-2015, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.i18n;
import java.text.MessageFormat;
import java.util.AbstractMap;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
import org.zanata.util.EmptyEnumeration;
import javax.annotation.Nonnull;
import static org.jboss.seam.ScopeType.EVENT;
/**
* Utility component to help with programmatic access to the message resource
* bundle.
*
* Unlike the {@link org.jboss.seam.international.Messages} component, this
* component formats messages using positional arguments like {0} and
* {1}, not by interpolating EL expressions.
*
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
* @author Sean Flanigan <a
* href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
@AutoCreate
@Name("msgs")
@Scope(EVENT)
public class Messages extends AbstractMap<String, String> {
/**
* Gets the 'messages' ResourceBundle for the locale of the current
* request, if any, otherwise server's default locale.
* @see org.jboss.seam.web.Locale
*/
private static ResourceBundle getResourceBundle() {
return getResourceBundle(org.jboss.seam.core.Locale.instance());
}
/**
* Gets the 'messages' ResourceBundle for the specified locale.
*/
private static ResourceBundle getResourceBundle(java.util.Locale locale) {
// Generic ResourceBundle without built-in interpolation:
ResourceBundle resourceBundle = null;
try {
resourceBundle = ResourceBundle.getBundle(
"messages", locale);
} catch (MissingResourceException e) {
resourceBundle = new ResourceBundle() {
@Override
protected Object handleGetObject(@Nonnull String key) {
return key;
}
@Override
@Nonnull
public Enumeration<String> getKeys() {
return EmptyEnumeration.instance();
}
};
}
return resourceBundle;
}
private final ResourceBundle resourceBundle;
/**
* Create an instance for the locale of the current request, if any,
* otherwise the server's default locale.
*/
public Messages() {
this(getResourceBundle());
}
/**
* Create an instance for the specified locale.
*/
public Messages(java.util.Locale locale) {
this(getResourceBundle(locale));
}
private Messages(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
}
@Observer("org.jboss.seam.localeSelected")
public void changeLocale(String localeString) {
// we need to refresh the bean
// see org.jboss.seam.international.LocaleSelector.select()
Contexts.removeFromAllContexts("msgs");
}
// the default toString includes the entire list of properties,
// which makes a mess of the log file
@Override
public String toString() {
return getClass().getName();
}
/**
* Gets a resource string, without any message formatting. (So an
* apostrophe just represents an apostrophe (single quote).)
* @param key
* @return
*/
@Override
public String get(Object key) {
if (key instanceof String) {
String resourceKey = (String) key;
try {
String resource = resourceBundle.getString(resourceKey);
return (resource == null) ? resourceKey : resource;
} catch (MissingResourceException mre) {
return resourceKey;
}
} else {
return null;
}
}
/**
* @deprecated use get(key) or format(key, args...)
* @param key
* @return
*/
@Deprecated
public String format(String key) {
return format(key, new Object[0]);
}
/**
* Gets a resource string, and formats it using MessageFormat and the
* positional parameters. Due to the use of {@link java.util.MessageFormat}
* any literal apostrophes (single quotes) will need to be doubled,
* otherwise they will be interpreted as quoting format patterns.
* @param key
* @param args
* @return
* @see java.util.MessageFormat
*/
public String format(String key, Object... args) {
String template = get(key);
return MessageFormat.format(template, args);
}
// JSF can't handle varargs, hence the need for these overloaded methods:
public String format(String key, Object arg1) {
return format(key, new Object[] {arg1});
}
public String format(String key, Object arg1, Object arg2) {
return format(key, new Object[] {arg1, arg2});
}
public String format(String key, Object arg1, Object arg2, Object arg3) {
return format(key, new Object[] {arg1, arg2, arg3});
}
public String format(String key, Object arg1, Object arg2, Object arg3, Object arg4) {
return format(key, new Object[] {arg1, arg2, arg3, arg4});
}
public String format(String key, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) {
return format(key, new Object[] {arg1, arg2, arg3, arg4, arg5});
}
public String format(String key, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) {
return format(key, new Object[] {arg1, arg2, arg3, arg4, arg5, arg6});
}
@Override
public Set<Map.Entry<String, String>> entrySet() {
Set<Map.Entry<String, String>> entrySet =
new HashSet<Entry<String, String>>();
for (final String key : resourceBundle.keySet()) {
entrySet.add(new Map.Entry<String, String>() {
@Override
public String getKey() {
return key;
}
@Override
public String getValue() {
return get(key);
}
@Override
public String setValue(String val) {
throw new UnsupportedOperationException();
}
});
}
return entrySet;
}
}