juice
juice

Reputation: 587

No message found under code 'email.subject' for locale 'en_US'

I have property resource files for different localization. I set my default locale like this Locale.forLanguageTag("en").

I retreive the property like this: messageSource.getMessage("email.subject", null, Locale.getDefault().stripExtensions());

However, my application fails with error No message found under code 'email.subject' for locale 'en_US'. My property file has suffix _en, so I understand that it can't find it - but my question is: why does it search for locale en_US instead of en?

For other languages (that doesn't have this region suffix), it works fine.

Upvotes: 1

Views: 136

Answers (1)

Michiel
Michiel

Reputation: 3440

I set my default locale like this Locale.forLanguageTag("en").

The method forLanguageTag does not set a default locale. Locale.setDefault(Locale) does (only for the current instance of the JVM).

My guess is that your default locale is en_US and while it may seem as if your code adds a region for some reason, it actually did not set a new default locale and kept your original en_US.

I have set up a small test to illustrate it:

public class MessageSourceTest {

    @Rule
    public ExpectedException expEx = ExpectedException.none();

    @Test
    public void test() {

        expEx.expectMessage("No message found under code 'email.subject' for locale 'en_CA'");

        Locale.setDefault(Locale.CANADA);

        Locale.forLanguageTag("en");

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:nonexistingResourceBundleButDoesntMatter");
        messageSource.getMessage("email.subject", null, Locale.getDefault().stripExtensions());
    }
}

The exception refers to the locale set by Locale.setDefault(Locale.CANADA) instead of the method invocation Locale.forLanguageTag("en");.

Upvotes: 1

Related Questions