Reputation: 1057
I am having application using play framework 2.3.x that I transform into 2.4.x. As part of migration, I am transforming services and ran into challenge with i18n. How can I inject Lang or get it's value?
class MyServiceUsedByController {
@Inject
private MessagesApi messagesApi;
void someFoo() {
Lang lang = ///How to get it?
commentToSaveToDb = messagesApi.get(lang, "comment.addedPlace");
}
}
I cannot use static because I am using that service at application start, so application is not running yet and static call to Messages fail.
Upvotes: 1
Views: 319
Reputation: 8263
On the server i18n have sense only in the request context. From the box it's based on the cookies, you can change it, to select language based on the url: Play Framework 2 Language Code in URL Concept?
Your situation - application starts, you are not in the request context, so you can take any language you like.
Suppose you want to take default server language, then just use trivial java method:
String javaServerLang = Locale.getDefault().getLanguage();
And convert it to the play Lang
Lang playLang = Lang.forCode(javaServerLang);
In the request context you can even set this language in to the context, I am not sure if you can do this at startup:
Context.current().changeLang(playLang);
Upvotes: 1