user985351
user985351

Reputation: 197

Spring: No message found under code for locale 'en_US'

applicationContext-Service.xml

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list><value>messages</value></list>
    </property>
</bean>

I have messages_en_US.properties under /src/messages_en_US.properties

registerForm.passwordNotMatch=Password does not match.

This is line of code is bringing up the error:

binding.addError(new FieldError(REGISTER_FORM, "passwordNotMatch", messageSource.getMessage("registerForm.passwordNotMatch", null, locale)));

Error: No message found under code 'registerForm.passwordNotMatch' for locale 'en_US'.

What might be wrong?

Upvotes: 10

Views: 41845

Answers (4)

Sanal S
Sanal S

Reputation: 1152

For those using @Bean annotation for bundleMessageSource. Add the name for @Bean.

name="messageSource"

Use the same name we used to create the MessageSource object in @RestController class

@RestController
public class HelloWorldController {

    @Autowired
    private MessageSource messageSource;

    @GetMapping(path = "/hello-world-internationalized")
    public String helloWorldInternationalized(@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
        return messageSource.getMessage("good.morning.message", null, locale);
    }
}

Then in the @SpringBootApplication class

@Bean(name="messageSource")//wont work without the bean name
public ResourceBundleMessageSource bundleMessageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}

Referred this link

Upvotes: 1

Sandeep
Sandeep

Reputation: 1

I think instead of <property name="basenames"> it should be <property name="basename">.

Upvotes: -2

Jeff
Jeff

Reputation: 1

I had the same issue. I tried classpath: but it didn't make a difference. Ended up putting in a carriage return on the last line of my properties file (so the cursor sits on the next line, which is the end of the file).

Also, if you're using Maven and you place your properties file like so: /resources/xxxx.properties, the resource directory is automatically pulled into your generated war, so classpath: is not necessary.

Upvotes: 0

Kent
Kent

Reputation: 195049

does it work if you change to:

classpath:messages

?

I had the experience that if using ReloadableResourceBundleMessageSource, in jsp will not find the properties file. adding classpath: before the basename solved my problem.

Well even though was my project managed by maven, I think you can give it a try anyway.

Upvotes: 16

Related Questions