Home > Blockchain >  How to change language for entire Spring BOOT app from language selector
How to change language for entire Spring BOOT app from language selector

Time:08-12

Hi everyone I got Spring boot application and now I am trying to implement i18n. I already created a Configuration class which implements WebMvcConfigurer and create beans for LocalResolver, MessageSource, and Interceptors. and it works perfectly when I am changing my language from the browser it changes language for all views of the application. but now the problem is that I wrote some language selector to change language and it changes language only for one view. how can I implement this to assign selected language for all views of the application? thank you.

this is my configuration

@Configuration
   public class WebMvcConfig implements WebMvcConfigurer {
    @Bean(name = "localeResolver")
    public LocaleResolver getLocaleResolver()  {
        CookieLocaleResolver resolver= new CookieLocaleResolver();
        resolver.setCookieDomain("myAppLocaleCookie");
        // 60 minutes
        resolver.setCookieMaxAge(60*60);
        return resolver;
    }

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

this is my home controller mapping

@GetMapping("/international")
    public String getInternationalPage() {
   
        return "home";
    }

and peace of my thymeleaf view

 <select id="locales">
                    <option value="choose language"></option>
                    <option value="en" th:text="english"></option>
                    <option value="hy" th:text="armenian"></option>
                </select>
    
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
                </script>
                <script type="text/javascript">
                    $(document).ready(function() {
                        $("#locales").change(function () {
                            var selectedOption = $('#locales').val();
                            if (selectedOption != ''){
                                window.location.replace('international?lang='   selectedOption);
                            }
                        });
                    });
                </script>

CodePudding user response:

use

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver slr = new SessionLocaleResolver();
    slr.setDefaultLocale(Locale.US);
    return slr;
}

full

import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
@ComponentScan(basePackages = "com.baeldung.internationalization.config")
public class MvcConfig implements WebMvcConfigurer {

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        slr.setDefaultLocale(Locale.US);
        return slr;
    }

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

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

Create these language files

messages.properties
messages_vi.properties
messages_fr.properties
messages_kr.properties
...

sample source code https://github.com/eugenp/tutorials/blob/master/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/internationalization/config/MvcConfig.java#L19

https://www.baeldung.com/spring-boot-internationalization

  • Related