I'm developing a command line module in magento 2 and I cannot find a way to set locale programmatically.
The module takes care of creating a pdf invoice from an order and sends it via email to customer. All is going fine but the mail and the pdf invoice are in english.
Also if I try to print something like echo __('my phrase')
, and I have the corresponding translation in /i18n/it_IT.csv
file, it doesn't print the translated sentence.
I'm stuck there since 8h or so, trying a bunch of ways to do that.
If I instantiate a local resolver instance (Magento\Framework\Locale\ResolverInterface
) via objectManager
or injecting it in the constructor and then try $this->_localResolver->getLocale()
, it prints it_IT
, but nothing happens.
Here is a piece of my code:
$locale = $objectManager->create('Magento\Framework\Locale\ResolverInterface');
echo $locale->getLocale(); //it prints 'it_IT'
$locale->setLocale('it_IT');
$locale->setDefaultLocale('it_IT');
echo __('Price including VAT') . "\n\r"; //It prints 'Price including VAT' and not the translated sentence.