Skip to content

Commit bd8af55

Browse files
committed
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
1 parent fa56361 commit bd8af55

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ public List<MediaType> getAccept() {
440440
}
441441

442442
/**
443-
* Set the acceptable language ranges,
444-
* as specified by the {@literal Accept-Language} header.
443+
* Set the acceptable language ranges, as specified by the
444+
* {@literal Accept-Language} header.
445445
* @see Locale.LanguageRange
446446
* @since 5.0
447447
*/
@@ -458,8 +458,12 @@ public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
458458
}
459459

460460
/**
461-
* Return the acceptable language ranges,
462-
* as specified by the {@literal Accept-Language} header
461+
* Return the acceptable language ranges from the
462+
* {@literal Accept-Language} header
463+
* <p>If you only need the most preferred locale use
464+
* {@link #getAcceptLanguageAsLocale()} or if you need to filter based on
465+
* a list of supporeted locales you can pass the returned list to
466+
* {@link Locale#filter(List, Collection)}.
463467
* @see Locale.LanguageRange
464468
* @since 5.0
465469
*/
@@ -473,18 +477,20 @@ public List<Locale.LanguageRange> getAcceptLanguage() {
473477

474478
/**
475479
* A variant of {@link #getAcceptLanguage()} that converts each
476-
* {@link java.util.Locale.LanguageRange} to a {@link Locale}.
480+
* {@link java.util.Locale.LanguageRange} to a {@link Locale} and returns
481+
* the first one on the list.
477482
* @since 5.0
478483
*/
479-
public List<Locale> getAcceptLanguageAsLocales() {
484+
public Locale getAcceptLanguageAsLocale() {
480485
List<Locale.LanguageRange> ranges = getAcceptLanguage();
481486
if (ranges.isEmpty()) {
482-
return Collections.emptyList();
487+
return null;
483488
}
484489
return ranges.stream()
485490
.map(range -> Locale.forLanguageTag(range.getRange()))
486491
.filter(locale -> StringUtils.hasText(locale.getDisplayName()))
487-
.collect(Collectors.toList());
492+
.findFirst()
493+
.orElse(null);
488494
}
489495

490496
/**

spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,7 @@ public void acceptLanguage() {
437437
);
438438
assertEquals(expectedRanges, headers.getAcceptLanguage());
439439

440-
List<Locale> expectedLocales = Arrays.asList(
441-
Locale.forLanguageTag("fr-ch"),
442-
Locale.forLanguageTag("fr"),
443-
Locale.forLanguageTag("en"),
444-
Locale.forLanguageTag("de")
445-
);
446-
assertEquals(expectedLocales, headers.getAcceptLanguageAsLocales());
440+
assertEquals(Locale.forLanguageTag("fr-ch"), headers.getAcceptLanguageAsLocale());
447441
}
448442

449443
@Test

0 commit comments

Comments
 (0)