Configured Spring for internalization and localization, but encountered a problem in encoding. English characters are displayed normally, but instead of Russian text, question marks???????. And messages_ru.properties the following problem arises from the file " Unsupported characters for the charset 'ISO-8859-1'"
label.home=Главная Страница
label.about=О Нас
label.name=Наименование
label.price=Цена
button.add=Добавить
Configuration Code:
@Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
source.setBasename("classpath:messages");
source.setDefaultEncoding("UTF-8");
source.setFallbackToSystemLocale(true);
return source;
}
@Bean
public CookieLocaleResolver localeResolver(){
CookieLocaleResolver resolver = new CookieLocaleResolver();
resolver.setDefaultLocale(new Locale("ru"));
resolver.setCookieName("language");
resolver.setCookieMaxAge(3600*24*365);
return resolver;
}
@Bean
public LocaleChangeInterceptor localeInterceptor(){
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lng");
return interceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(localeInterceptor());
}
Russian characters are not part of charset ISO-8859-1
and you have to use UTF-8
.
The messages.properties
file must be UTF-8 encoded because Spring doesn't convert it automatically even if you define setDefaultEncoding("UTF-8");
.
Open your messages.properties
file with an editor and make sure it has UTF-8
encoding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.