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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With