Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allows to change the current Lang in Java API

  • Loading branch information...
commit 1cd51587af13d4d6fb8ee4fb1d4a8482263f5ce1 1 parent ae32dfb
@julienrf julienrf authored
View
18 framework/src/play/src/main/java/play/i18n/Lang.java
@@ -42,12 +42,26 @@ public String code() {
public java.util.Locale toLocale() {
return underlyingLang.toLocale();
}
+
+ @Override
+ public boolean equals(Object other) {
+ return underlyingLang.equals(other);
+ }
+
+ @Override
+ public int hashCode() {
+ return underlyingLang.hashCode();
+ }
/**
* Create a Lang value from a code (such as fr or en-US).
*/
public static Lang forCode(String code) {
- return new Lang(play.api.i18n.Lang.apply(code));
+ try {
+ return new Lang(play.api.i18n.Lang.apply(code));
+ } catch (Exception e) {
+ return null;
+ }
}
/**
@@ -73,5 +87,5 @@ public static Lang preferred(List<Lang> langs) {
}
return new Lang(play.api.i18n.Lang.preferred(Scala.toSeq(result), play.api.Play.current()));
}
-
+
}
View
2  framework/src/play/src/main/java/play/i18n/Messages.java
@@ -35,7 +35,7 @@ public static String get(Lang lang, String key, Object... args) {
*/
public static String get(String key, Object... args) {
Buffer<Object> scalaArgs = scala.collection.JavaConverters.asScalaBufferConverter(Arrays.asList(args)).asScala();
- return play.api.i18n.Messages.apply(key, scalaArgs, play.mvc.Http.Context.Implicit.lang());
+ return play.api.i18n.Messages.apply(key, scalaArgs, play.mvc.Http.Context.current().lang());
}
}
View
27 framework/src/play/src/main/java/play/mvc/Controller.java
@@ -1,13 +1,10 @@
package play.mvc;
-import play.api.*;
-
+import play.i18n.Lang;
import play.mvc.Http.*;
-import play.mvc.Result.*;
import play.data.*;
-import java.util.*;
/**
* Superclass for a Java-based controller.
@@ -31,9 +28,19 @@ public static Request request() {
/**
* Returns the current lang.
*/
- public static play.i18n.Lang lang() {
- return play.i18n.Lang.preferred(Context.current().request().acceptLanguages());
+ public static Lang lang() {
+ return Http.Context.current().lang();
+ }
+
+ /**
+ * Change durably the lang for the current user
+ * @param code New lang code to use (e.g. "fr", "en_US", etc.)
+ * @return true if the requested lang was supported by the application, otherwise false.
+ */
+ public static boolean changeLang(String code) {
+ return Http.Context.current().changeLang(code);
}
+
/**
* Returns the current HTTP response.
@@ -97,28 +104,28 @@ public static DynamicForm form() {
* Instantiates a new form that wraps the specified class.
*/
public static <T> Form<T> form(Class<T> clazz) {
- return new Form(clazz);
+ return new Form<T>(clazz);
}
/**
* Instantiates a new form that wraps the specified class.
*/
public static <T> Form<T> form(String name, Class<T> clazz) {
- return new Form(name, clazz);
+ return new Form<T>(name, clazz);
}
/**
* Instantiates a new form that wraps the specified class.
*/
public static <T> Form<T> form(String name, Class<T> clazz, Class<?> group) {
- return new Form(name, clazz, group);
+ return new Form<T>(name, clazz, group);
}
/**
* Instantiates a new form that wraps the specified class.
*/
public static <T> Form<T> form(Class<T> clazz, Class<?> group) {
- return new Form(null, clazz, group);
+ return new Form<T>(null, clazz, group);
}
}
View
48 framework/src/play/src/main/java/play/mvc/Http.java
@@ -6,6 +6,8 @@
import org.w3c.dom.*;
import org.codehaus.jackson.*;
+import play.i18n.Lang;
+
/**
* Defines HTTP standard objects.
*/
@@ -38,7 +40,9 @@ public static Context current() {
private final Response response;
private final Session session;
private final Flash flash;
-
+
+ private Lang lang = null;
+
/**
* Creates a new HTTP context.
@@ -88,7 +92,43 @@ public Session session() {
public Flash flash() {
return flash;
}
-
+
+ /**
+ * @return the current lang.
+ */
+ public Lang lang() {
+ if (lang != null) {
+ return lang;
+ } else {
+ Cookie cookieLang = request.cookies().get(langCookieName());
+ if (cookieLang != null) {
+ Lang lang = Lang.forCode(cookieLang.value());
+ if (lang != null) return lang;
+ }
+ return Lang.preferred(request().acceptLanguages());
+ }
+ }
+
+ /**
+ * Change durably the lang for the current user.
+ * @param code New lang code to use (e.g. "fr", "en_US", etc.)
+ * @return true if the requested lang was supported by the application, otherwise false.
+ */
+ public boolean changeLang(String code) {
+ Lang lang = Lang.forCode(code);
+ if (Lang.availables().contains(lang)) {
+ this.lang = lang;
+ response.setCookie(langCookieName(), code);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private String langCookieName() {
+ return play.Configuration.root().getString("application.lang.cookie", "PLAY_LANG");
+ }
+
/**
* Free space to store your request specific data
*/
@@ -130,8 +170,8 @@ public static Session session() {
/**
* Returns the current lang.
*/
- public static play.i18n.Lang lang() {
- return play.i18n.Lang.preferred(Context.current().request().acceptLanguages());
+ public static Lang lang() {
+ return Context.current().lang();
}
/**
View
9 framework/test/integrationtest-java/app/controllers/Application.java
@@ -44,4 +44,13 @@ public Result apply(String a) {
}));
}
+ public static Result setLang(String code) {
+ changeLang(code);
+ return ok(lang().code());
+ }
+
+ public static Result hello() {
+ return ok(play.i18n.Messages.get("hello"));
+ }
+
}
View
2  framework/test/integrationtest-java/conf/application.conf
@@ -45,3 +45,5 @@ logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
+
+application.langs="fr,en"
View
1  framework/test/integrationtest-java/conf/messages.en
@@ -0,0 +1 @@
+hello=Hello
View
1  framework/test/integrationtest-java/conf/messages.fr
@@ -0,0 +1 @@
+hello=Bonjour
View
2  framework/test/integrationtest-java/conf/routes
@@ -5,6 +5,8 @@
GET / controllers.Application.index(name = "Guest")
GET /key controllers.Application.key
GET /async controllers.Application.asyncResult
+GET /lang/:code controllers.Application.setLang(code)
+GET /hello controllers.Application.hello()
GET /:name controllers.Application.index(name)
POST /json controllers.Application.getIdenticalJson()
View
35 framework/test/integrationtest-java/test/test/LangSpec.scala
@@ -0,0 +1,35 @@
+package test
+
+import org.specs2.mutable.Specification
+import play.api.test.TestServer
+import play.api.test.Helpers._
+
+object LangSpec extends Specification {
+
+ "The Application’s lang" can {
+
+ "be changed durably" in {
+ running(TestServer(3333), HTMLUNIT) { browser =>
+ // Set the lang to fr
+ browser.goTo("http://localhost:3333/lang/fr")
+ browser.pageSource must equalTo ("fr")
+ browser.goTo("http://localhost:3333/hello")
+ browser.pageSource must equalTo ("Bonjour")
+
+ // Change it for en
+ browser.goTo("http://localhost:3333/lang/en")
+ browser.pageSource must equalTo ("en")
+ browser.goTo("http://localhost:3333/hello")
+ browser.pageSource must equalTo ("Hello")
+
+ // Try to change it for an unsupported lang
+ browser.goTo("http://localhost:3333/lang/ja")
+ browser.pageSource must equalTo ("en")
+ browser.goTo("http://localhost:3333/hello")
+ browser.pageSource must equalTo ("Hello")
+ }
+ }
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.