Using language in Spring

I am trying to use LocaleResolverfrom Spring to change the language of the page whenever the user wants. The source language should be Portuguese, but it does not work as expected:

@Bean
public LocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(new Locale("pt-BR"));//StringUtils.parseLocaleString("en")

    return localeResolver;
}
+4
source share
1 answer

I'm not sure what you mean, but "it doesn't work as expected," but you are using the Locale constructor incorrectly. The first language and country are separated by an underscore, not a minus sign, and the only String argument constructor is for the language only. Thus, you have two valid options:

new Locale("pt", "BR")

or

StringUtils.parseLocaleString("pt_BR")
+2
source

Source: https://habr.com/ru/post/1611519/


All Articles