getAcceptLanguageAsLocale(s) returns most preferred Locale
An update on the last commit switching from: List<Locale> getAcceptLanguageAsLocales() to Locale getAcceptLanguageAsLocale() This best supports the scenario for the most preferred Locale. If there is a need to look at the prioritized list of languages it's best to use Locale.filter with the LocaleRange's. This is explained in the Javadoc for getAcceptLanguage(). Issue: SPR-15024
This commit is contained in:
parent
fa56361ad2
commit
bd8af55bc7
|
@ -440,8 +440,8 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the acceptable language ranges,
|
* Set the acceptable language ranges, as specified by the
|
||||||
* as specified by the {@literal Accept-Language} header.
|
* {@literal Accept-Language} header.
|
||||||
* @see Locale.LanguageRange
|
* @see Locale.LanguageRange
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
|
@ -458,8 +458,12 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the acceptable language ranges,
|
* Return the acceptable language ranges from the
|
||||||
* as specified by the {@literal Accept-Language} header
|
* {@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
|
||||||
|
* {@link Locale#filter(List, Collection)}.
|
||||||
* @see Locale.LanguageRange
|
* @see Locale.LanguageRange
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
|
@ -473,18 +477,20 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A variant of {@link #getAcceptLanguage()} that converts each
|
* A variant of {@link #getAcceptLanguage()} that converts each
|
||||||
* {@link java.util.Locale.LanguageRange} to a {@link Locale}.
|
* {@link java.util.Locale.LanguageRange} to a {@link Locale} and returns
|
||||||
|
* the first one on the list.
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public List<Locale> getAcceptLanguageAsLocales() {
|
public Locale getAcceptLanguageAsLocale() {
|
||||||
List<Locale.LanguageRange> ranges = getAcceptLanguage();
|
List<Locale.LanguageRange> ranges = getAcceptLanguage();
|
||||||
if (ranges.isEmpty()) {
|
if (ranges.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return null;
|
||||||
}
|
}
|
||||||
return ranges.stream()
|
return ranges.stream()
|
||||||
.map(range -> Locale.forLanguageTag(range.getRange()))
|
.map(range -> Locale.forLanguageTag(range.getRange()))
|
||||||
.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
|
.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
|
||||||
.collect(Collectors.toList());
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -437,13 +437,7 @@ public class HttpHeadersTests {
|
||||||
);
|
);
|
||||||
assertEquals(expectedRanges, headers.getAcceptLanguage());
|
assertEquals(expectedRanges, headers.getAcceptLanguage());
|
||||||
|
|
||||||
List<Locale> expectedLocales = Arrays.asList(
|
assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocale());
|
||||||
Locale.forLanguageTag("fr-ch"),
|
|
||||||
Locale.forLanguageTag("fr"),
|
|
||||||
Locale.forLanguageTag("en"),
|
|
||||||
Locale.forLanguageTag("de")
|
|
||||||
);
|
|
||||||
assertEquals(expectedLocales, headers.getAcceptLanguageAsLocales());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue