Playframework 2.2 java: how to install language (i18n) from subdomain

How to set the language (i18n) not only from the settings of the users browser, but also from subdomains (which should have a higher priority) in playframework 2.2 (java)?

The following should work:

  • example.com → English or German, depending on your browser settings.
  • en.example.com → force english
  • de.example.com → force german

The user should be able to switch between subdomains without losing a session.

Since I have many java controllers, it would be great if the solution worked in a centralized place (for example, Global.java with a filter that might be in scala).

+4
source share
1 answer

You can use the play.mvc.Controllersuperclass method for this .

Then you need to tell your language resolver which domain uses the language in which we default, using application.confor database entries for this. Further, depending on what you want to achieve, simply use the global class to intercept all of your request or create a simple action that will change the language and they will return to one page (so that the user can decide which language he wants to use).

- , cookie, , (.. ). , , , ;)

:

.

public Action onRequest(final Http.Request request, final Method actionMethod) {

    if (request.host().equals("de.yourdomain.tld")
            && (request.cookie("PLAY_LANG") == null || !request.cookie("PLAY_LANG").value().equals("de"))) {
        return new Action.Simple() {
            public Result call(Http.Context ctx) throws Throwable {
                ctx.changeLang("de");
                return redirect(request.path());
            }
        };
    } else {
        return super.onRequest(request, actionMethod);
    }
}

, de lang application.conf, . PLAY_LANG Play cookie .

+4

Source: https://habr.com/ru/post/1526997/


All Articles