I have an internationalized Scala Play 2.7.x WebApp and have the usual routes e.g.
GET / controllers.ApplicationController.index
GET /page/somePage/ controllers.SomeController.somePage
GET /contact controllers.ContactController.view
Now I'd like to add a new route that will basically change-language-redirect to any target route. I implement this use-case by adding an additional route on top of routes like this:
GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
The idea is that every time you do e.g.
http://localhost:9000/en => will go to home page in english
http://localhost:9000/en/contact => will go to contact page in english
http://localhost:9000/es => will go to home page in spanish
http://localhost:9000/es/contact => will go to contact page in spanish
and so on. Unfortunately it doesn't always work e.g. the one included before /en/page/somePage/ it will not match it correctly to the first rule:
GET /$lang<(en|es)> controllers.ApplicationController.langRedirect(lang: String, target: String = "")
presumably because of the intermediate / ... how can I fix that?
For completeness here is my ApplicationController.langRedirect(...) implementation:
def langRedirect(lang: String, target: String = "") = silhouette.UserAwareAction.async { implicit request =>
Future.successful(Redirect("/" + target).withLang(Lang(lang)))
}
Using
Router.withPrefix, you can add langage code prefix to all your routes.Here is an example.
To enable
I18nRequestHandler, you have to add it to "application.conf".Also add supported languages to "application.conf".
This code forces all routes to have the language code prefix. If you need a exceptional routes such as "/" to let users choose its language, create custom routes and add it on
routeRequestmethod.Hope this is what you want ;)