Refactor HttpHeaders "Accept-Language" with Locale
This commit is contained in:
@@ -457,47 +457,43 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the acceptable language ranges from the
|
||||
* {@literal Accept-Language} header
|
||||
* <p>If you only need the most preferred locale use
|
||||
* {@link #getAcceptLanguageAsLocale()} or if you need to filter based on
|
||||
* a list of supporeted locales you can pass the returned list to
|
||||
* Return the language ranges from the {@literal "Accept-Language"} header.
|
||||
* <p>If you only need sorted, preferred locales only use
|
||||
* {@link #getAcceptLanguageAsLocales()} or if you need to filter based on
|
||||
* a list of supported locales you can pass the returned list to
|
||||
* {@link Locale#filter(List, Collection)}.
|
||||
* @since 5.0
|
||||
*/
|
||||
public List<Locale.LanguageRange> getAcceptLanguage() {
|
||||
String value = getFirst(ACCEPT_LANGUAGE);
|
||||
if (value != null) {
|
||||
return Locale.LanguageRange.parse(value);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
return value != null ? Locale.LanguageRange.parse(value) : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* A variant of {@link #setAcceptLanguage(List)} that sets the {@literal Accept-Language}
|
||||
* header value to the specified locale.
|
||||
* Variant of {@link #setAcceptLanguage(List)} using {@link Locale}'s.
|
||||
* @since 5.0
|
||||
*/
|
||||
public void setAcceptLanguageAsLocale(Locale locale) {
|
||||
setAcceptLanguage(Collections.singletonList(new Locale.LanguageRange(locale.toLanguageTag())));
|
||||
public void setAcceptLanguageAsLocales(List<Locale> locales) {
|
||||
setAcceptLanguage(locales.stream()
|
||||
.map(locale -> new Locale.LanguageRange(locale.toLanguageTag()))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* A variant of {@link #getAcceptLanguage()} that converts each
|
||||
* {@link java.util.Locale.LanguageRange} to a {@link Locale} and returns
|
||||
* the first one on the list.
|
||||
* {@link java.util.Locale.LanguageRange} to a {@link Locale}.
|
||||
* @return the locales or an empty list
|
||||
* @since 5.0
|
||||
*/
|
||||
public Locale getAcceptLanguageAsLocale() {
|
||||
public List<Locale> getAcceptLanguageAsLocales() {
|
||||
List<Locale.LanguageRange> ranges = getAcceptLanguage();
|
||||
if (ranges.isEmpty()) {
|
||||
return null;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return ranges.stream()
|
||||
.map(range -> Locale.forLanguageTag(range.getRange()))
|
||||
.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,8 +34,13 @@ import java.util.TimeZone;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.http.HttpHeaders}.
|
||||
@@ -436,11 +441,10 @@ public class HttpHeadersTests {
|
||||
new Locale.LanguageRange("*", 0.5)
|
||||
);
|
||||
assertEquals(expectedRanges, headers.getAcceptLanguage());
|
||||
assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocales().get(0));
|
||||
|
||||
assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocale());
|
||||
|
||||
headers.setAcceptLanguageAsLocale(Locale.FRANCE);
|
||||
assertEquals(Locale.FRANCE, headers.getAcceptLanguageAsLocale());
|
||||
headers.setAcceptLanguageAsLocales(Collections.singletonList(Locale.FRANCE));
|
||||
assertEquals(Locale.FRANCE, headers.getAcceptLanguageAsLocales().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -82,8 +82,8 @@ public class RequestContext {
|
||||
this.model = model;
|
||||
this.messageSource = messageSource;
|
||||
|
||||
Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
|
||||
this.locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
|
||||
List<Locale> locales = exchange.getRequest().getHeaders().getAcceptLanguageAsLocales();
|
||||
this.locale = locales.isEmpty() ? Locale.getDefault() : locales.get(0);
|
||||
this.timeZone = TimeZone.getDefault(); // TODO
|
||||
|
||||
this.defaultHtmlEscape = null; // TODO
|
||||
|
||||
@@ -200,8 +200,8 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
|
||||
Model model = result.getModel();
|
||||
MethodParameter parameter = result.getReturnTypeSource();
|
||||
|
||||
Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
|
||||
Locale locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
|
||||
List<Locale> locales = exchange.getRequest().getHeaders().getAcceptLanguageAsLocales();
|
||||
Locale locale = locales.isEmpty() ? Locale.getDefault() : locales.get(0);
|
||||
|
||||
Class<?> clazz = valueType.getRawClass();
|
||||
if (clazz == null) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -171,8 +172,8 @@ public class FreeMarkerView extends AbstractUrlBasedView {
|
||||
logger.debug("Rendering FreeMarker template [" + getUrl() + "].");
|
||||
}
|
||||
|
||||
Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
|
||||
Locale locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
|
||||
List<Locale> locales = exchange.getRequest().getHeaders().getAcceptLanguageAsLocales();
|
||||
Locale locale = locales.isEmpty() ? Locale.getDefault() : locales.get(0);
|
||||
|
||||
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user