2
votes

Is it possible to access Play's config values in Javascript? Specifically knowing the current lang that is selected? (v2.2.3)

EDIT:

More specifically I want to localize the components that I have on the client side, and I need to know the lang that play has used to render the page in the javascript file.

Play's i18n documentation:

On the first request from a new user, Play will guess the default language to use. It does so by parsing the HTTP Accept-language header. It will then save the chosen language in a PLAY_LANG cookie. So the next request will use the same language.

For some reason I don't see this cookie on the client side.

My question is a) Is there any other way to know the Play's lang on the client side ? b) why is this cookie not working ?

2
Have you tried this? Description says: Returns the current lang - fxfour

2 Answers

0
votes

Have a look at play.Application.Configuration().asMap()or one of the other methods of the Configuration class. You can read about it in the docs.

After having obtained the settings you are interested in, you can serve them via HTML/JS, but this is something you have to do manually.

But I'm not sure what you mean by the current lang that is selected. Play is stateless, the language depends on the client's request. In contrast, the values in your configuration are static, e.g. application.langs.

edit:

The lang method of a controller returns the best possible language that was determined (and thus is the "current language"), maybe that is what you want.

0
votes

Now that I've spent a day looking at how Play does things my question seems to be wrong. I'm just posting the answer here in case anyone has this problem.

In play 1.1.2 it sets the Play_lang cookie. In the version that I used 2.2.3 that cookie is not set.

I solved my problem by setting a cookie on the server side:

response().setCookie("Language", play.mvc.Controller.lang().code(), 360, "", "", false, false);

And reading it with JavaScript

var value = "; " + document.cookie;
var parts = value.split("; Language=");
if (parts.length == 2) { 
var culture = parts.pop().split(";").shift(); }