Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transientLang methods for RequestBuilder #8876

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,51 @@ public void testClearRequestTransientLang() {
assertFalse(request.withoutTransientLang().transientLang().isPresent());
}

@Test
public void testAddATransientLangToRequestBuilder() {
RequestBuilder builder = new RequestBuilder().uri("http://www.playframework.com/");

Lang lang = new Lang(Locale.GERMAN);
Request request = builder.transientLang(lang).build();

assertTrue(request.transientLang().isPresent());
assertEquals(lang, request.attrs().get(Messages.Attrs.CurrentLang));
}

@Test
public void testAddATransientLangByCodeToRequestBuilder() {
RequestBuilder builder = new RequestBuilder().uri("http://www.playframework.com/");

String lang = "de";
Request request = builder.transientLang(lang).build();

assertTrue(request.transientLang().isPresent());
assertEquals(Lang.forCode(lang), request.attrs().get(Messages.Attrs.CurrentLang));
}

@Test
public void testAddATransientLangByLocaleToRequestBuilder() {
RequestBuilder builder = new RequestBuilder().uri("http://www.playframework.com/");

Locale locale = Locale.GERMAN;
Request request = builder.transientLang(locale).build();

assertTrue(request.transientLang().isPresent());
assertEquals(new Lang(locale), request.attrs().get(Messages.Attrs.CurrentLang));
}

@Test
public void testClearRequestBuilderTransientLang() {
Lang lang = new Lang(Locale.GERMAN);
RequestBuilder builder = new RequestBuilder().uri("http://www.playframework.com/").transientLang(lang);

assertTrue(builder.build().transientLang().isPresent());
assertEquals(Optional.of(lang), builder.transientLang());

// Language attr should be removed
assertFalse(builder.withoutTransientLang().build().transientLang().isPresent());
}

@Test
public void testFlash() {
Application app = new GuiceApplicationBuilder().build();
Expand Down
50 changes: 50 additions & 0 deletions framework/src/play/src/main/java/play/mvc/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,56 @@ public RequestBuilder clientCertificateChain(List<X509Certificate> clientCertifi
));
return this;
}

/**
* Sets the transient language.
*
* @param lang The language to use.
* @return the builder instance
*/
public RequestBuilder transientLang(Lang lang) {
req = req.withTransientLang(lang);
return this;
}

/**
* Sets the transient language.
*
* @param code The language to use.
* @return the builder instance
*/
public RequestBuilder transientLang(String code) {
req = req.withTransientLang(code);
return this;
}

/**
* Sets the transient language.
*
* @param locale The language to use.
* @return the builder instance
*/
public RequestBuilder transientLang(Locale locale) {
req = req.withTransientLang(locale);
return this;
}

/**
* Removes the transient language.
*
* @return the builder instance
*/
public RequestBuilder withoutTransientLang() {
req = req.withoutTransientLang();
return this;
}

/**
* @return The current transient language of this builder instance.
*/
Optional<Lang> transientLang() {
return OptionConverters.toJava(req.transientLang()).map(play.api.i18n.Lang::asJava);
}
}

/**
Expand Down