英文:
The problem with Internationalization in Spring Boot do not work in Configuration file
问题
以下是翻译好的部分:
我目前正在使用Spring Boot(包括Spring Security)开发Web应用程序。目前我正在尝试集成国际化支持,首先是默认语言俄语。
这是一个配置文件,我在其中编写代码以获取俄语语言的消息,如果用户输入了错误的数据等,但是这段代码却返回英语。
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class ConfigForAuth {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(new Locale("ru"));
return slr;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource rs = new ReloadableResourceBundleMessageSource();
rs.addBasenames("classpath:i18n/messages");
rs.addBasenames("classpath:/org/springframework/security/messages_ru.properties:1");
rs.setDefaultEncoding("UTF-8");
rs.setUseCodeAsDefaultMessage(true);
return rs;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
return localeChangeInterceptor;
}
}
但是,如果我将相同的代码写在应用程序文件中,这段代码可以正常工作,并以俄语返回给我。
应用程序文件:
@SpringBootApplication
@ComponentScan("--.--.--") //conf
@EnableCaching
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:clients.properties")
})
public class OneBpmAuthApiApplication {
public static void main(String[] args) {
SpringApplication.run(OneBpmAuthApiApplication.class, args);
}
}
英文:
I'm currently working on a Web-App using Spring boot (including spring security). At the moment i'm trying to integrate internationalization support for the defult language russian as a start.
This is a configuration file where I write to get messages in the Russian language, if the user writed wrong data or e.t but this code returns me In English
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import java.util.Locale;
@Configuration
public class ConfigForAuth {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(new Locale("ru"));
return slr;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource rs = new ReloadableResourceBundleMessageSource();
rs.addBasenames("classpath:i18n/messages");
rs.addBasenames("classpath:/org/springframework/security/messages_ru.properties:1");
rs.setDefaultEncoding("UTF-8");
rs.setUseCodeAsDefaultMessage(true);
return rs;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
return localeChangeInterceptor;
}
}
But if I will write this same code in Application file this code works fine and returns to me in RU
Application file:
@SpringBootApplication
@ComponentScan("--.--.--")//conf
@EnableCaching
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource("classpath:clients.properties")
})
public class OneBpmAuthApiApplication {
public static void main(String[] args) {
SpringApplication.run(OneBpmAuthApiApplication.class, args);
}
答案1
得分: 0
尝试从 WebMvcConfigurerAdapter
进行扩展,并添加拦截器。
@Configuration
public class ConfigForAuth extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
英文:
Try extending from WebMvcConfigurerAdapter
and adding the interceptors.
@Configuration
public class ConfigForAuth extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
答案2
得分: 0
我认为之前的建议基本上是正确的,只是你需要将 ParamName 设置为 "lang",像这样:
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
同时,请记得扩展 WebMvcConfigurer。
英文:
I think previous advice was more or less right just you need to set ParamName to "lang" like so:
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new
LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
also please do remember to extend WebMvcConfigurer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论