Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {
        CookieLocaleResolver localeResolver = new CookieLocaleResolver();
        localeResolver.setDefaultLocale(new Locale("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);
        return messageSource.getMessage(errorCode, args, locale);
    }

    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();
like image 558
Stephane Avatar asked May 19 '14 12:05

Stephane


2 Answers

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;
}

UPDATE: This above solution is not working any longer.

I'm now trying to pass the accepted language in a header:

httpHeaders.add(HttpHeaders.ACCEPT_LANGUAGE, "fr_FR");

and retrieve it in this locale resolver:

@Override
public Locale resolveLocale(HttpServletRequest request) {
    for (String httpHeaderName : Collections.list(request.getHeaderNames())) {
        logger.debug("===========>> Header name: " + httpHeaderName);
    }
    String acceptLanguage = request.getHeader(HttpHeaders.ACCEPT_LANGUAGE);
    logger.debug("===========>> acceptLanguage: " + acceptLanguage);
    Locale locale = super.resolveLocale(request);
    logger.debug("===========>> acceptLanguage locale: " + locale.getDisplayCountry());
    if (null == locale) {
        locale = getDefaultLocale();
        logger.debug("===========>> Default locale: " + locale.getDisplayCountry());
    }
    return locale;
}

But there is no Accept-Language in the output of the ===========>> Header name logger, and the acceptLanguage logger is empty.

like image 159
Stephane Avatar answered Nov 13 '22 23:11

Stephane


when we are using

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


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

it is able to accept locale from query parameter

{{url}}/com-manh-cp-ext-order/api/ext/ex23order/greeting?language=es
like image 23
avinash kumar Avatar answered Nov 13 '22 22:11

avinash kumar