Lesson 10: Internationalisation with the Scala Play Framework

Here I’ll demonstrate how I finally got internationalization working after some futile attempts.

Step 1: Create a Skeleton Application.
This is as per previous lessons:

Download the Play framework, unzip it, put it on your Path and use these commands:

$ cd
$ activator new lesson10
( at this point I choose option 4 – play-scala)
$ cd lesson10
$ activator eclipse
$ activator run

I now have a folder called lesson10 in my home directory.

Step 2: Modify the front page to use Internationalization
We need to create the default file to use for messages, called conf/messages – we’ll add a value to it:

my.greeting=Hello!

Now we add a messages file for another language we wish to support. We’ll add French. In a file conf/messages.fr add the following:

my.greeting=Bonjour!

We need to configure the app to look for this language though – modifying the application.langs entry in conf/application.conf as follows:

application.langs="en,fr"

Now we can use the my.greeting key in a template. We’ll just update the one we have, app/views/index.scala.html:

@(message: String)(implicit request:RequestHeader, lang:Lang)

@main("Welcome to Play") {
    @Messages("my.greeting")
}

The implicit request and lang are important – without them the language will default to that of the JVM and therefore, quite probably that of the Operating System.

Let’s try it with English as first choice in my Chrome browser:

In my browser I’ll add French as my main language:

And let’s try again:

It’s so simple…