gpt4 book ai didi

java - 无法在 spring 应用程序中更改语言

转载 作者:行者123 更新时间:2023-11-29 08:24:50 25 4
gpt4 key购买 nike


我正在尝试使用 <spring:message> 更改语言标签。但它没有得到认可。

语言.jsp

<%@ taglib prefix="spring"
uri="http://www.springframework.org/tags" %>
<html>
<body>
<h1><spring:message code="home.title" /></h1>
<p><spring:message code="home.intro" /></p>
<p>
<a href="?lang=en">English</a> |
<a href="?lang=fr">French</a>
</p>
</body>
</html>

AppConfig.java

  @Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.dilini.controller", "com.dilini.service"})
@Import({DatabaseConfig.class, SecurityConfig.class})
public class AppConfig extends WebMvcConfigurerAdapter {


@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}

@Bean
public HandlerInterceptor performanceInterceptor() {
PerformanceInterceptor interceptor;
interceptor = new PerformanceInterceptor();
return interceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(performanceInterceptor()).addPathPatterns("/user/*");
registry.addInterceptor(localeChangeInterceptor());
}

@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}

@Bean
public HandlerInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
return interceptor;
}

@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
return localeResolver;
}
}

src/main/resources/messages/en.properties

home.title=Home
home.intro= this is my magnificent intro

法国人也是如此。

src/main/resources/messages/fr.properties

home.title=Accueil
home.intro=Splendide page d'accueil,

我是否需要为此功能添加另一个依赖项?还是代码有问题?


请帮忙

最佳答案

试试下面的代码

@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver() {
CookieLocaleResolver resolver= new CookieLocaleResolver();
resolver.setCookieDomain("myAppLocaleCookie");
resolver.setCookieMaxAge(600);
return resolver;
}

@Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageResource= new ReloadableResourceBundleMessageSource();
// For example: i18n/messages_en.properties
// For example: i18n/messages_fr.properties
messageResource.setBasename("classpath:i18n/messages");
messageResource.setDefaultEncoding("UTF-8");
return messageResource;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
localeInterceptor.setParamName("lang");
registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
}

注意:messages_en.properties 和 messages_fr.properties 应该出现在 src/main/resources/i18n 中

关于java - 无法在 spring 应用程序中更改语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com