How is js-translation.json file generated?

static-content
localization
asset
javascript
knockout
js-translation-json
static-content-deploy

(Dmitry Fedyuk) #1

If you want just to use JavaScript translation read the topics:

Here is in deep internal explanation of how the JavaScript translation dictionary js-translation.json is generated.

Step 1

Client part of Magento 2 requests the js-translation.json file from the server part.

Step 2

The js-translation.json file is generated dynamically.
The server uses \Magento\Translation\Model\Json\PreProcessor class instance to handle the request:

Step 3

The \Magento\Translation\Model\Json\PreProcessor class instance parses the Knockout templates and JavaScript files to create a dictionary:

The parsing is implemented using regular expressions:


How to translate Knockout templates?
How to make the JavaScript strings localizable (translatable)
How is a JavaScript translation dictionary («js-translation.json») generated on the static assets deployment («bin/magento setup:static-content:deploy»)?
(Dmitry Fedyuk) #2

A more comprehensive analysis after 2 years:

See also: