Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring i18n: NoSuchMessageException: No message found under code 'welcome' for locale 'en_US'

Attempting to realizing internationalization for my application with spring i18n, but when the messageSource.getMessage () was called, it throws noSuchMessageException exception, the details are as follows:

detailMessage = "No message found under code 'welcome' for locale 'en_US'."
cause = org.springframework.context.NoSuchMessageException: No message found under code 'welcome' for locale 'en_US'.

And, in application.yml, the spring i18n basename was configured as:

spring:
   messages:
      basename: i18n/messages

In addition, in the directory: src/main/resources, there are three properties:

resources
  - i18n.messages
     - messages.properties
     - messages_en_US.properties
     - messages_zh_CN.properties

In the controller, I tried to get the message by calling getMessage("key”, null, locale), below is the pertinent code:

import org.springframework.context.MessageSource;
@Autowired
private MessageSource messageSource;

Locale locale = LocaleContextHolder.getLocale();
String str1 = messageSource.getMessage("welcome", null, locale);

In the properties files, I have set value for key-welcome.

To realize internationalization, I thought, it should work well with the above configurations and code, but I always get noSuchMessageException, I have no idea what is going on with this issue, could anyone help me out ?

like image 227
W.Zhou Avatar asked Jan 02 '17 01:01

W.Zhou


1 Answers

It is due to the properties files cannot be found, based on properties file structure, the application.yml should be like: spring.messages.basename: i18n/messages/messages

like image 116
W.Zhou Avatar answered Nov 16 '22 08:11

W.Zhou