Play Framework 2 Language Code in URL Concept?

Adrian Pop

The manual about i18n is short: https://www.playframework.com/documentation/2.4.x/ScalaI18N Is there anything more that explains the concept for handling the user's language choice?

What I'd like to achieve is what so many other sites do: put the language code into the URL

And then, when calling (Java) Lang.defaultLang().language() or from a Scala template @lang.language I'd like to get that value. Of course with the usual resolving, it must be in the application.conf play.i18n.langs = [ "en","de" ]

Do I really need to read it from the URL myself?

Also, in the routes file, isn't there a concept already for mapping it directly to the language resolving?

The alternative is to:

  1. duplicate all routes in the routes file per language, or use a regex for the lang code
  2. in each controller call a method for setting the language
Andriy Kuba

You can implement custom request handler and resolve language on every request. This is the same idea like your "in each controller call a method for setting the language" but you need to write the code only in one place - legacy GlobalSettings.onRequest or new HttpRequestHandler.createAction

There is a very good description about realisation i18n in play based on the url part, the only one thing - it's for 2.0.4, so I suppose you would use HttpRequestHandler.createAction but GlobalSettings.onRequest.

Guide: http://www.flowstopper.org/2013/01/i18n-play-framework-java-app-website.html

Migration guide: https://www.playframework.com/documentation/2.4.x/GlobalSettings

Custom Request Handlers: https://www.playframework.com/documentation/2.4.x/JavaHttpRequestHandlers

Live examples from my project (Play 2.4.3, Java)

application.conf

play.i18n.langs = [ "en", "de", "fr", "ua" ]
play.http.requestHandler = "plugins.RequestHandler"

routes

# Home page
GET     /$lang<[a-z]{2}>/home       controllers.Application.home(lang:String)

plugins/RequestHandler.java

package plugins;

import play.http.DefaultHttpRequestHandler;
import play.libs.F;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.lang.reflect.Method;

public class RequestHandler extends DefaultHttpRequestHandler {

    @Override
    public Action createAction(Http.Request request, Method actionMethod) {
        return new Action.Simple() {
            @Override
            public F.Promise<Result> call(Http.Context ctx) throws Throwable {
                Path path = Paths.get(ctx.request().path());
                String lang = path.getName(0).toString();
                // we detect language only by URL path, cookies does not used 
                ctx.setTransientLang(lang);
                return delegate.call(ctx);
            }
        };
    }
}

controllers/Application.java

package controllers;

import play.*;
import play.mvc.*;
import play.i18n.Lang;

import views.html.*;

public class Application extends Controller {

    public Result home(String lang){
       return ok(ctx().lang().code());
    }

}

This App would give results

http://localhost:9000/de/home -> "de"

http://localhost:9000/en/home -> "en"

http://localhost:9000/dk/home -> "exception: Language not supported in this application: Lang(dk,) not in Lang.availables()"

Take in to attention: Lang.defaultLang().language() will not return current request language. You need to call ctx().lang() for returning current request language.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect with URL fragment with Play framework 2

From Dev

Facebook Like Extracting URL Data with Play Framework 2.x

From Dev

Play Framework 2.x always send single response code

From Dev

Objectify with Play Framework 2

From Dev

Configure QueryDSL on Play 2 Framework

From Dev

Play Framework 2 view declaration

From Dev

Use MongoDB with Play 2 Framework

From Dev

Configure QueryDSL on Play 2 Framework

From Dev

Play 2! framework multithreading issue

From Dev

How can I consume a web service with Play Framework 2 when the url has a colon in it?

From Dev

Can i define multiple URL for an action in one route in Play Framework 2.x?

From Dev

Why can I not mix Scala code with HTML here in Play Framework 2 views?

From Dev

Redirect to external url in play framework 2.0

From Dev

Play Framework WS.url stuck forever

From Dev

Play Framework 2.2 : Get URL of the requesting page

From Dev

Redirect to external url in play framework 2.0

From Dev

Play framework external Javascript URL syntax

From Dev

Play framework get Response status code in filter

From Dev

Confusion about Play framework's code

From Dev

How to run code on startup in Play! framework 2.4

From Dev

Slick code generator with Postgres in Play Framework

From Dev

Play framework async code dos not wait to result

From Dev

Play Framework 2.2.0 - Force the language using Filter and Global object

From Dev

Set Accept-Language on PhantomJSDriver in a Play Framework Specification

From Dev

play framework 2.3 change template language without extra request

From Dev

Scala Play Framework how to set language for current request?

From Dev

Play Framework 2.2.0 - Force the language using Filter and Global object

From Dev

Url of Zend Framework 2

From Java

Play Router: How to add a language-sensitive URL redirect rule?

Related Related

  1. 1

    Redirect with URL fragment with Play framework 2

  2. 2

    Facebook Like Extracting URL Data with Play Framework 2.x

  3. 3

    Play Framework 2.x always send single response code

  4. 4

    Objectify with Play Framework 2

  5. 5

    Configure QueryDSL on Play 2 Framework

  6. 6

    Play Framework 2 view declaration

  7. 7

    Use MongoDB with Play 2 Framework

  8. 8

    Configure QueryDSL on Play 2 Framework

  9. 9

    Play 2! framework multithreading issue

  10. 10

    How can I consume a web service with Play Framework 2 when the url has a colon in it?

  11. 11

    Can i define multiple URL for an action in one route in Play Framework 2.x?

  12. 12

    Why can I not mix Scala code with HTML here in Play Framework 2 views?

  13. 13

    Redirect to external url in play framework 2.0

  14. 14

    Play Framework WS.url stuck forever

  15. 15

    Play Framework 2.2 : Get URL of the requesting page

  16. 16

    Redirect to external url in play framework 2.0

  17. 17

    Play framework external Javascript URL syntax

  18. 18

    Play framework get Response status code in filter

  19. 19

    Confusion about Play framework's code

  20. 20

    How to run code on startup in Play! framework 2.4

  21. 21

    Slick code generator with Postgres in Play Framework

  22. 22

    Play framework async code dos not wait to result

  23. 23

    Play Framework 2.2.0 - Force the language using Filter and Global object

  24. 24

    Set Accept-Language on PhantomJSDriver in a Play Framework Specification

  25. 25

    play framework 2.3 change template language without extra request

  26. 26

    Scala Play Framework how to set language for current request?

  27. 27

    Play Framework 2.2.0 - Force the language using Filter and Global object

  28. 28

    Url of Zend Framework 2

  29. 29

    Play Router: How to add a language-sensitive URL redirect rule?

HotTag

Archive