With static Messages.get function removed from Play 2.6, how do we get translation/localization in model/class?

23 views
Skip to first unread message

Jean-François Côté

unread,
Aug 11, 2017, 8:55:32 AM8/11/17
to Play Framework, jc...@stingray.com
I have asked the question on StackOverflow too: https://stackoverflow.com/questions/45635698/with-static-messages-get-function-removed-from-play-2-6-how-do-we-get-translati

Here is a copy of it:

In Play 2.5, the function Messages.get was deprecated but we could still use it. The recommandation was to go all the way with injection.

I have done this everywhere I could (controller, service, etc.) but there is still places where a static function is needed. For example, I have a custom exception called InvalidEntityException which override the getLocalizedMessage.

@Override
public String getLocalizedMessage() {
    return String.format(Messages.get("error.entity.invalid"), type.name(), entityId);
}

Since it's an override function, I can't pass the object for translation. And since it's an exception class, I can't inject it neither.

This is just an example, I have the same problem with Models. Models can't be injected because they are retrieve using Ebean. But I have functions like validate() that throws some custom message that needs to be translated. In this case, I could pass the object for translation as a parameter but it seems wrong.

Any help would be appreciated.

Jülide Meyer

unread,
Aug 14, 2017, 10:11:49 AM8/14/17
to Play Framework, jc...@stingray.com
You can pass arguments into a constructor of your custom exception class. Your validate functions can return message keys. So you can resolve them later. 
Reply all
Reply to author
Forward
0 new messages