Lets dive deeper into how Magento 2 generates the translations. Lets start at the very surface - the one thing we can see from the browser.
We know that in localstorage the translations are stored with key mage-translation-storage. So lets search for this key in the core code.
The js-translation.json is located at the theme root for example pub/static/frontend/Vendor/theme/en_US/js-translation.json
The file is automatically generated during static content deploy. Lets keep going backwards to see the flow.
php bin/magento setup:static-content:deploy
The file is deployed in module-deploy/Service/DeployTranslationsDictionary.php which is called in deploy during static content deploy.
As we can see in the function the actual data however is gathered in the \Magento\Translation\Model\Js\DataProvider
A couple things happening here:
First we are getting all the js files using \Magento\Translation\Model\Js\DataProvider::$filesUtility.
Then we are going through each file path, reading the file content and parsing translation phrases using \Magento\Translation\Model\Js\DataProvider::getPhrases.
Once we have found a phrase we are attempting to render it using \Magento\Framework\Phrase\Renderer\Composite::render
The composite renderer is defined in translation modules di.xml:
Once we have rendered the translation we compare it with the original phrase. If the translation differs then we add it to the dictionary.
Ok, now we know how the file content is generated. Last step is to understand how the file genration is triggered. If we take a look at the usages of the class we can see that the processor is defined here:
The JS\PreProcessor is responsible for replacing translation calls in js files to translated strings. The PreProcessor pool is implemented in framework \Magento\Framework\View\Asset\PreProcessor\Pool.
The pool process is calling getPreProcessors which picks up our class:
The Pool itself is trigger by a call in \Magento\Framework\App\View\Asset\Publisher::publishAsset which occurs during \Magento\Deploy\Service\DeployStaticFile::deployFile.
Normally you will never have to go as deep in core code but it is quite interesting to browse through and can be useful in case any bugs appear.
Hopefully this does give you a bit of insight on how the process works. It is certainly fun to go through and see various methods and strategies used in Magento 2 core.