In this article, you will come to know how one can generate a translation dictionary of a module in Magento 2.
You can generate a translation dictionary to use by itself (for example, to translate words and phrases in a custom module) or for use by a language package.
Work with translation dictionaries
To translate words and phrases, you must:
- Run the translation collection command to extract translatable words and phrases from enabled components.
- Translate the words and phrases.
- You can package the translation dictionaries into a language package and provide the package to the Magento store administrator.
- In the Magento Admin, the store administrator configures the translations.
php bin/magento i18n:collect-phrases [-o|--output="<csv file path and name>"] [-m|--magento] <path to directory to translate>
The following table explains the parameters and values of the above command:
|<path to directory to translate>||Path to a directory that has translatable code; in other words, PHP, PHTML, or XML files that have phrases to translate.|
The tool starts searching in the path you entered and searches all the files and subdirectories it contains.
Do not use this parameter if you use -m|–magento.
Yes (dictionaries), no (packages).
|-m|–magento||Required to create a language package from this translation dictionary.|
If used, searches the directories that contain bin/magento. This option adds themes or modules to each line in the dictionary.
An example follows:
“No Items Found”,”No Items Found”,module,Magento_Wishlist
|-o|–output=”<path>”|| Specifies the absolute file system path and file name of the translation dictionary .csv file to create. The value you enter is case-sensitive. The name of the .csv file must exactly match the locale name, including the characters’ case.|
If you omit this parameter, the output is directed to stdout.
To create a language pack from a translation dictionary, you must use the -m|–magento option.
Example: Create a translation dictionary for a module or theme
Collect phrases from your module:
Run below command from Magento root directory to generate translation directory for a module.
php bin/magento i18n:collect-phrases -o app/code/ExampleCorp/SampleModule/i18n/en_US.csv app/code/ExampleCorp/SampleModule
To translate in Magento 2, you need to use the Inline tool. You can enable it through the admin dashboard. First, switch to developer mode and go to Stores > Settings > Configuration > Advanced > Developer > Translate Inline and enable it for storefront or admin panel. You can then translate by clicking on the book icon.
To install language pack in Magento 2, follow the below steps:
1. Choose a language pack from the component manager and install it.
2. Create its store view by going to Stores>Settings>All Stores in the admin panel.
3. Go to Locale Options and select the Locale for the translated page.
4. Use the command line to add the language pack.
To add language switcher in Magento 2, follow these steps:
1. Visit the configuration option in Stores and go to Magefan Extensions > Auto Language Switcher.
2. Enable the Auto Language Switcher extension and select Switcher mode.
3. Configure the restrictions to add allowed pages to the language switcher.
4. Set the default store view for different countries.
i18n in Magento 2 refers to the internalization feature. It is used to localize Magento stores based on location and language. It uses language packs to configure the stores based on strings for a particular market by translating product names, titles, prices, and other attributes.