-
Notifications
You must be signed in to change notification settings - Fork 0
ScalaI18N
A valid language code is specified by a valid ISO Language Code, optionally followed by a valid ISO Country Code, such as fr or en_US.
To start you need to specify the languages supported by your application in the conf/application.conf file:
application.langs=en,en_US,fr
You can externalize messages in the conf/messages.xxx files.
The default conf/messages file matches all languages. Additionally you can specify language-specific message files like conf/messages.fr or conf/messages.en_US.
Then you can retrieve messages using the play.api.i18n.Messages object:
val title = Messages.get("home.title")
All i18n API take an implicit play.api.i18.Lang argument retrieved from the current scope. You can also specify it explicitly:
val title = Messages.get("home.title", Lang("fr"))
Note: If you have an implicit
Requestin the scope, it will provide an implicitLangvalue corresponding to the preferred language extracted from theAccept-Languageheader and matching one the application supported languages.
The messages can be formatted using the java.text.MessageFormat library. For example:
Assuming you have message defined like:
files.summary=The disk {1} contains {0} file(s).
You can then specify parameters as:
Messages.get("files.summary", d.files.length, d.name)
You can retrieve supported languages by a specific HTTP request:
def index = Action { request =>
Ok("Languages: " + request.acceptLanguages.map(_.code).mkString(", "))
}
- HTTP programming
- Asynchronous HTTP programming
- The template engine
- HTTP form submission and validation
- Working with JSON
- Working with XML
- Handling file upload
- Accessing an SQL database
- Using the Cache
- Calling WebServices
- Integrating with Akka
- Internationalization
- The application Global object
- Testing your application