cover
cover

Reputation: 57

Where to put properties files when dealing with internationalization?

I'm trying to make a internationalization in my program and I found this simple example how to use it:


package tests;

import java.util.*;

public class I18NSample {

    static public void main(String[] args) {

        String language;
        String country;

        if (args.length != 2) {
            language = new String("en");
            country = new String("US");
        } else {
            language = new String(args[0]);
            country = new String(args[1]);
        }

        Locale currentLocale;
        ResourceBundle messages;

        currentLocale = new Locale(language, country);

        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
        System.out.println(messages.getString("greetings"));
        System.out.println(messages.getString("inquiry"));
        System.out.println(messages.getString("farewell"));
    }
}

but every time I compile the code i get this exception:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US

My files are named MessagesBundle_en_US.properties and MessagesBundle_es_ES.properties and are placed in the same package as I18NSample class, so it should work, I think.

Upvotes: 1

Views: 403

Answers (1)

Jasper Huzen
Jasper Huzen

Reputation: 1573

It should be in the root of the classpath because it use by default non package.

Otherwise change:

messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);

to

messages = ResourceBundle.getBundle("tests.MessagesBundle", currentLocale);

to make it work with the bundle inside of the directory/package of your class.

Upvotes: 2

Related Questions