Magento 2 – List Translation Strings from a Module

i18nlocalisationmagento2translate

In order to build this CSV app/code/Vendor/Module/i18n/en_US.csv, I want to get an array from my module with all the translation-ready strings from all files types (.php, .phtml, .js, .xml).

.php & .phtml

__('string')

.js

$.mage.__('string')

.xml

<group id="general" translate="label" type="text" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="1">

and

<field id="showmethod" translate="label" type="select" sortOrder="280" showInDefault="1" showInWebsite="1" showInStore="0">

and

<argument name="header" xsi:type="string" translate="true">Report Date</argument>

and

<item name="label" xsi:type="string" translate="true">CSV</item>

How can I get a list (array) of all the translation-ready strings from a module for creating a CSV file?

Best Answer

Magento 2 already has a command that can collect all the texts from a certain folder that are translatable.
Since one module can mean 1 folder, let's say your module name is app/code/Vendor/Module you can collect all translatable texts like this:

bin/magento i18n:collect-phrases app/code/Vendor/Module/ -o app/code/Vendor/Module/i18n/en_US_raw.csv

Just make sure the folder i18n exists inside your module.
After you are done translating what you need to move the file en_US_raw.csv to a file name corresponding to your language code inside the same folder.
For Romanian, for example, the file name should be ro_RO.csv.

Related Topic