Language pack or translation for Magento 2 custom module

Language pack for Magento 2

Hi Guys, hope you are doing well and still enjoying your nightmare understanding Magento 2 and most complicated architecture and code :-). Today we are going to talk about language pack in Magento 2

But there is a good news for Magento 2 developers and especially the extension developers, if you remember good old days of Magento 1, we know! we know! some of you are still on Magento 1 so please don’t get offended.

We all remember it was nightmare getting your translation file(s) up to date in Magento 1 and sometimes you just don’t even bother and include no translation or language pack files because you can either concentrate on writing code or remember each and every text phrase you are writing in your code.

The good news is in Magento 2 you don’t need to bother to remember these phrases and it has become super-duper easy, trust me really really really easy!!!!! Now once you are finished with your code development, you just need to run the following command and it will give you your translation file in no time. Isn’t that great?

If you want this to work smoothly, you need to make sure you are using label tag in your XML files, __ function in your PHP / PHTML files and $.mage.__ in your JavaScript files. Based on that i18n:collect-phrases command below will retrieve your translation strings and added to your language pack CSV file.

bin/magento i18n:collect-phrases [-o|--output=""] [-m|--magento]

-o is for output of your language pack file

-m directory from where language pack content needs to be scanned and added to output language pack file.

For example

php bin/magento i18n:collect-phrases -o "app/code/CompanyName/ModuleName/i18n/en_US.csv" "app/code/CompanyName/ModuleName/"

or

php bin/magento i18n:collect-phrases -o vendor/companyname/modulename/i18n/en_US.csv vendor/companyname/modulename

Once you run the above command, it will give you the following results

Dictionary successfully processed.

Isn’t that awesome? I know you said YES 🙂

Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.