I need to send the mail to a user other than the one logged in. And I need to translate the text to the other_user's language.

  global $locale;
  $old_locale = $locale;
  $locale = $other_user->language;
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  $subject = t('You account is approved');
  $body = t('Dear %userfullname. Your account on the site "%site" is approved by your referees. You can enter the site using the login details sent to you before.', array('%userfullname' => $user->profile_fname . ' ' . $user->profile_lname, '%site' => variable_get('site_name', 'drupal')));
  $mail = $other_user->mail;
  user_mail($mail, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
  $locale = $old_locale;

This code generates the mail with translated subject.

  Subject: Ваш аккаунт подтвержден

But not translated body, with only sitename translated, though i18 variables are defined properly in settings.php.

Dear Testor Testoridze. Your account on the site "Секретный сайт" is approved by your referees. You can enter the site using the login details sent to you before.

Comments

almays’s picture

Well, being tested in just a page or a block it behaves like that.


global $locale;
$old_locale = $locale;
$locale = $other_user->language;

print t('Text translated properly %var', array('%var' => variable_get('site_name', 'drupal')));

$locale = $old_locale;

Here the plain text in the t() is translated as supposed on the other user language. But the 'site_name' is fetched on the active locale, but not on the one chosen by $locale.