@Configuration
public class AppConfig {
@Autowired
private MessageSource messageSource;
@Bean
public String getMessage() {
// プレースホルダーを置換する値の配列
Object[] args = {"John", "Doe"};
// ロケールを取得
Locale locale = LocaleContextHolder.getLocale();
// MessageSourceResolvableを使用してメッセージを取得
MessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("hello.message", args, "Default message", locale);
String message = messageSource.getMessage(resolvable, locale);
return message;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:message");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}