Run application and go to the browser:
http://localhost:8080
- demo for change langhttp://localhost:8080/local
- show current local code
With Spring Boot, we can use the messages.properties
(/src/main/resources/messages.properties
) file which will be used for the default locale, US in our case.
For other language we must add local code, for example for French: messages_fr.properties
- By application.properties to French:
spring.mvc.locale=fr
spring.mvc.locale-resolver=fixed
see application.properties
- By bean configuration:
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
}
see AppConfig.java
Use LocaleChangeInterceptor
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
And then when we will pass the GET parameter: ?lang=fr
language will be changed to transmitted
Use MessageSource method getMessage
@RequestMapping("/trulala")
public String trulala(Locale locale) {
return messageSource.getMessage("trulala", null, locale);
}