Access translated i18n messages from Scala templat

2019-04-12 16:15发布

In my Play! 2.0 application I would like to define the following languages:

# The application languages
# ~~~~~
application.langs=en-GB,de-DE,nl-NL

I also have created 3 files that ends with the corresponding language codes:

Messages.en-GB
Messages.de-DE
Messages.nl-NL

When I start the application without any request for a translated key I get the following error message:

conf/application.conf: 12: Key 'de-DE' may not be followed by token: ',' (if you intended ',' to be part of the value for 'de-DE', try enclosing the value in double quotes)

Also when trying to access a message from the Scala template I still see the same message. I request the message by the following code:

@Messages("login.page")

The above changes I have done according to the Play manual: http://www.playframework.org/documentation/2.0/JavaI18N . So I have two questions:

  1. How can I set the default langauge and change it like in 1.2.4 (Lang.change("en-GB"))
  2. How to access the messages from the Scala templates?

2条回答
做自己的国王
2楼-- · 2019-04-12 16:53

Changing the language is not possible in Play! 2.0, see this discussion: http://groups.google.com/group/play-framework/browse_thread/thread/744d523c169333ac/5bfe28732a6efd89?show_docid=5bfe28732a6efd89

and this ticket: https://play.lighthouseapp.com/projects/82401-play-20/tickets/174-20-i18n-add-ability-to-define-implicit-lang-for-java-api#ticket-174-4

Although, when you register multiple languages you should enclose them in double qoutes, like this:

application.langs="en-GB,de-DE,nl-NL"

And then you can access them from the scala templates like this:

@Messages.get("login.title")

So currently the default language is the language that is defined in the file messages (without any prefix!!)

Or you can just use @Messages("not.logged.in")

查看更多
Fickle 薄情
3楼-- · 2019-04-12 17:03

In your scala file use:

<h1>@Messages("pack.key")</h1>

And in your java file use :

String title = Messages.get("pack.key");

Don't forget to add quote around your language list : conf/application.conf

application.langs="en-GB,de-DE,nl-NL"
查看更多
登录 后发表回答