Loading of Java message properties for internalization

4.8k views Asked by At

I have the following Java i18n Message class.:

public class Messages { 

    private static final String BUNDLE_NAME = "languages.message";
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

    private Messages() {
    }

    public static String getI18n(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

    public static String getI18n(String key, Object... params  ) {
        try {
            return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

I have created the following message properties files.:

message.properties
message_de.properties
message_de_DE.properties

In my program I get the translation according to the default locale of the system. If it is de_DE, the german message properties message_de_DE.properties loaded.

If the default locale is de_CH, then there is no message properties file. Is then the message_de.properties as fallback loaded or do I need to implement it by myself?

1

There are 1 answers

0
pjanssen On BEST ANSWER

According to this blog post you are right.

So when the default locale of your system is de_DE and you request a resource for locale en_US, the lookup order for the properties files is:

  1. MyApp_en_US.properties
  2. MyApp_en.properties
  3. MyApp_de_DE.properties
  4. MyApp_de.properties
  5. MyApp.properties