how could I set up the language depending on the subdomain?

Asked by marc

It's in the summary, bascially I would like that when the user is on:
http://en.monsite.com
The website is in english.

When the user is on:
http://fr.monsite.com
The website is in french.

I would like to avoid to have something in each method of my controllers and I would like the user to be able to switch language without clicking on a link which will send him back to the homepage.

Question information

Language:
English Edit question
Status:
Solved
For:
play framework Edit question
Assignee:
No assignee Edit question
Solved by:
marc
Solved:
Last query:
Last reply:

This question was reopened

  • by marc
Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#1

Hi,

Basicly, to change language you should simply have to call play.libs.Lang.change (). This will switch language +place a "lang" cookie, that will remember the "default" lang for all subsequent requests.

Alternatively, if you have a root controller, you can do without this without a cookie, in a @before, examining the request:
if (request.domain.equals("en.monsite.com")) {
Lang.set("en");
}

Revision history for this message
marc (marc-ttux) said :
#2

Rhank you for your answer, how can I have a "root" controller?

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#3

By root controller, we usually mean something like a myBaseController extends play.mvc.Controller. Then all your controllers in your app extends myBaseController. On this controller, if you have a @Before method, it is applied to all the actions of your application (plays looks for @before methods in the whole class hierarchy).

Revision history for this message
marc (marc-ttux) said :
#4

Perfect, so that's what I am going to do. Cheers.

Revision history for this message
marc (marc-ttux) said :
#5

I have created a Class RootController which extends Controller and then I have a method:
@Before
public static void setLanguage() {
      ...
}

I have changed all my Controller to extend RootController

I get the following:
No route able to invoke action RootController.setLanguage with arguments {} was found.

I then tried to change public static void to a simple public void but the method is not called.

What am I doing wrong?

Revision history for this message
marc (marc-ttux) said :
#6

I found it in the manual just not public