Spring Framework is really great when it comes to i18n. It applies different strategies to get user’s locale from request. In this post we’ll show how apply that!
Spring Locale from Request
This is the simplest Spring REST Controller illustrating how to get Locale from HTTP Request. All we need to do is to add Locale parameter to the method receiving HTTP Request:
package com.farenda.spring.tutorial.rest.locale; import org.springframework.web.bind.annotation.*; import java.util.Locale; import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; import static org.springframework.web.bind.annotation.RequestMethod.GET; @RestController public class LocalizedController { @RequestMapping(value = "/echo", method = GET, produces = TEXT_PLAIN_VALUE) public String getLocaleName(Locale locale) { return "Locale from request: " + locale; } }
Why that works? When a request comes in it goes to Spring’s DespatcherServlet, which tries to find a LocaleResolver that can provide locale based on some strategy – “accept-language” header, cookies, user’s session, etc. Locale from request is then available through RequestContext.getLocale(), but also can be injected as we did here.
Spring test of REST Controller
We can pretty easily test the above controller using MockMvcBuilders from the Spring Test package. The only thing we need to do is to call locale(Locale) method, when we build GET Request, to set our Locale on the request – you can pass more locales if you want.
package com.farenda.spring.tutorial.rest.locale import org.springframework.test.web.servlet.setup.MockMvcBuilders import spock.lang.Specification import spock.lang.Subject import static org.springframework.http.MediaType.APPLICATION_JSON import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status class LocalizedControllerTest extends Specification { @Subject def localeApi = new LocalizedController() def mockMvc = MockMvcBuilders.standaloneSetup(localeApi).build() def 'should return locale from request'() { given: def myLocale = Locale.CHINA def expectedResponse = 'Locale from request: ' + myLocale when: def result = mockMvc.perform( get('/echo').contentType(APPLICATION_JSON) .locale(myLocale)) then: result.andExpect(status().isOk()) .andExpect(content().string(expectedResponse)) } }
Experiment with the above code and use different locales, pass more than one locale, or even add parameters to the API method.
References:
- Check out other cool stuff in Spring Tutorial!
- See Spock Framework Tutorial if you don’t use it yet!