Stephane Stephane - 13 days ago 5
reST (reStructuredText) Question

Spring REST Handle locale change

I'm trying to handle locale change in a Spring 3 REST application.

But the locale is not changed to fr.

The console log shows:
2014-05-19 14:29:46,214 DEBUG [AbstractExceptionHandler] ===================>>> locale: en

Here is my configuration:

@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:messages/messages", "classpath:messages/validation");
// If true, the key of the message will be displayed if the key is not found, instead of throwing an exception
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
// The value 0 means always reload the messages to be developer friendly
messageSource.setCacheSeconds(0);
return messageSource;
}

// The locale interceptor provides a way to switch the language in any page just by passing the lang=’en’, lang=’fr’, and so on to the url
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}

@Bean
public LocaleResolver localeResolver() {
// AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
// SessionLocaleResolver localeResolver = new SessionLocaleResolver();
// localeResolver.setDefaultLocale(StringUtils.parseLocaleString("en"));
return localeResolver;
}


Here is my exception handler:

@ControllerAdvice
public class AdminExceptionHandler extends AbstractExceptionHandler {

@ExceptionHandler(NullPointerException.class)
@ResponseBody
public ResponseEntity<ErrorInfo> nullPointerException(HttpServletRequest request, NullPointerException e) {
String url = request.getRequestURL().toString();
String errorMessage = localizeErrorMessage("error.npe", new Object[] { e.getMessage() });
return new ResponseEntity<ErrorInfo>(new ErrorInfo(url, errorMessage), HttpStatus.INTERNAL_SERVER_ERROR);
}

}

public class AbstractExceptionHandler {

private static Logger logger = LoggerFactory.getLogger(AbstractExceptionHandler.class);

@Autowired
private MessageSource messageSource;

protected String localizeErrorMessage(String errorCode, Object args[]) {
Locale locale = LocaleContextHolder.getLocale();
logger.debug("===================>>> locale: " + locale);
String errorMessage = messageSource.getMessage(errorCode, args, locale);
return errorMessage;
}

protected String localizeErrorMessage(String errorCode) {
return localizeErrorMessage(errorCode, null);
}

protected String extractAdminIdFromUrl(String url) {
String adminId = null;
try {
URI uri = new URI(url);
String path = uri.getPath();
adminId = path.substring(path.lastIndexOf('/') + 1);
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
return adminId;
}

}


And here is my test:

@Test
public void testExceptionLocalizedMessage() throws Exception {
HttpHeaders httpHeaders = Common.createAuthenticationHeaders("stephane" + ":" + PASSWORD);

MvcResult resultGet = this.mockMvc.perform(
get("/error/npe").headers(httpHeaders)
.param("lang", "fr")
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value("Une erreur inconnue s'est produite. Veuillez nous excuser."))
.andReturn();

httpHeaders.add("Accept-Language", "fr");
resultGet = this.mockMvc.perform(
get("/error/npe").headers(httpHeaders)
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value("Une erreur inconnue s'est produite. Veuillez nous excuser."))
.andReturn();
}


I would like to handle the locale argument in the url as in ?lang=en and the Accept-Language header as a fall back.

As a REST application I was thinking of using the AcceptHeaderLocaleResolver class but it does not support the setting of the locale via the url parameter.

I reckoned using the SessionLocaleResolver class makes little sense in a REST application.

That leaves my with the CookieLocaleResolver class which I'm not specially convinced is the one that should be used in a REST application.

Anyway, the retrieved locale is still en and not fr as I expect it to be.

EDIT:

In the test, using the statement:

httpHeaders.add("Accept-Language", Locale.FRENCH.getLanguage());


does not set the locale.
But using the locale() does.
This test passes:

this.mockMvc.perform(
get("/error/npe").headers(httpHeaders).locale(Locale.FRENCH)
.accept(MediaType.APPLICATION_JSON))
.andDo(print()
)
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value(localizeErrorMessage("error.npe", Locale.FRENCH)))
.andReturn();

Answer

I found the solution. Now the Accept-Language header is being used and the cookie as well.

public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public LocaleResolver localeResolver() {
        return new SmartLocaleResolver();
    }

}

public class SmartLocaleResolver extends CookieLocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String acceptLanguage = request.getHeader("Accept-Language");
        if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
            return super.determineDefaultLocale(request);
        }
        return request.getLocale();
    }

}

UPDATE: Following Thor's comment, here is a resolver that checks first for the cookie, and if not found checks for the request header:

@Override
public Locale resolveLocale(HttpServletRequest request) {
    Locale locale = super.determineDefaultLocale(request);
    if (null == locale) {
        String acceptLanguage = request.getHeader("Accept-Language");
        if (acceptLanguage != null && !acceptLanguage.trim().isEmpty()) {
            locale = request.getLocale();
        }
    }
    return locale;
}

Or with a simpler implementation (not tested):

private AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();

@Override
public Locale resolveLocale(HttpServletRequest request) {
    Locale locale = super.determineDefaultLocale(request);
    if (null == locale) {
        locale = acceptHeaderLocaleResolver.resolveLocale(request);
    }
    return locale;
}
Comments