Search in sources :

Example 51 with MissingResourceException

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;
}
Also used : MessageFormat(java.text.MessageFormat) MissingResourceException(java.util.MissingResourceException)

Example 52 with MissingResourceException

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;
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 53 with MissingResourceException

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
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 54 with MissingResourceException

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
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Example 55 with MissingResourceException

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
    }
}
Also used : Locale(java.util.Locale) MissingResourceException(java.util.MissingResourceException) ResourceBundle(java.util.ResourceBundle)

Aggregations

MissingResourceException (java.util.MissingResourceException)163 ResourceBundle (java.util.ResourceBundle)85 Locale (java.util.Locale)67 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)10 MessageFormat (java.text.MessageFormat)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 File (java.io.File)7 PropertyResourceBundle (java.util.PropertyResourceBundle)7 SMSException (com.sun.identity.sm.SMSException)6 Secure.getString (android.provider.Settings.Secure.getString)5 SSOException (com.iplanet.sso.SSOException)5 Iterator (java.util.Iterator)5 Set (java.util.Set)5 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 ISResourceBundle (com.sun.identity.common.ISResourceBundle)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Enumeration (java.util.Enumeration)3