I'm trying to programatically send a custom email from admin and I need to add some strings before sending the email. I would like to translate these strings based on a language selected. I used the code below but it does not work, the string stays in English.
$localeInterface = $objectManager-
>create('Magento\Framework\Locale\ResolverInterface');
$localeInterface->setLocale('de_DE');
$localeInterface->setDefaultLocale('de_DE');
echo __('Some string');
You can translate the string into another locale using getDictionary()
function of \Magento\Framework\App\Language\Dictionary
class as described follows:
protected $_dictionary;
public function __construct(
...
\Magento\Framework\App\Language\Dictionary $dictionary,
...
) {
...
$this->_dictionary = $dictionary;
...
}
public function execute() {
...
$arrString = $this->_dictionary->getDictionary('de_DE')['Some String'];
...
}
In $arrString
variable, you will get phrase in the source code as a key and the string translation as a value.
I hope it may resolve your problem.