Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

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?

like image 577
user985351 Avatar asked Nov 03 '11 20:11

user985351


2 Answers

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.

like image 167
Kent Avatar answered Nov 13 '22 18:11

Kent


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

like image 1
Sanal S Avatar answered Nov 13 '22 18:11

Sanal S