英文:
SpringBoot: accessing custom i18n validation message programmatically
问题
问题很简单:我正在开发一个Spring Boot网站,有3种语言版本,我已经通过messages_xx.properties国际化了消息,其中xx是语言前缀,以下是代码部分:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
这个工作得很好。然后,为了保持简单,我还创建了ValidationMessages_xx.properties用于验证,它也运行良好。问题出现在我需要以编程方式从ValidationMessages访问一些消息时,例如:
if (!userDto.getPassword().equals(userDto.getRepeatPassword())) {
bindingResult.rejectValue("repeatPassword", "error.repeatPassword", "custom.validation.passwordsDontMatch.message");
}
问题出在rejectValue的第三个参数上 - 我需要以某种方式替换来自ValidationMessages的消息。但是上面显示的代码仅返回文字字符串"custom.validation.passwordsDontMatch.message",尽管Idea编辑器在我将光标放在字符串上时显示它“看到”了ValidationMessages中的正确消息。这个也不起作用:
```bindingResult.rejectValue("username", "error.username", "${custom.validation.passwordsDontMatch.message}");```
我如何从Java类中访问custom.validation.passwordsDontMatch.message字符串?
英文:
Simple problem here: I'm working on a Spring Boot website with 3 language versions, I have internationalized the messages using messages_xx.properties where xx is the language prefix, and the following code:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
This works fine. Then, to keep things simple, I have also created ValidationMessages_xx.properties for validation and it works fine, too. The problem arises when I need to access some message from ValidationMessages programmatically, for instance:
if (!userDto.getPassword().equals(userDto.getRepeatPassword())) {
bindingResult.rejectValue("repeatPassword", "error.repeatPassword", "custom.validation.passwordsDontMatch.message");
}
The probliem lies in third parameter for rejectValue - I need to somehow substitute a message from the ValidationMessages here. But the code shown above only returns the literal string "custom.validation.passwordsDontMatch.message", even though the Idea editor shows it "sees" the correct message from ValidationMessages here when I put the cursor over the string. This doesn't work either:
bindingResult.rejectValue("username", "error.username", "${custom.validation.passwordsDontMatch.message}");
How can I access that custom.validation.passwordsDontMatch.message string from a Java class?
答案1
得分: 2
ResourceBundle bundle = ResourceBundle.getBundle("ValidationMessages", LocaleContextHolder.getLocale());
String value = bundle.getString("custom.validation.passwordsDontMatch.message");
英文:
ResourceBundle bundle = ResourceBundle.getBundle("ValidationMessages", LocaleContextHolder.getLocale());
String value = bundle.getString("custom.validation.passwordsDontMatch.message");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论