Magento – Get translated string programmatically in custom module

extensionslocalisationmagento2module

I am writing custom module in order to verify customers mobile number before checkout if the cart have products from specified type.

The message body sent to mobile operator passed to translation function

$messageContent = __('Use that code to verify').': '.$verifyCode;

The string have been translated in csv file but when I use like this to send http request to SMS gateway server it goes to the mobile like below:

{{{TRANSLATED_STRING}}{{TRANSLATED_STRING}}{{ENGLISH_STRING}}{{THEME_KEY}}}

How can I get the string for every store view in code

Edit:

After @Max Stsepantsevich answer I turned off the inline translation and now I get only the English string.

I have that line in my custom module to translate

"Use that code to verify ","أستخدم هذا الكود للتفعيل
",module,Magento_Bundle

I added the following piece of code to emulate the store view but still get the english only see below:

$resolver = $this->om->get('\Magento\Framework\Locale\Resolver');

$resolver->emulate('2');//store view id
$content = urlencode(__('Use that code to verify '). $verifyCode);

$sender->send($content);
$resolver->revert();

Thanks in advance

Best Answer

Looks like inline translation enabled in your application. Try to check inline translation status in stores configuration and disable if needed. Moreover, before sending any email or other customer messages (sms in your case) you need to suspend inline translation to prevent similar issues with improperly enabled inline translation.

public function send($code)
{
    $this->inlineTranslation->suspend();

    $this->mySender->send(__('Use that code to verify %1', $code));

    $this->inlineTranslation->resume();
}
Related Topic