Write as follows in src / main / resources / application.properties. If the locale is ja in Japanese environment, read messages_ja.properties.
application.properties
spring.messages.basename=messages //File name (no extension required)
spring.messages.cache-duration=-1
spring.messages.encoding=UTF-8
Create src / main / resources / messages.properties
If there is no file ** messages.properties **, auto-configuration of messageSource will not be executed and an error message will be displayed at startup, so create an empty file.
When multilingualizing, prepare a file after messages that indicates the locale, for example, messages_en.properties
for English (en) and messages_ja.properties
for Japanese.
This time, create the locale messages_ja.properties
which means Japanese.
messages_ja.properties
#Hello World
hello.world=Hello World
Define MessageSource as a property.
UserController.java
@Controller
public class UserController {
@Autowired
protected MessageSource messageSource;
@RequestMapping("/")
public String hello(Model model) {
model.addAttribute("Msg", messageSource.getMessage("hello.world", null, Locale.JAPAN));
return "hello";
}
}
hello.html
<h1 th:text="${Msg}">XXXXX</h1>
hello.html
<h1 th:text="#{hello.world}">XXXXX</h1>
Recommended Posts