InsideDarkWeb.com

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

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");

Magento Asked by Pini on November 25, 2021

3 Answers

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!

Related Questions

How to place sizes in order

1  Asked on December 7, 2020 by stefaniiia

 

Magento 2.4 disable UI/Theme part

0  Asked on December 7, 2020 by bogdan-hepting

   

Add same product to quote as a separate item in Magento 2

1  Asked on December 1, 2020 by chirag-dodia

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.