use of java.util.MissingResourceException in project blueocean-plugin by jenkinsci.
the class BlueI18n method getBundle.
@CheckForNull
private JSONObject getBundle(BundleParams bundleParams, Locale locale) {
PluginWrapper plugin = bundleParams.getPlugin();
if (plugin == null) {
return null;
}
try {
ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleParams.bundleName, locale, plugin.classLoader);
JSONObject bundleJSON = new JSONObject();
for (String key : resourceBundle.keySet()) {
bundleJSON.put(key, resourceBundle.getString(key));
}
return bundleJSON;
} catch (MissingResourceException e) {
// fall through and return null.
}
return null;
}
use of java.util.MissingResourceException in project qi4j-sdk by Qi4j.
the class ConstraintViolationException method localizedMessagesFrom.
/**
* Creates localized messages of all the constraint violations that has occured.
* <p>
* The key "<code>Qi4j_ConstraintViolation_<i><strong>CompositeType</strong></i></code>" will be used to lookup the text formatting
* pattern from the ResourceBundle, where <strong><code><i>CompositeType</i></code></strong> is the
* class name of the Composite where the constraint was violated. If such key does not exist, then the
* key "<code>Qi4j_ConstraintViolation</code>" will be used, and if that one also doesn't exist, or
* the resourceBundle argument is null, then the default patterns will be used;
* </p>
* <table summary="Localization of constraint vioations.">
* <tr><th>Type of Composite</th><th>Pattern used</th></tr>
* <tr><td>Composite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in composite \n{0} of type {1}</code></td>
* </tr>
* <tr><td>EntityComposite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in entity {1}[id={0}]</code></td>
* </tr>
* <tr><td>ServiceComposite</td>
* <td><code>Constraint Violation in {2}.{3} with constraint {4}, in service {0}</code></td>
* </tr>
* </table>
* Then format each ConstraintViolation according to such pattern, where the following argument are passed;
* <table summary="List of arguments available."><tr><th>Arg</th><th>Value</th></tr>
* <tr>
* <td>{0}</td>
* <td>Composite instance toString()</td>
* </tr>
* <tr>
* <td>{1}</td>
* <td>CompositeType class name</td>
* </tr>
* <tr>
* <td>{2}</td>
* <td>MixinType class name</td>
* </tr>
* <tr>
* <td>{3}</td>
* <td>MixinType method name</td>
* </tr>
* <tr>
* <td>{4}</td>
* <td>Annotation toString()</td>
* </tr>
* <tr>
* <td>{5}</td>
* <td>toString() of value passed as the argument, or "null" text if argument was null.</td>
* </tr>
* </table>
* <p>
* <b>NOTE!!!</b> This class is still under construction and will be modified further.
* </p>
*
* @param bundle The ResourceBundle for Localization, or null if default formatting and locale to be used.
*
* @return An array of localized messages of the violations incurred.
*/
public String[] localizedMessagesFrom(ResourceBundle bundle) {
String pattern = "Constraint violation in {0}.{1} for method ''{3}'' with constraint \"{4}({6})\", for value ''{5}''";
ArrayList<String> list = new ArrayList<String>();
for (ConstraintViolation violation : constraintViolations) {
Locale locale;
if (bundle != null) {
try {
pattern = bundle.getString("qi4j.constraint." + mixinTypeName + "." + methodName);
} catch (MissingResourceException e1) {
try {
pattern = bundle.getString("qi4j.constraint");
} catch (MissingResourceException e2) {
// ignore. The default pattern will be used.
}
}
locale = bundle.getLocale();
} else {
locale = Locale.getDefault();
}
MessageFormat format = new MessageFormat(pattern, locale);
Annotation annotation = violation.constraint();
String name = violation.name();
Object value = violation.value();
String classes;
if (Iterables.count(instanceTypes) == 1) {
classes = Iterables.first(instanceTypes).getSimpleName();
} else {
classes = "[" + Iterables.<Class<?>>toString(instanceTypes, new Function<Class<?>, String>() {
@Override
public String map(Class<?> from) {
return from.getSimpleName();
}
}, ",") + "]";
}
Object[] args = new Object[] { instanceToString, classes, mixinTypeName, methodName, annotation.toString(), "" + value, name };
StringBuffer text = new StringBuffer();
format.format(args, text, null);
list.add(text.toString());
}
String[] result = new String[list.size()];
list.toArray(result);
return result;
}
use of java.util.MissingResourceException in project jersey by jersey.
the class Localizer method localize.
public String localize(Localizable l) {
String key = l.getKey();
if (Localizable.NOT_LOCALIZABLE.equals(key)) {
// this message is not localizable
return (String) l.getArguments()[0];
}
String bundlename = l.getResourceBundleName();
try {
ResourceBundle bundle = _resourceBundles.get(bundlename);
if (bundle == null) {
try {
bundle = ResourceBundle.getBundle(bundlename, _locale);
} catch (MissingResourceException e) {
// work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
// all files with an extension different from .class (hence all the .properties files)
// get copied to the top level directory instead of being in the package where they
// are defined
// so, since we can't find the bundle under its proper name, we look for it under
// the top-level package
int i = bundlename.lastIndexOf('.');
if (i != -1) {
String alternateBundleName = bundlename.substring(i + 1);
try {
bundle = ResourceBundle.getBundle(alternateBundleName, _locale);
} catch (MissingResourceException e2) {
// try OSGi
OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();
if (osgiRegistry != null) {
bundle = osgiRegistry.getResourceBundle(bundlename);
} else {
final String path = bundlename.replace('.', '/') + ".properties";
final URL bundleUrl = ResourceFinder.findEntry(path);
if (bundleUrl != null) {
try {
bundle = new PropertyResourceBundle(bundleUrl.openStream());
} catch (IOException ex) {
// ignore
}
}
}
}
}
}
if (bundle == null) {
return getDefaultMessage(l);
} else {
_resourceBundles.put(bundlename, bundle);
}
}
if (key == null) {
key = "undefined";
}
String msg;
try {
msg = bundle.getString(key);
} catch (MissingResourceException e) {
// notice that this may throw a MissingResourceException of its own (caught below)
msg = bundle.getString("undefined");
}
// localize all arguments to the given localizable object
Object[] args = l.getArguments();
for (int i = 0; i < args.length; ++i) {
if (args[i] instanceof Localizable) {
args[i] = localize((Localizable) args[i]);
}
}
String message = MessageFormat.format(msg, args);
return message;
} catch (MissingResourceException e) {
return getDefaultMessage(l);
}
}
use of java.util.MissingResourceException in project jodd by oblac.
the class ResourceBundleMessageResolver method findResourceBundle.
/**
* Finds resource bundle by it's name. Missed and founded resource bundles are cached for
* better performances. Returns <code>null</code> if resource bundle is missing.
*/
public ResourceBundle findResourceBundle(String bundleName, Locale locale) {
if (bundleName == null) {
bundleName = fallbackBundlename;
}
if (locale == null) {
locale = fallbackLocale;
}
if (!cacheResourceBundles) {
try {
return getBundle(bundleName, locale, ClassLoaderUtil.getDefaultClassLoader());
} catch (MissingResourceException ignore) {
return null;
}
}
String key = bundleName + '_' + LocaleUtil.resolveLocaleCode(locale);
try {
if (!misses.contains(key)) {
ResourceBundle bundle = notmisses.get(key);
if (bundle == null) {
bundle = getBundle(bundleName, locale, ClassLoaderUtil.getDefaultClassLoader());
notmisses.put(key, bundle);
}
return bundle;
}
} catch (MissingResourceException ignore) {
misses.add(key);
}
return null;
}
use of java.util.MissingResourceException in project OpenAM by OpenRock.
the class PWResetModelImpl method getL10NAttributeName.
/**
* Returns localized string of an attribute in a service
*
* @param mgr Service schema manager.
* @param key localization key of the attribute.
* @return localized string of an attribute in a service.
*/
protected String getL10NAttributeName(ServiceSchemaManager mgr, String key) {
String i18nName = key;
try {
String name = mgr.getI18NFileName();
if (name != null) {
ResourceBundle rb = PWResetResBundleCacher.getBundle(name, localeContext.getLocale());
i18nName = Locale.getString(rb, key, debug);
}
} catch (MissingResourceException e) {
debug.warning("PWResetModelImpl.getL10NAttributeName", e);
}
return i18nName;
}
Aggregations