1

I prepared own class Internationalization which extends class ResourceBundle for exception hangling when key is not found in properties file.

public class Internationalization extends ResourceBundle{
    private ResourceBundle bundle;

    public Internationalization(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    public String getFormattedString(String key, Object[] args) {
       if (bundle.containsKey(key)) {
            String value = bundle.getString(key);
            value = replaceSingleQuotes(value);
            MessageFormat mf = new MessageFormat(value);
            return mf.format(args);
       } else {
           return '!' + key + '!';
       }        
    }

    private static String replaceSingleQuotes(String template) {
        return template.replace("'", "''");
    }  

    @Override
    protected Object handleGetObject(String key) {
        if (bundle.containsKey(key)) {
            return bundle.getString(key);
        } else {
            return '!' + key + '!';
        }
    }

    @Override
    public Enumeration<String> getKeys() {
        return bundle.getKeys();
    }
}

But FXMLLoader still throws exception "Resource "MissingKey" not found." when key is not in the properies file.

Internationalization lngStr = new Internationalization(ResourceBundle.getBundle("language/lngStr"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/LogIn.fxml"));
loader.setResources(lngStr);
Pane pane = loader.load();
scrMain.setContent((Node) pane);

Can you help me please to find where is the problem?

T..o
  • 11
  • 2
  • Have you thought of the key being searched in the Enumaration returned by the getKeys method? – Zsolt Apr 07 '17 at 12:38
  • You're right. Problem is in getKey method. Any idea how to handle it? – T..o Apr 10 '17 at 06:40
  • I guess you have to return a "made-up" Enumeration that return true for any call to enum.contains(key), regardless the value of key. – Zsolt Feb 07 '21 at 16:07

0 Answers0