简体   繁体   中英

cannot change language in spring application


I'm trying to change language using <spring:message> tag. But it doesn't get recognized.

language.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

Likewise the french.

src/main/resources/messages/fr.properties

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

Do I need to add another dependency for this feature? Or there is any issue is the code?


Please help

try this below code

@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("/*");
    }

Note : messages_en.properties and messages_fr.properties should be present into src/main/resources/i18n

In your appconfig i don't see the interceptor registration. You simply defined it but you never registered it. You should override the addInterceptors method

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(localeChangeInterceptor());
}

Try to add it and check if it works

In your src/main/resources/messages/ be sur to create both i18 files (en and fr named as below ):

in your MessageSource you just set the basename to messages => messageSource.setBasename("classpath:/messages");

so your local files should be named messages_[local].properties (spring will search for local names file as set in the as .setBasename() )

as below

messages_en.properties that contains :

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

and messages_fr.properties that

home.title=Accueil
home.intro= ceci est ma magnifique intro

It should work

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM