Problem/Motivation
When using LegacyEmailBuilder, the email langcode is always set to the current langcode. Since a langcode is provided to the mail method on the MailManager, we should use that langcode.
Proposed resolution
Use the legacy message langcode during the processing in Mailer.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | symfony_mailer-set_langcode_with_legacyemailbuilder-3413081-2.patch | 604 bytes | aerzas |
Issue fork symfony_mailer-3413081
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
aerzas commentedHere is a patch proposition.
Comment #3
adamps commentedThanks for the issue and patch. Strange, I thought I had written code for this already😃.
The issue is only about LegacyEmailBuilder so the fix should also be in the legacy code. I believe we can fix it in
LegacyMailerHelper::emailFromArray(). On line 141 there is a call to$email->setAddress(). We can pass a second parameter of$message['langcode']. This should be only for the to address.This project now uses merge requests not patches please.
Comment #6
viren18febs commentedPlease review.
Comment #7
adamps commentedThanks, I mean pass langcode as second parameter to parseAddress()
Comment #8
viren18febs commentedI have updated langcode as second parameter to parseAddress(), please review.
Comment #9
adamps commentedThat looks good to me. @Aerzas please can you test?
Comment #10
aerzas commentedThank you for the updated code @viren18febS .
Unfortunately, it doesn't seem to fix my issue. The langcode is correctly set on the address but it is still the current langcode that it is used by
Mailer::doSend. In the context of a legacy email, the__disable_customize__parameter is set and the langcode processing in theMailer::doSendmethod is ended early intentionally, no address is parsed. The address langcode is correct but not the email langcode.We could prevent the setting of the
__disable_customize__parameter inLegacyEmailBuilder::createParams, but then the account will also be updated/processed inMailer::doSendwhich may have unwanted side effects.We could otherwise split the disabling parameter into two:
__disable_customize_langcode__and__disable_customize_account__. This would offer a fine-grained way of handling overrides but will require additional conditions in the process loop.Or we could consider that
__disable_customize__only excludes account customization and still gather the langcode from the to addresses.Comment #11
adamps commentedGood point, thanks @Aerzas. How about this?
- LegacyEmailBuilder creates the To address with the current langcode and also current account. Add an extra param to parseAddress.
- Remove all code for __disable_customize__
Comment #12
yorder.be commentedHas there been any progress on this issue?
Seems quite the priority since this affects all Core user mails.
Or is there another workaround to be able to send the mails in the correct language?
Comment #13
aerzas commented@yorder.be The patch I proposed fixes the issue in the meanwhile.
Comment #14
aerzas commented@AdamPS Thank you for your feedback, I'll work on a new MR as soon as I can.
Comment #15
adamps commented__disable_customize__ has now been removed from 2.x. It should now be a 1 line fix to
LegacyMailer::send()to pass$message['langcode']as the 2nd parameter.Comment #16
koustav_mondal commentedComment #18
koustav_mondal commented@adamps Made the changes. Previous MR was forked form 1.4.0 and LegacyMailer class was not present in that specific version. So, I pulled the latest 2.x-dev and raised new MR-139 and made the changes in that MR. Kindly review it.
Comment #19
adamps commentedThank please see the comment in the MR
Comment #20
koustav_mondal commentedAdded 2nd parameter to parseAddress().
Comment #21
adamps commentedGreat, now it needs someone to test. A manual test would be OK. Set up a multi-language site, configure a user with a non-default language, send an email with template text that depends on language. Test without the fix the language is wrong, with the fix it is right.
Comment #22
debrup commentedI was testing this issue as mentioned by @adamps.
Am I doing it correctly? If not, then mention where and how the error occurs and how to reproduce it. Any help is appreciated. Thanks in advance :)
Comment #23
adamps commentedThanks @debrup
I have one idea - make sure that receiver langcode is not the default.
Comment #24
debrup commented@adamps I have created a new user set to a non-default language. When their account gets created the mail is sent with the corresponding template in the langcode set for that user.
Comment #26
adamps commentedAlso please make sure that the current account that you are logged in with has a different langcode from the one on the new user.
Comment #27
adamps commentedSorry I realise anyway that the fix is incomplete, my mistake. We need to
__disable_customize__inMailer::doSend()andLegacyMailer::send()ImportHelper::parseAddress()which is?AccountInterface $account = NULLand pass the value tonew Address()in 2 places.LegacyMailer, after the code that we already added, pass a 3rd parameter which is the current account. Add an extra constructor parameterprotected readonly AccountInterface $accountto fetch this.Comment #28
adamps commentedComment #30
adamps commentedThis issue is missing "steps to reproduce" and symptoms. I agree with @debrup that it is hard to reproduce. I can confirm that the langcode is wrong in Mailer.php (by adding a debug print), which affects the current langcode during the call to hook_mail. However both user_mail and contact_mail ignore the current langcode, and use message['langcode'] so the emails produced are all correct.
Comment #31
adamps commentedI have updated the MR so it works in my testing, confirmed by debug print. Please review @aerzas.
Comment #32
adamps commentedComment #34
adamps commentedI've now added a test. I'll commit as I'm getting too many issues in progress. Anyone else please do test to confirm.