Skip to content

Instantly share code, notes, and snippets.

@delta-9
Created February 8, 2016 06:37
Show Gist options
  • Save delta-9/084b501b365bd1b95d8c to your computer and use it in GitHub Desktop.
Save delta-9/084b501b365bd1b95d8c to your computer and use it in GitHub Desktop.
<?php
// Remember original language before this operation.
$original_language = \Drupal::configFactory()->getLanguage();
$language = language_load($account->getPreferredLangcode());
// Set the languag without saving, this is actually only for the current script execution:
\Drupal::configFactory()->setLanguage($language);
// Get the user.mail config object normally, but for the language we just set :)
$mail_config = \Drupal::config('user.mail');
// Now send email based on $mail_config which is in the proper language.
//...
// Set back the language to the prior value.
\Drupal::configFactory()->setLanguage($original_language);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment