Access translated i18n messages from Scala templates (Play! Internationalization)

限于喜欢 提交于 2020-01-01 19:05:20

问题


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?

回答1:


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"



回答2:


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")



来源:https://stackoverflow.com/questions/10063349/access-translated-i18n-messages-from-scala-templates-play-internationalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!