0

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

  • Did you change locale in configuration for emulated store view? – Max Jan 14 '17 at 4:58
  • I don't know what are you talking about, Could you please let me know what I have to do? – Mohammed Gomma Jan 15 '17 at 7:00
  • I have the same issue, did you find a fix ? Thanks ! – magentodev Jun 3 at 14:35
1

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();
}
  • I disabled the inline translation and it returns only the english string, How to get the translated string? – Mohammed Gomma Jan 11 '17 at 6:51
  • 1
    If you send message from non-customer area(for example admin scope), you need to emulate customer store area for correct translation. Try to inject \Magento\Framework\Locale\ResolverInterface in your sender and call \Magento\Framework\Locale\ResolverInterface::emulate($storeId) before and \Magento\Framework\Locale\ResolverInterface::revert() after sending message. – Max Jan 11 '17 at 7:27
  • I have edited the question, Please see above. – Mohammed Gomma Jan 11 '17 at 9:16
0

You can use sprintf() feature of core PHP

 $messageContent=   sprintf(__('Use that code to verify %s'), $verifyCode);
  • Still the same only get the english string, I think stings translated in the theme layer it may need to be rendered. – Mohammed Gomma Jan 10 '17 at 8:48
  • 1
    you can pass arguments in translate function (__('Use %1 for %2', 'translate', 'translation')). Do not use sprintf for this, incorrect translated string may throw warning in sprintf. – Max Jan 10 '17 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.