0

in D8 (multilanguage site, with two languages) i wanna send email for registration, etc by user preference of language of user (People -> Edit people -> Language Settings)

I've enabled all modules for translations and translations are saved correctly (see the attachment).enter image description here

I followed this guide: https://www.valuebound.com/resources/blog/how-to-send-mail-programmatically-drupal-8 but not work for me.

The language code passed for the email is correct but the email is always send in default language (in my case, english).

0

Be sure that the string you sent by mail is already translated in the target language. Otherwise it will not work. You can check it here http://youwebsite.com/admin/config/regional/translate

0

For solve this problem, in hook_mail insert this:

    $token = \Drupal::service('token');
    $language_manager = \Drupal::languageManager();
    $language = $language_manager->getLanguage($user->getPreferredLangcode());
    $original_language = $language_manager->getConfigOverrideLanguage();
    $language_manager->setConfigOverrideLanguage($language);
    $mail_config = \Drupal::config('user.mail');

    $subject = $token->replace($mail_config->get($key . '.subject'), [
        'user' => $user
    ]);

    $body = $token->replace($mail_config->get($key . '.body'), [
        'user' => $user
    ]);

    $language_manager->setConfigOverrideLanguage($original_language);

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.