TransWikia.com

Magento 2 - How to translate strings to a specific locale?

Magento Asked by Pini on November 25, 2021

Is there any way to translate strings in Magento 2 to a specific locale (not necessarily the current one)?

Hypothetical example:

$translatedString = __("Some String", "de_DE");

3 Answers

I had to use a combination of these two answers, for some reason the __($textToTranslate) would not work within my helper class even though the text was there it wouldn't return the translation it just returned the text I was trying to translate, so I did it like this:

    // in constructor add:
public function __construct(
        MagentoStoreModelAppEmulation $appEmulation,
        MagentoFrameworkTranslateInterface $translate,
        MagentoFrameworkAppAreaList $areaList
    ) {
        $this->appEmulation = $appEmulation;
        $this->areaList = $areaList;
        $this->translate = $translate;
    }
    
    $this->appEmulation->startEnvironmentEmulation($store);
    $area = $this->areaList->getArea(MagentoFrameworkAppArea::AREA_FRONTEND);
    $area->load(MagentoFrameworkAppArea::PART_TRANSLATE);
    $translation = $this->translate->getData()[$textToTranslate]

Answered by Kevin Chavez on November 25, 2021

It's a bit more complicated - you'll need to emulate a store with that locale.

  1. Add the AppEmulation and the AreaList classes to the constructor of your class:
    /**
     * @var MagentoStoreModelAppEmulation
     */
    private $appEmulation;
    /**
     * @var MagentoFrameworkAppAreaList
     */
    private $areaList;

    public function __construct(
        MagentoStoreModelAppEmulation $appEmulation,
        MagentoFrameworkAppAreaList $areaList
    ) {
        $this->appEmulation = $appEmulation;
        $this->areaList = $areaList;
    }
  1. Emulate the store and reload the translations
$this->appEmulation->startEnvironmentEmulation($store->getId());
$area = $this->areaList->getArea(MagentoFrameworkAppArea::AREA_FRONTEND);
$area->load(MagentoFrameworkAppArea::PART_TRANSLATE);
  1. Do your translation
$translatedText = __($text);
  1. Revert the App Emulation
$this->appEmulation->stopEnvironmentEmulation();

Answered by Andreas von Studnitz on November 25, 2021

The only way I managed to get the translation of specific locale is by using the function getDictionary of class MagentoFrameworkAppLanguageDictionary

Here is the way I have used it:

1.) Declare

protected $dictionary;

2.) Use dependency inside the construct by

 MagentoFrameworkAppLanguageDictionary $dictionary,

 $this->dictionary = $dictionary;

3.) Now use the getDictionary() inside your function like:

$this->dictionary->getDictionary('de_DE')['Some String'];

This function returns associative array where key is phrase in the source code and value is its translation.

EDIT This solution works only for getting the translation from language pack. To have the correct translation which is used for the frontend we need to do app emulation. Please check the correct answer below by @Andreas

Answered by Sarvagya on November 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP