Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

java - Multilanguage Option in Spring MVC Project

This code I have put in the Header.JSP, this dropdown will appear on all pages

<li class="dropdown">Language
 <ul class="dropdown-menu">
<li><a href="?lang=hin">Hindi</a></li>
<li><a href="?lang=en">English</a></li>
<li><a href="?lang=tam">Tamil</a></li>

messages_en.properties

label.our_objective = Our Objective
label.our_objective1 =Internet Banking site provides personal banking and corporate banking services that gives you complete control over all your banking demands online.

Likes this messages_hin.properties for Hindi languages are used.

the code below is used in JSP for language change.

<spring:message  code="label.account_summary" />

This is SpringMVCConfig.java

public class WebMvcConfig extends WebMvcConfigurerAdapter {
 
   // Static Resource Config
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
       // Default..
   }
 
   @Override
   public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
   }
 
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
       localeInterceptor.setParamName("lang");
      
     
       registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
   }
  
}

public class WebMvcConfig extends WebMvcConfigurerAdapter {
 
   // Static Resource Config
   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
       // Default..
   }
 
   @Override
   public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
       configurer.enable();
   }
 
   @Override
   public void addInterceptors(InterceptorRegistry registry) {
       LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
       localeInterceptor.setParamName("lang");
      
     
       registry.addInterceptor(localeInterceptor).addPathPatterns("/*");
   }
  
}

This is ApplicationContextConfig.java

@ComponentScan("com.cedge.*")
public class ApplicationContextConfig {
 
   @Bean(name = "viewResolver")
   public InternalResourceViewResolver getViewResolver() {
       InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
 
       viewResolver.setPrefix("/resources/jsp/");
       viewResolver.setSuffix(".jsp");
 
       return viewResolver;
   }
    
   @Bean(name = "messageSource")
   public MessageSource getMessageResource()  {
       ReloadableResourceBundleMessageSource messageResource= new ReloadableResourceBundleMessageSource();
        
       // Read i18n/messages_xxx.properties file.
       // For example: i18n/messages_en.properties
 
       messageResource.setBasename("classpath:property/message");
       messageResource.setDefaultEncoding("UTF-8");
       return messageResource;
   }
    
   @Bean(name = "localeResolver")
   public LocaleResolver getLocaleResolver()  {
       CookieLocaleResolver resolver= new CookieLocaleResolver();
       //resolver.setCookieDomain("myAppLocaleCookie");
       // 60 minutes
       resolver.setDefaultLocale(Locale.ENGLISH);
       //resolver.setCookieMaxAge(0);
       
       return resolver;
   }
  

 
}

This is SpringWebAppInitializer.java

public class SpringWebAppInitializer implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);
 
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher",new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
 
        // UtF8 Charactor Filter.
        FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class);
 
        fr.setInitParameter("encoding", "UTF-8");
        fr.setInitParameter("forceEncoding", "true");
        fr.addMappingForUrlPatterns(null, true, "/*");
    }}

I want to change the whole project language.

I referred to this link:https://o7planning.org/11227/create-a-multiple-languages-web-application-with-spring-mvc

My problem is When I logout and login again with the same user or the other the language chosen last time gets reflected even for another user. I don't want that to happen. When I logout and login again the application language should default to English.

There is this link that deals with my issue it uses config.language https://dzone.com/articles/learn-how-to-internationalize-and-localize-your-ja

but I don't know how to implement it.

Please Help.

question from:https://stackoverflow.com/questions/65882510/multilanguage-option-in-spring-mvc-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...