/** * Returns a {@link ResourceBundle} corresponding to the given {@link Locale} package and resource class. Falls-back * on the ROOT {@link Locale}, if the {@code fallbackOnRoot} flag is true and the requested Locale is not available. * * @param locale the {@link Locale} for which the {@link ResourceBundle} is being requested * @param packagePath * @param resourceClass * @param fallbackOnRoot if true, and a {@link ResourceBundle} cannot be found for the requested {@link Locale}, falls * back on the ROOT {@link Locale} * @return a {@link ResourceBundle} corresponding to the given {@link Locale} package and resource class */ public static ResourceBundle getBundle( final Locale locale, final String packagePath, final Class<?> resourceClass, final boolean fallbackOnRoot ) { final GlobalMessageControl control = new GlobalMessageControl( fallbackOnRoot ); final String resourceName = control.toResourceName( control.toBundleName( packagePath, locale ), "properties" ); ResourceBundle bundle; try { bundle = ResourceBundle.getBundle( packagePath, locale, resourceClass.getClassLoader(), new GlobalMessageControl( fallbackOnRoot ) ); } catch ( final MissingResourceException e ) { final StringBuilder msg = new StringBuilder(); msg.append( "Unable to find properties file '" ).append( resourceName ).append( "': " ).append( e.toString() ); throw new MissingResourceException( msg.toString(), resourceClass.getName(), packagePath ); } return bundle; }
public static String getMessage(String key, Locale requestedLocale, Object... replacements){ String value = null; try { ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME, requestedLocale); value = resourceBundle.getString(key); return MessageFormat.format(value, replacements); } catch (MissingResourceException e) { return e.toString(); } }
/** * Get a raw message from the resource bundles using the given code. */ public String getMessage(final String code) { assert code != null; for (ResourceBundle bundle : bundles) { try { return bundle.getString(code); } catch (MissingResourceException e) { log.trace(e.toString(), e); } } throw new ResourceNotFoundException(code); }
public static String getLocalizedMessage(String bundleName, String key){ String value = null; ResourceBundle resourceBundle = null; try { if(LocaleProviderFacade.getLocaleProvider()!=null){ resourceBundle = ResourceBundle.getBundle(bundleName, LocaleProviderFacade.getLocaleProvider().getLocale()); }else{ resourceBundle = ResourceBundle.getBundle(bundleName, Locale.ENGLISH); } value = resourceBundle.getString(key); return value; } catch (MissingResourceException e) { return e.toString(); } } }
public static String getMessage(MessageReference reference, Locale requestedLocale){ String value = null; try { ResourceBundle resourceBundle = ResourceBundle.getBundle(reference.getBundleName(), requestedLocale); value = resourceBundle.getString(reference.getKey()); if (reference.getContent()!=null){ Object[] contentAsArray = reference.getContent().toArray(new Object[0]); return MessageFormat.format(value, contentAsArray); }else{ return value; } } catch (MissingResourceException e) { return e.toString(); } }