Internationalisation in Magento 1 was a powerful feature introduced that allowed merchants to translate text on their storefront into different languages of their choice. There are some similarities when using translations in Magento 2, including enabling inline translations within the admin for the storefront or the admin area, or using language packages in the form of .csv files.
Inline translation can be enabled in the admin within Stores -> Configuration -> Advanced -> Developer, and opening up the Translate Inline
section.
Magento will then highlight the text available to translate by adding borders around it.
When hovering over the highlighted text, click on the dictionary icon to open a Translate popup allowing you to translate the specific text.
Magento recommends refreshing the translation cache and refreshing the page afterwards to ensure that you see your updated translations.
By using inline translation, the translated text records are saved in the translation
database table.
Language packages also make a return in the form of .csv files that can reside in a module or theme’s i18n
directory.
For example, using the en_GB
locale, a en_GB.csv
file can be created within a module or theme’s i18n
directory.
The .csv file should contain two columns: the original phrase in the to be translated, and the phrase it should be translated to.
"Customer Login","Login here!"
The above example would successfully change any phrases that matched Customer Login
to Login here!
, however this is providing that the same original phrase hadn’t been already changed when using Inline Translation.
This is because their is an order of priority, similar to how there was in Magento 1. Inline translation takes highest priority, following by theme .csv translations and followed by module .csv translations.
In fact, you can see the order of how Magento loads translations by looking at the vendor/magento/framework/Translate.php
file and the loadData()
method.
// vendor/magento/framework/Translate.php
<?php
namespace Magento\Framework;
use Magento\Framework\App\Filesystem\DirectoryList;
class Translate implements \Magento\Framework\TranslateInterface
{
....
public function loadData($area = null, $forceReload = false)
{
$this->setConfig(
['area' => isset($area) ? $area : $this->_appState->getAreaCode()]
);
if (!$forceReload) {
$this->_data = $this->_loadCache();
if ($this->_data !== false) {
return $this;
}
}
$this->_data = [];
$this->_loadModuleTranslation();
$this->_loadThemeTranslation();
$this->_loadPackTranslation();
$this->_loadDbTranslation();
if (!$forceReload) {
$this->_saveCache();
}
return $this;
}
....
}
As database (inline) translations are loaded last, these take priority over other transations (Language Pack translations will be covered at a later date).
Note: This article is based on Magento CE version 2.1.7.