use of java.util.MissingResourceException in project sling by apache.
the class Localizer method getMessage.
/*
* Returns the localized error message corresponding to the given error
* code.
*
* If the given error code is not defined in the resource bundle for
* localized error messages, it is used as the error message.
*
* @param errCode Error code to localize
* @param args Arguments for parametric replacement
*
* @return Localized error message
*/
public static String getMessage(String errCode, Object[] args) {
String errMsg = errCode;
try {
errMsg = bundle.getString(errCode);
if (args != null) {
MessageFormat formatter = new MessageFormat(errMsg);
errMsg = formatter.format(args);
}
} catch (MissingResourceException e) {
}
return errMsg;
}
use of java.util.MissingResourceException in project deltaspike by apache.
the class DefaultMessageResolver method getMessage.
@Override
public String getMessage(MessageContext messageContext, String messageTemplate, String category) {
// we can use {{ as escaping for now
if (messageTemplate.startsWith("{{")) {
// in which case we just cut of the first '{'
return messageTemplate.substring(1);
}
if (messageTemplate.startsWith("{") && messageTemplate.endsWith("}")) {
String resourceKey = messageTemplate.substring(1, messageTemplate.length() - 1);
List<String> messageSources = getMessageSources(messageContext);
if (messageSources == null || messageSources.isEmpty()) {
// using {} without a bundle is always an error
return null;
}
Iterator<String> messageSourceIterator = messageSources.iterator();
Locale locale = messageContext.getLocale();
String currentMessageSource;
while (messageSourceIterator.hasNext()) {
currentMessageSource = messageSourceIterator.next();
try {
ResourceBundle messageBundle = PropertyFileUtils.getResourceBundle(currentMessageSource, locale);
if (category != null && category.length() > 0) {
try {
return messageBundle.getString(resourceKey + "_" + category);
} catch (MissingResourceException e) {
// we fallback on the version without the category
return messageBundle.getString(resourceKey);
}
}
return messageBundle.getString(resourceKey);
} catch (MissingResourceException e) {
if (!messageSourceIterator.hasNext()) {
return null;
}
}
}
}
return messageTemplate;
}
use of java.util.MissingResourceException in project robovm by robovm.
the class ResourceBundleTest method test_getStringArrayLjava_lang_String.
public void test_getStringArrayLjava_lang_String() {
ResourceBundle bundle;
String name = "tests.support.Support_TestResource";
Locale.setDefault(new Locale("en", "US"));
bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
String[] array = bundle.getStringArray("StringArray");
for (int i = 0; i < array.length; i++) {
assertEquals("Str" + (i + 1), array[i]);
}
try {
bundle.getStringArray(null);
fail("NullPointerException expected");
} catch (NullPointerException ee) {
//expected
}
try {
bundle.getStringArray("");
fail("MissingResourceException expected");
} catch (MissingResourceException ee) {
//expected
}
try {
bundle.getStringArray("IntegerVal");
fail("ClassCastException expected");
} catch (ClassCastException ee) {
//expected
}
}
use of java.util.MissingResourceException in project robovm by robovm.
the class ResourceBundleTest method test_getObjectLjava_lang_String.
public void test_getObjectLjava_lang_String() {
ResourceBundle bundle;
String name = "tests.support.Support_TestResource";
Locale.setDefault(new Locale("en", "US"));
bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
assertEquals("Wrong value parent4", "frFRVARValue4", (String) bundle.getObject("parent4"));
assertEquals("Wrong value parent3", "frFRValue3", (String) bundle.getObject("parent3"));
assertEquals("Wrong value parent2", "frValue2", (String) bundle.getObject("parent2"));
assertEquals("Wrong value parent1", "parentValue1", (String) bundle.getObject("parent1"));
assertEquals("Wrong value child3", "frFRVARChildValue3", (String) bundle.getObject("child3"));
assertEquals("Wrong value child2", "frFRVARChildValue2", (String) bundle.getObject("child2"));
assertEquals("Wrong value child1", "frFRVARChildValue1", (String) bundle.getObject("child1"));
assertEquals("Wrong value IntegerVal", 1, bundle.getObject("IntegerVal"));
try {
bundle.getObject(null);
fail("NullPointerException expected");
} catch (NullPointerException ee) {
//expected
}
try {
bundle.getObject("");
fail("MissingResourceException expected");
} catch (MissingResourceException ee) {
//expected
}
}
use of java.util.MissingResourceException in project robovm by robovm.
the class ResourceBundleTest method test_getStringLjava_lang_String.
/**
* java.util.ResourceBundle#getString(java.lang.String)
*/
public void test_getStringLjava_lang_String() {
ResourceBundle bundle;
String name = "tests.support.Support_TestResource";
Locale.setDefault(new Locale("en", "US"));
bundle = ResourceBundle.getBundle(name, new Locale("fr", "FR", "VAR"));
assertEquals("Wrong value parent4", "frFRVARValue4", bundle.getString("parent4"));
assertEquals("Wrong value parent3", "frFRValue3", bundle.getString("parent3"));
assertEquals("Wrong value parent2", "frValue2", bundle.getString("parent2"));
assertEquals("Wrong value parent1", "parentValue1", bundle.getString("parent1"));
assertEquals("Wrong value child3", "frFRVARChildValue3", bundle.getString("child3"));
assertEquals("Wrong value child2", "frFRVARChildValue2", bundle.getString("child2"));
assertEquals("Wrong value child1", "frFRVARChildValue1", bundle.getString("child1"));
try {
bundle.getString(null);
fail("NullPointerException expected");
} catch (NullPointerException ee) {
//expected
}
try {
bundle.getString("");
fail("MissingResourceException expected");
} catch (MissingResourceException ee) {
//expected
}
try {
bundle.getString("IntegerVal");
fail("ClassCastException expected");
} catch (ClassCastException ee) {
//expected
}
}
Aggregations