Skip to content

Commit 36da299

Browse files
committed
Add set/getContentLanguage() to HttpHeaders
Issue: SPR-14536
1 parent 9764d57 commit 36da299

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

+26
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,32 @@ public ContentDisposition getContentDisposition() {
762762
return ContentDisposition.empty();
763763
}
764764

765+
/**
766+
* Set the {@link Locale} of the content language,
767+
* as specified by the {@literal Content-Language} header.
768+
* <p>Use {@code set(CONTENT_LANGUAGE, ...)} if you need
769+
* to set multiple content languages.</p>
770+
*/
771+
public void setContentLanguage(Locale locale) {
772+
Assert.notNull(locale, "'locale' must not be null");
773+
set(CONTENT_LANGUAGE, locale.toLanguageTag());
774+
}
775+
776+
/**
777+
* Return the first {@link Locale} of the content languages,
778+
* as specified by the {@literal Content-Language} header.
779+
* <p>Returns {@code null} when the content language is unknown.
780+
* <p>Use {@code getValuesAsList(CONTENT_LANGUAGE)} if you need
781+
* to get multiple content languages.</p>
782+
*/
783+
public Locale getContentLanguage() {
784+
return getValuesAsList(CONTENT_LANGUAGE)
785+
.stream()
786+
.findFirst()
787+
.map(Locale::forLanguageTag)
788+
.orElse(null);
789+
}
790+
765791
/**
766792
* Set the length of the body in bytes, as specified by the
767793
* {@code Content-Length} header.

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

+11
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,15 @@ public void acceptLanguage() {
439439
assertArrayEquals(languageArray, languages.toArray());
440440
}
441441

442+
@Test
443+
public void contentLanguage() {
444+
assertNull(headers.getContentLanguage());
445+
headers.setContentLanguage(Locale.FRANCE);
446+
assertEquals(Locale.FRANCE, headers.getContentLanguage());
447+
assertEquals("fr-FR", headers.getFirst(HttpHeaders.CONTENT_LANGUAGE));
448+
headers.clear();
449+
headers.set(HttpHeaders.CONTENT_LANGUAGE, Locale.GERMAN.toLanguageTag() + ", " + Locale.CANADA);
450+
assertEquals(Locale.GERMAN, headers.getContentLanguage());
451+
}
452+
442453
}

0 commit comments

Comments
 (0)