How to create and use language-pack component in Magento 2?

By: Vishal Sutariya, Oct 29th, 2018 # Magento 2 # Frontend # How to

How to create and use language-pack component in Magento 2 - Aureate Labs

If you are new to create a custom component in Magento 2 and if you are in the requirement to have a working language component, you landed up at the right place.

In Magento 2, each component type has a different directory structure. To create a language-package component, we have to create a vendor directory and package directory under the following directory: app > i18n

Follow the below steps to create a language-package component:

  • Step 1: Create a directory for the language package
  • Step 2: Collect Phrases for translation
  • Step 3: Translate word or phrases
  • Step 4: Create registration.php, language.xml and composer.json file in the language package directory
  • Step 5: Flush the Magento cache and static file cache

Note: Replace Aureatelabs/it_ch from entries in this article to your module name.

Step 1: Create a directory for the language package

The directory structure for the language package will look like this: <root>/app/i18n/<Vendor>/<Package>

For example: <root> > app > i18n > Aureatelabs > it_ch

Step 2: Collect Phrases for translation

Generate a .csv file, using the command given below. But instead of specifying a module or theme directory for CSV generation, specify the Magento application root directory. Generated .csv file will contain any phrases that the command will find in the code.

bin/magento i18n:collect-phrases -o "/app/i18n/Aureatelabs/it_ch/it_CH.csv" -m

Step 3: Translate words and phrases

Use the following instruction for translating the words and phrases:

  • Change the content of the second column only. Translate the phrases from English to the respective language.
  • While translating, pay attention to placeholders like %1, %2 and so on. They are used by the Magento application to insert context values, they are not used for translations. For example:
    • Product '%1' has been added to the shopping cart.
    • Product 'Multimeter-2000' has been added to the shopping cart.

Step 4: Create registration.php, language.xml and composer.json file in the language package directory

registration.php
Create a registration.php file on language package root directory for registering the component with the following code.

\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
   'aureatelabs_it_ch',
   __DIR__
);

language.xml
Create a language.php file on language package root directory with the following code.

<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
   <code>it_CH</code>
   <vendor>aureatelabs</vendor>
   <package>it_ch</package>
</language>

Where:

  • <code>: Language package locale (required)
  • <vendor>: Module’s vendor name (required)
  • <package>: Language package name (required)
  • <sort_order>: Priority of uploading a package when there are several language packages available for a store (optional)
  • <use>: Parent language package locale from which to inherit dictionaries (optional)

composer.json
Create a composer.json file on language package root directory with the following code.

{
 "name": "aureatelabs/language-it_ch",
 "description": "Italian language",
 "version": "100.0.2",
 "license": [
   "OSL-3.0",
   "AFL-3.0"
 ],
 "require": {
   "magento/framework": "100.0.*"
 },
 "type": "magento2-language",
 "autoload": {
   "files": [
     "registration.php"
   ]
 }
}

Step 5: Flush the Magento cache and static file cache

Now, run the following commands from the command line interface
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento cache:flush

That is all you need to set up and you are all good to go with the newly created language-package component. Though you faced any difficulties, queries even after following all the steps carefully, connect us anytime and we will be glad about solving your issues.

Happy Translating!