3


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

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Dil.
  • 1,996
  • 7
  • 41
  • 68

3 Answers3

4

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

IMParasharG
  • 1,869
  • 1
  • 15
  • 26
  • I already have en.properties and fr.properties files in src/main/resources/messages/ path. How do I change this according to my files – Dil. Dec 31 '18 at 11:23
  • 2
    @pippilongstocking : Rename both file with message_en , message_fr and update messageSource bean code with messageResource.setBasename("classpath:messages/message"); – IMParasharG Dec 31 '18 at 11:27
  • Yep its working thank you very much. Can you please tell me why I needed to change that file names. – Dil. Dec 31 '18 at 11:32
1

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

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65
1

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

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52