16
16
* limitations under the License.
17
17
*/
18
18
19
- import java .lang .reflect .Field ;
19
+ import javax .inject .Named ;
20
+ import javax .inject .Singleton ;
21
+
20
22
import java .text .MessageFormat ;
21
23
import java .util .HashMap ;
22
24
import java .util .Locale ;
23
25
import java .util .Map ;
24
26
import java .util .MissingResourceException ;
25
27
import java .util .ResourceBundle ;
26
28
27
- import org .codehaus .plexus .component .annotations .Component ;
28
- import org .codehaus .plexus .logging .AbstractLogEnabled ;
29
- import org .codehaus .plexus .personality .plexus .lifecycle .phase .Initializable ;
30
- import org .codehaus .plexus .personality .plexus .lifecycle .phase .InitializationException ;
31
- import org .codehaus .plexus .util .StringUtils ;
29
+ import org .slf4j .Logger ;
30
+ import org .slf4j .LoggerFactory ;
32
31
33
32
/**
34
33
*
35
34
*/
36
- @ Component (role = I18N .class )
37
- public class DefaultI18N extends AbstractLogEnabled implements I18N , Initializable {
35
+ @ Named
36
+ @ Singleton
37
+ public class DefaultI18N implements I18N {
38
+
39
+ private final Logger log = LoggerFactory .getLogger (DefaultI18N .class );
38
40
private static final Object [] NO_ARGS = new Object [0 ];
39
41
40
- private HashMap bundles ;
42
+ private Map < String , Map < Locale , ResourceBundle >> bundles ;
41
43
42
44
private String [] bundleNames ;
43
45
44
46
private String defaultBundleName ;
45
47
46
- private Locale defaultLocale = Locale .getDefault ();
47
-
48
- private String defaultLanguage = Locale .getDefault ().getLanguage ();
49
-
50
- private String defaultCountry = Locale .getDefault ().getCountry ();
51
-
52
48
private boolean devMode ;
53
49
50
+ public DefaultI18N () {
51
+ initialize ();
52
+ }
53
+
54
+ public DefaultI18N (String [] bundleNames ) {
55
+ this .bundleNames = bundleNames != null ? bundleNames .clone () : new String [0 ];
56
+ initialize ();
57
+ }
54
58
// ----------------------------------------------------------------------
55
59
// Accessors
56
60
// ----------------------------------------------------------------------
57
61
58
62
public String getDefaultLanguage () {
59
- return defaultLanguage ;
63
+ return Locale . getDefault (). getLanguage () ;
60
64
}
61
65
62
66
public String getDefaultCountry () {
63
- return defaultCountry ;
67
+ return Locale . getDefault (). getCountry () ;
64
68
}
65
69
66
70
public String getDefaultBundleName () {
67
71
return defaultBundleName ;
68
72
}
69
73
70
74
public String [] getBundleNames () {
71
- return ( String []) bundleNames .clone ();
75
+ return bundleNames .clone ();
72
76
}
73
77
74
78
public ResourceBundle getBundle () {
@@ -111,25 +115,7 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
111
115
// ----------------------------------------------------------------------
112
116
113
117
if (devMode ) {
114
- try {
115
- Class klass = ResourceBundle .getBundle (bundleName ).getClass ().getSuperclass ();
116
-
117
- Field field = klass .getDeclaredField ("cacheList" );
118
-
119
- field .setAccessible (true );
120
-
121
- // SoftCache cache = (SoftCache) field.get( null );
122
- //
123
- // cache.clear();
124
-
125
- Object cache = field .get (null );
126
-
127
- cache .getClass ().getDeclaredMethod ("clear" , null ).invoke (cache , null );
128
-
129
- field .setAccessible (false );
130
- } catch (Exception e ) {
131
- // Intentional
132
- }
118
+ ResourceBundle .clearCache ();
133
119
}
134
120
135
121
if (locale == null ) {
@@ -139,13 +125,12 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
139
125
// Find/retrieve/cache bundle.
140
126
ResourceBundle rb ;
141
127
142
- HashMap bundlesByLocale = ( HashMap ) bundles .get (bundleName );
128
+ Map < Locale , ResourceBundle > bundlesByLocale = bundles .get (bundleName );
143
129
144
130
if (bundlesByLocale != null ) {
145
131
// Cache of bundles by locale for the named bundle exists.
146
132
// Check the cache for a bundle corresponding to locale.
147
- rb = (ResourceBundle ) bundlesByLocale .get (locale );
148
-
133
+ rb = bundlesByLocale .get (locale );
149
134
if (rb == null ) {
150
135
// Not yet cached.
151
136
rb = cacheBundle (bundleName , locale );
@@ -161,16 +146,15 @@ public ResourceBundle getBundle(String bundleName, Locale locale) {
161
146
* @see I18N#getLocale(String)
162
147
*/
163
148
public Locale getLocale (String header ) {
164
- if (! StringUtils .isEmpty (header )) {
149
+ if (header != null && ! header .isEmpty ()) {
165
150
I18NTokenizer tok = new I18NTokenizer (header );
166
-
167
151
if (tok .hasNext ()) {
168
- return ( Locale ) tok .next ();
152
+ return tok .next ();
169
153
}
170
154
}
171
155
172
156
// Couldn't parse locale.
173
- return defaultLocale ;
157
+ return Locale . getDefault () ;
174
158
}
175
159
176
160
public String getString (String key ) {
@@ -187,7 +171,6 @@ public String getString(String key, Locale locale) {
187
171
*/
188
172
public String getString (String bundleName , Locale locale , String key ) {
189
173
String value ;
190
-
191
174
if (locale == null ) {
192
175
locale = getLocale (null );
193
176
}
@@ -198,11 +181,8 @@ public String getString(String bundleName, Locale locale, String key) {
198
181
value = getStringOrNull (rb , key );
199
182
200
183
// Look for text in list of default bundles.
201
- if (value == null && bundleNames .length > 0 ) {
202
- String name ;
203
- for (int i = 0 ; i < bundleNames .length ; i ++) {
204
- name = bundleNames [i ];
205
-
184
+ if (value == null ) {
185
+ for (String name : bundleNames ) {
206
186
if (!name .equals (bundleName )) {
207
187
rb = getBundle (name , locale );
208
188
@@ -218,27 +198,20 @@ public String getString(String bundleName, Locale locale, String key) {
218
198
}
219
199
220
200
if (value == null ) {
221
- String loc = locale .toString ();
222
-
223
- String mesg =
224
- "Noticed missing resource: " + "bundleName=" + bundleName + ", locale=" + loc + ", key=" + key ;
225
-
226
- getLogger ().debug (mesg );
227
-
201
+ log .debug ("Noticed missing resource: bundleName={}, locale={}, key={}" , bundleName , locale , key );
228
202
// Just send back the key, we don't need to throw an exception.
229
-
230
203
value = key ;
231
204
}
232
205
233
206
return value ;
234
207
}
235
208
236
209
public String format (String key , Object arg1 ) {
237
- return format (defaultBundleName , defaultLocale , key , new Object [] {arg1 });
210
+ return format (defaultBundleName , Locale . getDefault () , key , new Object [] {arg1 });
238
211
}
239
212
240
213
public String format (String key , Object arg1 , Object arg2 ) {
241
- return format (defaultBundleName , defaultLocale , key , new Object [] {arg1 , arg2 });
214
+ return format (defaultBundleName , Locale . getDefault () , key , new Object [] {arg1 , arg2 });
242
215
}
243
216
244
217
/**
@@ -275,28 +248,15 @@ public String format(String bundleName, Locale locale, String key, Object[] args
275
248
if (args == null ) {
276
249
args = NO_ARGS ;
277
250
}
278
-
279
- // FIXME: after switching to JDK 1.4, it will be possible to clean
280
- // this up by providing the Locale along with the string in the
281
- // constructor to MessageFormat. Until 1.4, the following workaround
282
- // is required for constructing the format with the appropriate locale:
283
- MessageFormat messageFormat = new MessageFormat ("" );
284
- messageFormat .setLocale (locale );
285
- messageFormat .applyPattern (value );
286
-
287
- return messageFormat .format (args );
251
+ return new MessageFormat (value , locale ).format (args );
288
252
}
289
253
290
254
/**
291
255
* Called the first time the Service is used.
292
256
*/
293
- public void initialize () throws InitializationException {
294
- bundles = new HashMap ();
295
-
296
- defaultLocale = new Locale (defaultLanguage , defaultCountry );
297
-
257
+ public void initialize () {
258
+ bundles = new HashMap <>();
298
259
initializeBundleNames ();
299
-
300
260
if ("true" .equals (System .getProperty ("PLEXUS_DEV_MODE" ))) {
301
261
devMode = true ;
302
262
}
@@ -307,8 +267,7 @@ public void initialize() throws InitializationException {
307
267
// ----------------------------------------------------------------------
308
268
309
269
protected void initializeBundleNames () {
310
- // System.err.println("cfg=" + getConfiguration());
311
- if (defaultBundleName != null && defaultBundleName .length () > 0 ) {
270
+ if (defaultBundleName != null && !defaultBundleName .isEmpty ()) {
312
271
// Using old-style single bundle name property.
313
272
if (bundleNames == null || bundleNames .length <= 0 ) {
314
273
bundleNames = new String [] {defaultBundleName };
@@ -333,18 +292,15 @@ protected void initializeBundleNames() {
333
292
* @throws MissingResourceException Bundle not found.
334
293
*/
335
294
private synchronized ResourceBundle cacheBundle (String bundleName , Locale locale ) throws MissingResourceException {
336
- HashMap bundlesByLocale = (HashMap ) bundles .get (bundleName );
337
-
338
- ResourceBundle rb = (bundlesByLocale == null ? null : (ResourceBundle ) bundlesByLocale .get (locale ));
295
+ Map <Locale , ResourceBundle > bundlesByLocale = bundles .get (bundleName );
339
296
297
+ ResourceBundle rb = (bundlesByLocale == null ? null : bundlesByLocale .get (locale ));
340
298
if (rb == null ) {
341
- bundlesByLocale = (bundlesByLocale == null ? new HashMap (3 ) : new HashMap (bundlesByLocale ));
342
-
299
+ bundlesByLocale = (bundlesByLocale == null ? new HashMap <>(3 ) : new HashMap <>(bundlesByLocale ));
343
300
try {
344
301
rb = ResourceBundle .getBundle (bundleName , locale );
345
302
} catch (MissingResourceException e ) {
346
303
rb = findBundleByLocale (bundleName , locale , bundlesByLocale );
347
-
348
304
if (rb == null ) {
349
305
throw (MissingResourceException ) e .fillInStackTrace ();
350
306
}
@@ -353,11 +309,8 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
353
309
if (rb != null ) {
354
310
// Cache bundle.
355
311
bundlesByLocale .put (rb .getLocale (), rb );
356
-
357
- HashMap bundlesByName = new HashMap (bundles );
358
-
312
+ Map <String , Map <Locale , ResourceBundle >> bundlesByName = new HashMap <>(bundles );
359
313
bundlesByName .put (bundleName , bundlesByLocale );
360
-
361
314
this .bundles = bundlesByName ;
362
315
}
363
316
}
@@ -380,30 +333,31 @@ private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale
380
333
* <p>Since we're really just guessing at possible bundles to use,
381
334
* we don't ever throw <code>MissingResourceException</code>.</p>
382
335
*/
383
- private ResourceBundle findBundleByLocale (String bundleName , Locale locale , Map bundlesByLocale ) {
336
+ private ResourceBundle findBundleByLocale (
337
+ String bundleName , Locale locale , Map <Locale , ResourceBundle > bundlesByLocale ) {
384
338
ResourceBundle rb = null ;
385
339
386
- if (!StringUtils .isNotEmpty (locale .getCountry ()) && defaultLanguage .equals (locale .getLanguage ())) {
387
- /*
388
- category.debug("Requested language '" + locale.getLanguage() +
389
- "' matches default: Attempting to guess bundle " +
390
- "using default country '" + defaultCountry + '\'');
391
- */
392
- Locale withDefaultCountry = new Locale (locale .getLanguage (), defaultCountry );
393
- rb = (ResourceBundle ) bundlesByLocale .get (withDefaultCountry );
340
+ if (locale .getCountry () != null
341
+ && !locale .getCountry ().isEmpty ()
342
+ && Locale .getDefault ().getLanguage ().equals (locale .getLanguage ())) {
343
+ Locale withDefaultCountry =
344
+ new Locale (locale .getLanguage (), Locale .getDefault ().getCountry ());
345
+ rb = bundlesByLocale .get (withDefaultCountry );
394
346
if (rb == null ) {
395
347
rb = getBundleIgnoreException (bundleName , withDefaultCountry );
396
348
}
397
- } else if (!StringUtils .isNotEmpty (locale .getLanguage ()) && defaultCountry .equals (locale .getCountry ())) {
398
- Locale withDefaultLanguage = new Locale (defaultLanguage , locale .getCountry ());
399
- rb = (ResourceBundle ) bundlesByLocale .get (withDefaultLanguage );
349
+ } else if (locale .getLanguage () != null
350
+ && !locale .getLanguage ().isEmpty ()
351
+ && Locale .getDefault ().getCountry ().equals (locale .getCountry ())) {
352
+ Locale withDefaultLanguage = new Locale (Locale .getDefault ().getLanguage (), locale .getCountry ());
353
+ rb = bundlesByLocale .get (withDefaultLanguage );
400
354
if (rb == null ) {
401
355
rb = getBundleIgnoreException (bundleName , withDefaultLanguage );
402
356
}
403
357
}
404
358
405
- if (rb == null && !defaultLocale .equals (locale )) {
406
- rb = getBundleIgnoreException (bundleName , defaultLocale );
359
+ if (rb == null && !Locale . getDefault () .equals (locale )) {
360
+ rb = getBundleIgnoreException (bundleName , Locale . getDefault () );
407
361
}
408
362
409
363
return rb ;
0 commit comments