我已经将内化功能添加到我的应用程序中,但是我仍然得到一个例外
这是我的配置文件,其中包含messagesource配置
@Configuration
public class LocaleConfiguration implements WebMvcConfigurer {
private static final Logger LOGGER = LoggerFactory.getLogger(LocaleConfiguration.class);
/**
* * @return default Locale set by the user
*/
@Bean(name = "localeResolver")
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.ENGLISH);
return slr;
}
/**
* an interceptor bean that will switch to a new locale based on the value of the lang parameter appended to a request:
*
* @param registry
* @language should be the name of the request param i.e localhost:8010/api/get-all-exceptions/1?language=fr
* <p>
* Note: All requests to the backend needing Internalization should have the "language" request param
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
registry.addInterceptor(localeChangeInterceptor);
}
@Bean("messageSource")
public MessageSource messageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource ();
resourceBundleMessageSource.setBasename("i18n/messages");
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setFallbackToSystemLocale(true);
return resourceBundleMessageSource;
}
这是我使用消息源的服务
@Service
public class LocaleServiceImpl implements LocaleService {
private static final Logger LOGGER = LoggerFactory.getLogger(LocaleServiceImpl.class);
@Autowired
private MessageSource messageSource;
public String getMessage(ErrorDictionnary code, String... params) {
return this.messageSource.getMessage(code.toString(), params, Locale.ENGLISH);
}
}
当我试图发布?language=en时,出现以下错误:
org.springframework.context.NoSuchMessageException: No message found under code 'ERROR_FIND_TRANSCODIFICATION' for locale 'en'.
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:161)
at fr.cnp.aec.commons.services.impls.LocaleServiceImpl.getMessage(LocaleServiceImpl.java:38)
每次我都会遇到这个错误,我检查过属性文件是否存在\u en和\u fr
编辑:
message.properties中的代码错误
暂无答案!
目前还没有任何答案,快来回答吧!