Dynamic Javascript Using Scala Template

I am trying to localize Javascript files. For example, I would:

var count = 0; $('#choices .choice').each(function(i) { $('input', this).each(function() { count++ $(this).attr('placeholder', '@Message("placeholder.choice") ' + count) }) }) 

This will obviously work if the Javascript file is inside the Scala HTML template, but I would rather have it in a dedicated file.

To begin with, I wonder if this is a good idea: how about caching a file if the contents can change? In this case, there is one parameter: is there a solution to this problem in the URL? For example: /assets/javascripts/:lang/my-file.js .

And the real question is: is this possible using Play! framework? It seems that Javascript templates are not supported (or I missed something). Is there any way to do this right?

+1
source share
2 answers

In fact, you don’t need to dynamically translate your Java scripts, it’s a waste of resources, instead they prepare static JS files, such as messages.en.js , messages.de.js , etc., and include the necessary file, based on the user language directly in the view.

Here you have a description of how to do this easily (JavaScript approach)

+1
source

There is a module that allows the internationalization of javascript, using the same mechanism as in game templates, take a look at: https://github.com/julienrf/play-jsmessages

It certainly fits your needs. I have been using it for some time with success. You can publish your translations via a javascript file, and then use browser caching with the proper fingerprint configuration.

0
source

Source: https://habr.com/ru/post/977742/


All Articles