Language pack translation specifics

We've compiled some language pack work specifics of Magento 2 that weren't specified in the official documentation. It took us some time to gather them all in one place.

Phrase translations can be stored in:

  1. Extension files
  2. Theme files
  3. Language pack files
  4. The data base

If a translation of the same phrase exists in all of these files, then the website will use the translation present in the last source.

For example, we have the phrase "Bonjour" which is going to get translated.

  1. The extension has it translated as "Hi"
  2. The theme has it translated as "Hello"
  3. The language pack — as "Good day"
  4. The data base has no translation

It will be displayed as "Good day", because it was in the last available translation source.

NB! Do not change phrases in 3rd party language packs. You will lose all changes after an update.

How to translate my website?

Translating your website the way you want it can be done in two ways:

1. Using the Inline Translate function

To use Inline Translate you will first need to temporarily disable the translate, blocks' and other output caches in System -> Cache management.

To enable the function go to Stores -> Configuration -> Advanced -> Developer, select the Inline translation tool, enable it for the frontend and/or backend, and save the settings.

Now, everything that can be translated with it will have a red outline. If you hover the cursor over such an element an icon will be displayed, simply press it to enter the desired translation.

NB! Translations should be added in the store view they are intended to be displayed in.

2. Creating a custom package with desired inheritance

For example, you wish to create your own Russian language pack — MyLangPack, that inherits phrases from our M2 Russian language pack. To do this:

  1. 2.1. Create a path for your new language pack

    /app/i18n/mylangpack/ru_RU

    2.2. Create a csv file of your translation:

    /app/i18n/mylangpack/ru_RU/ru_RU.csv

    2.3 Copy the phrases you wish to fix from our M2 Russian language pack's ru_RU.csv, fix them, and save the file

    2.4 In the ru_RU directory, create three files: composer.json, language.xml, registration.php, and insert the info below:

    composer.json

     {
       "name": "mylangpack/language-ru_ru",
       "description": "My Custom language",
       "version": "100.0.1",
       "license": [
         "OSL-3.0",
         "AFL-3.0"
     ],
       "require": {
         "magento/framework": "100.0.*"
       },
       "type": "magento2-language",
       "autoload": {
         "files": [
           "registration.php"
         ]
       }
     }
     

    Whereas "description" is the pack's description.


    language.xml

     <?xml version="1.0"?>
     <language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
         <code>ru_RU</code>
         <vendor>mylangpack</vendor>
         <package>ru_ru</package>
         <sort_order>100</sort_order>
         <use vendor="etws" package="ru_ru"/>
     </language>
     

    "Use vendor" is "etws" in this example, because we're inheriting phrases from the M2 Russian language pack

    NB! If you wish to create a pack that doesn't inherit any phrases, remove the "use vendor" line. In this case the priority of the language pack will depend on "sort_order". Greater values have higher priority.


    registration.php

     <?php
     \Magento\Framework\Component\ComponentRegistrar::register(
         \Magento\Framework\Component\ComponentRegistrar::LANGUAGE,
         'mylangpack_ru_ru',
         __DIR__
     );
     

    2.5 Launch the following command from your site's root folder:

    /bin/magento setup:static-content:deploy ru_RU

Frequently asked questions

What if I have several translation packages installed for the same language?

Language packs have their own sort order specified in the language.xml file. The greater the number, the more priority the pack has. In other words, the following order will be used:

  1. Extension files
  2. Theme files
  3. Language pack files
    • 3.1. Pack with sort order 0
    • 3.2. Pack with sort order 1
    • ...
    • 3.99. Pack with sort order 100
  4. The data base

Last modify date: Wednesday, January 25, 2017