| 1 | |
package org.codehaus.plexus.i18n; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
| 20 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; |
| 21 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; |
| 22 | |
import org.codehaus.plexus.util.StringUtils; |
| 23 | |
|
| 24 | |
import java.text.MessageFormat; |
| 25 | |
import java.util.HashMap; |
| 26 | |
import java.util.Locale; |
| 27 | |
import java.util.Map; |
| 28 | |
import java.util.MissingResourceException; |
| 29 | |
import java.util.ResourceBundle; |
| 30 | |
import java.lang.reflect.Field; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 3 | public class DefaultI18N |
| 37 | |
extends AbstractLogEnabled |
| 38 | |
implements I18N, Initializable |
| 39 | |
{ |
| 40 | 1 | private static final Object[] NO_ARGS = new Object[0]; |
| 41 | |
|
| 42 | |
private HashMap bundles; |
| 43 | |
|
| 44 | |
private String[] bundleNames; |
| 45 | |
|
| 46 | |
private String defaultBundleName; |
| 47 | |
|
| 48 | 3 | private Locale defaultLocale = Locale.getDefault(); |
| 49 | |
|
| 50 | 3 | private String defaultLanguage = Locale.getDefault().getLanguage(); |
| 51 | |
|
| 52 | 3 | private String defaultCountry = Locale.getDefault().getCountry(); |
| 53 | |
|
| 54 | |
private boolean devMode; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public String getDefaultLanguage() |
| 61 | |
{ |
| 62 | 0 | return defaultLanguage; |
| 63 | |
} |
| 64 | |
|
| 65 | |
public String getDefaultCountry() |
| 66 | |
{ |
| 67 | 0 | return defaultCountry; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public String getDefaultBundleName() |
| 71 | |
{ |
| 72 | 3 | return defaultBundleName; |
| 73 | |
} |
| 74 | |
|
| 75 | |
public String[] getBundleNames() |
| 76 | |
{ |
| 77 | 0 | return (String[]) bundleNames.clone(); |
| 78 | |
} |
| 79 | |
|
| 80 | |
public ResourceBundle getBundle() |
| 81 | |
{ |
| 82 | 0 | return getBundle( getDefaultBundleName(), (Locale) null ); |
| 83 | |
} |
| 84 | |
|
| 85 | |
public ResourceBundle getBundle( String bundleName ) |
| 86 | |
{ |
| 87 | 0 | return getBundle( bundleName, (Locale) null ); |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
public ResourceBundle getBundle( String bundleName, String languageHeader ) |
| 100 | |
{ |
| 101 | 0 | return getBundle( bundleName, getLocale( languageHeader ) ); |
| 102 | |
} |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
public ResourceBundle getBundle( String bundleName, Locale locale ) |
| 115 | |
{ |
| 116 | |
|
| 117 | 13 | bundleName = ( bundleName == null ? getDefaultBundleName() : bundleName.trim() ); |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | 13 | if ( devMode ) |
| 124 | |
{ |
| 125 | |
try |
| 126 | |
{ |
| 127 | 0 | Class klass = ResourceBundle.getBundle( bundleName ).getClass().getSuperclass(); |
| 128 | |
|
| 129 | 0 | Field field = klass.getDeclaredField( "cacheList" ); |
| 130 | |
|
| 131 | 0 | field.setAccessible( true ); |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | Object cache = field.get( null ); |
| 138 | |
|
| 139 | 0 | cache.getClass().getDeclaredMethod( "clear", null ).invoke( cache, null ); |
| 140 | |
|
| 141 | 0 | field.setAccessible( false ); |
| 142 | |
} |
| 143 | 0 | catch ( Exception e ) |
| 144 | |
{ |
| 145 | |
|
| 146 | 0 | } |
| 147 | |
} |
| 148 | |
|
| 149 | 13 | if ( locale == null ) |
| 150 | |
{ |
| 151 | 0 | locale = getLocale( null ); |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | |
ResourceBundle rb; |
| 156 | |
|
| 157 | 13 | HashMap bundlesByLocale = (HashMap) bundles.get( bundleName ); |
| 158 | |
|
| 159 | 13 | if ( bundlesByLocale != null ) |
| 160 | |
{ |
| 161 | |
|
| 162 | |
|
| 163 | 8 | rb = (ResourceBundle) bundlesByLocale.get( locale ); |
| 164 | |
|
| 165 | 8 | if ( rb == null ) |
| 166 | |
{ |
| 167 | |
|
| 168 | 8 | rb = cacheBundle( bundleName, locale ); |
| 169 | |
} |
| 170 | |
} |
| 171 | |
else |
| 172 | |
{ |
| 173 | 5 | rb = cacheBundle( bundleName, locale ); |
| 174 | |
} |
| 175 | |
|
| 176 | 13 | return rb; |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
public Locale getLocale( String header ) |
| 183 | |
{ |
| 184 | 5 | if ( !StringUtils.isEmpty( header ) ) |
| 185 | |
{ |
| 186 | 0 | I18NTokenizer tok = new I18NTokenizer( header ); |
| 187 | |
|
| 188 | 0 | if ( tok.hasNext() ) |
| 189 | |
{ |
| 190 | 0 | return (Locale) tok.next(); |
| 191 | |
} |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | 5 | return defaultLocale; |
| 196 | |
} |
| 197 | |
|
| 198 | |
public String getString( String key ) |
| 199 | |
{ |
| 200 | 0 | return getString( key, null ); |
| 201 | |
} |
| 202 | |
|
| 203 | |
public String getString( String key, Locale locale ) |
| 204 | |
{ |
| 205 | 1 | return getString( getDefaultBundleName(), locale, key ); |
| 206 | |
} |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
public String getString( String bundleName, Locale locale, String key ) |
| 213 | |
{ |
| 214 | |
String value; |
| 215 | |
|
| 216 | 13 | if ( locale == null ) |
| 217 | |
{ |
| 218 | 2 | locale = getLocale( null ); |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | 13 | ResourceBundle rb = getBundle( bundleName, locale ); |
| 223 | |
|
| 224 | 13 | value = getStringOrNull( rb, key ); |
| 225 | |
|
| 226 | |
|
| 227 | 13 | if ( value == null && bundleNames.length > 0 ) |
| 228 | |
{ |
| 229 | |
String name; |
| 230 | 0 | for ( int i = 0; i < bundleNames.length; i++ ) |
| 231 | |
{ |
| 232 | 0 | name = bundleNames[i]; |
| 233 | |
|
| 234 | 0 | if ( !name.equals( bundleName ) ) |
| 235 | |
{ |
| 236 | 0 | rb = getBundle( name, locale ); |
| 237 | |
|
| 238 | 0 | value = getStringOrNull( rb, key ); |
| 239 | |
|
| 240 | 0 | if ( value != null ) |
| 241 | |
{ |
| 242 | 0 | locale = rb.getLocale(); |
| 243 | |
|
| 244 | 0 | break; |
| 245 | |
} |
| 246 | |
} |
| 247 | |
} |
| 248 | |
} |
| 249 | |
|
| 250 | 13 | if ( value == null ) |
| 251 | |
{ |
| 252 | 0 | String loc = locale.toString(); |
| 253 | |
|
| 254 | 0 | String mesg = "Noticed missing resource: " + "bundleName=" + bundleName + ", locale=" + loc + ", key=" + key; |
| 255 | |
|
| 256 | 0 | getLogger().debug( mesg ); |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | 0 | value = key; |
| 261 | |
} |
| 262 | |
|
| 263 | 13 | return value; |
| 264 | |
} |
| 265 | |
|
| 266 | |
public String format( String key, Object arg1 ) |
| 267 | |
{ |
| 268 | 0 | return format( defaultBundleName, defaultLocale, key, new Object[]{arg1} ); |
| 269 | |
} |
| 270 | |
|
| 271 | |
public String format( String key, Object arg1, Object arg2 ) |
| 272 | |
{ |
| 273 | 0 | return format( defaultBundleName, defaultLocale, key, new Object[]{arg1, arg2} ); |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public String format( String bundleName, |
| 280 | |
Locale locale, |
| 281 | |
String key, |
| 282 | |
Object arg1 ) |
| 283 | |
{ |
| 284 | 1 | return format( bundleName, locale, key, new Object[]{arg1} ); |
| 285 | |
} |
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
public String format( String bundleName, |
| 291 | |
Locale locale, |
| 292 | |
String key, |
| 293 | |
Object arg1, |
| 294 | |
Object arg2 ) |
| 295 | |
{ |
| 296 | 1 | return format( bundleName, locale, key, new Object[]{arg1, arg2} ); |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
public String format( String bundleName, Locale locale, String key, Object[] args ) |
| 309 | |
{ |
| 310 | 3 | if ( locale == null ) |
| 311 | |
{ |
| 312 | |
|
| 313 | |
|
| 314 | 3 | locale = getLocale( null ); |
| 315 | |
} |
| 316 | |
|
| 317 | 3 | String value = getString( bundleName, locale, key ); |
| 318 | 3 | if ( args == null ) |
| 319 | |
{ |
| 320 | 0 | args = NO_ARGS; |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | 3 | MessageFormat messageFormat = new MessageFormat( "" ); |
| 328 | 3 | messageFormat.setLocale( locale ); |
| 329 | 3 | messageFormat.applyPattern( value ); |
| 330 | |
|
| 331 | 3 | return messageFormat.format( args ); |
| 332 | |
} |
| 333 | |
|
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
public void initialize() |
| 339 | |
throws InitializationException |
| 340 | |
{ |
| 341 | 3 | bundles = new HashMap(); |
| 342 | |
|
| 343 | 3 | defaultLocale = new Locale( defaultLanguage, defaultCountry ); |
| 344 | |
|
| 345 | 3 | initializeBundleNames(); |
| 346 | |
|
| 347 | 3 | if ( "true".equals( System.getProperty( "PLEXUS_DEV_MODE" ) ) ) |
| 348 | |
{ |
| 349 | 0 | devMode = true; |
| 350 | |
} |
| 351 | 3 | } |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
protected void initializeBundleNames() |
| 358 | |
{ |
| 359 | |
|
| 360 | 3 | if ( defaultBundleName != null && defaultBundleName.length() > 0 ) |
| 361 | |
{ |
| 362 | |
|
| 363 | 3 | if ( bundleNames == null || bundleNames.length <= 0 ) |
| 364 | |
{ |
| 365 | 3 | bundleNames = new String[]{defaultBundleName}; |
| 366 | |
} |
| 367 | |
else |
| 368 | |
{ |
| 369 | |
|
| 370 | 0 | String[] array = new String[bundleNames.length + 1]; |
| 371 | 0 | array[0] = defaultBundleName; |
| 372 | 0 | System.arraycopy( bundleNames, 0, array, 1, bundleNames.length ); |
| 373 | 0 | bundleNames = array; |
| 374 | |
} |
| 375 | |
} |
| 376 | 3 | if ( bundleNames == null ) |
| 377 | |
{ |
| 378 | 0 | bundleNames = new String[0]; |
| 379 | |
} |
| 380 | 3 | } |
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
private synchronized ResourceBundle cacheBundle( String bundleName, Locale locale ) |
| 390 | |
throws MissingResourceException |
| 391 | |
{ |
| 392 | 13 | HashMap bundlesByLocale = (HashMap) bundles.get( bundleName ); |
| 393 | |
|
| 394 | 13 | ResourceBundle rb = ( bundlesByLocale == null ? null : (ResourceBundle) bundlesByLocale.get( locale ) ); |
| 395 | |
|
| 396 | 13 | if ( rb == null ) |
| 397 | |
{ |
| 398 | 13 | bundlesByLocale = ( bundlesByLocale == null ? new HashMap( 3 ) : new HashMap( bundlesByLocale ) ); |
| 399 | |
|
| 400 | |
try |
| 401 | |
{ |
| 402 | 13 | rb = ResourceBundle.getBundle( bundleName, locale ); |
| 403 | |
} |
| 404 | 0 | catch ( MissingResourceException e ) |
| 405 | |
{ |
| 406 | 0 | rb = findBundleByLocale( bundleName, locale, bundlesByLocale ); |
| 407 | |
|
| 408 | 0 | if ( rb == null ) |
| 409 | |
{ |
| 410 | 0 | throw (MissingResourceException) e.fillInStackTrace(); |
| 411 | |
} |
| 412 | 13 | } |
| 413 | |
|
| 414 | 13 | if ( rb != null ) |
| 415 | |
{ |
| 416 | |
|
| 417 | 13 | bundlesByLocale.put( rb.getLocale(), rb ); |
| 418 | |
|
| 419 | 13 | HashMap bundlesByName = new HashMap( bundles ); |
| 420 | |
|
| 421 | 13 | bundlesByName.put( bundleName, bundlesByLocale ); |
| 422 | |
|
| 423 | 13 | this.bundles = bundlesByName; |
| 424 | |
} |
| 425 | |
} |
| 426 | |
|
| 427 | 13 | return rb; |
| 428 | |
} |
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
private ResourceBundle findBundleByLocale( String bundleName, |
| 446 | |
Locale locale, |
| 447 | |
Map bundlesByLocale ) |
| 448 | |
{ |
| 449 | 0 | ResourceBundle rb = null; |
| 450 | |
|
| 451 | 0 | if ( !StringUtils.isNotEmpty( locale.getCountry() ) && |
| 452 | |
defaultLanguage.equals( locale.getLanguage() ) ) |
| 453 | |
{ |
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | 0 | Locale withDefaultCountry = new Locale( locale.getLanguage(), |
| 460 | |
defaultCountry ); |
| 461 | 0 | rb = (ResourceBundle) bundlesByLocale.get( withDefaultCountry ); |
| 462 | 0 | if ( rb == null ) |
| 463 | |
{ |
| 464 | 0 | rb = getBundleIgnoreException( bundleName, withDefaultCountry ); |
| 465 | |
} |
| 466 | 0 | } |
| 467 | 0 | else if ( !StringUtils.isNotEmpty( locale.getLanguage() ) && |
| 468 | |
defaultCountry.equals( locale.getCountry() ) ) |
| 469 | |
{ |
| 470 | 0 | Locale withDefaultLanguage = new Locale( defaultLanguage, |
| 471 | |
locale.getCountry() ); |
| 472 | 0 | rb = (ResourceBundle) bundlesByLocale.get( withDefaultLanguage ); |
| 473 | 0 | if ( rb == null ) |
| 474 | |
{ |
| 475 | 0 | rb = getBundleIgnoreException( bundleName, withDefaultLanguage ); |
| 476 | |
} |
| 477 | |
} |
| 478 | |
|
| 479 | 0 | if ( rb == null && !defaultLocale.equals( locale ) ) |
| 480 | |
{ |
| 481 | 0 | rb = getBundleIgnoreException( bundleName, defaultLocale ); |
| 482 | |
} |
| 483 | |
|
| 484 | 0 | return rb; |
| 485 | |
} |
| 486 | |
|
| 487 | |
|
| 488 | |
|
| 489 | |
|
| 490 | |
|
| 491 | |
|
| 492 | |
|
| 493 | |
private ResourceBundle getBundleIgnoreException( String bundleName, Locale locale ) |
| 494 | |
{ |
| 495 | |
try |
| 496 | |
{ |
| 497 | 0 | return ResourceBundle.getBundle( bundleName, locale ); |
| 498 | |
} |
| 499 | 0 | catch ( MissingResourceException ignored ) |
| 500 | |
{ |
| 501 | 0 | return null; |
| 502 | |
} |
| 503 | |
} |
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
protected final String getStringOrNull( ResourceBundle rb, String key ) |
| 512 | |
{ |
| 513 | 13 | if ( rb != null ) |
| 514 | |
{ |
| 515 | |
try |
| 516 | |
{ |
| 517 | 13 | return rb.getString( key ); |
| 518 | |
} |
| 519 | 0 | catch ( MissingResourceException ignored ) |
| 520 | |
{ |
| 521 | |
|
| 522 | |
} |
| 523 | |
} |
| 524 | 0 | return null; |
| 525 | |
} |
| 526 | |
|
| 527 | |
} |