If you want just to use JavaScript translation read the topics:
- How to make JavaScript strings localizable (translatable)
- How to make strings in Knockout templates translatable
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: