I have a web application on Play. A web application consists of several pages. Each page has a small flag that allows the user to change the language (locale) from German to English and vice versa.
I handle this with a referer redirect:
def referer(implicit request: Request[AnyContent]) = request.headers.get(REFERER).getOrElse(mainUrl) def locale(l: String) = Authenticated { user => implicit request => Redirect(referer).withCookies(Cookie(LANG, if (l == "de" || l == "en") l else "de")) }
It is working fine. Well, at least for GET requests.
I have a specific page where the user must enter data on the form. Then this form is sent to the server. If errors are detected, the form displays again with error messages, as usual. Now, if the user wants to change the language (by clicking on the flag), the redirection to the referent does not work, because he is trying to use a GET request, and Play complains that there is no GET route for this method (which is true).
I solve this by caching the form and defining another method in which the form is taken from the cache:
It works, but I don't like it. It seems abnormal to create another entry in the routes and another method to simply address this problem. I need to add this hack for every POST route in my application!
Is there a more elegant solution?
Jaime ocampo
source share