Problem/motivation
Currently having an issue that when viewing a node that it ignores my user language preference and shows the language based on my second Interface text language detection method which is url.
This is being caused by an if statement in:
core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUser.php
In the getLangcode function shown below, there is an if statement which has a check were the 'user' preferred_langcode needs to be different from the default_langcode.
/**
* {@inheritdoc}
*/
public function getLangcode(Request $request = NULL) {
$langcode = NULL;
// User preference (only for authenticated users).
if ($this->languageManager && $this->currentUser->isAuthenticated()) {
$preferred_langcode = $this->currentUser->getPreferredLangcode();
$default_langcode = $this->languageManager->getDefaultLanguage()->getId();
$languages = $this->languageManager->getLanguages();
if (!empty($preferred_langcode) && $preferred_langcode != $default_langcode && isset($languages[$preferred_langcode])) {
$langcode = $preferred_langcode;
}
}
// No language preference from the user.
return $langcode;
}
Steps to reproduce:
- Create at least two languages
- Set default language to English
- Go to user edit settings and set default language to English.
- In the admin/config/regional/language/detection settings. Set User as the first detection method and Url as second.
- Clear the cache
- Visit a node edit page with other language prefix, then your default language (English) in your url. e.g: nl/node/123/edit
- Interface language will display in Dutch (Which I would not expect).
Proposed solution
In my opinion the $preferred_langcode != $default_langcode
check needs be removed. Because default langcode could be the same as preferred langcode and should not skip the Detection method if they are the same.
Comment | File | Size | Author |
---|---|---|---|
#10 | interdiff_3-10.txt | 1.88 KB | johnwebdev |
#10 | users-language-preference-not-applied-2907546-10--PASS.patch | 2.38 KB | johnwebdev |
#10 | users-language-preference-not-applied-2907546-10--FAIL.patch | 1.26 KB | johnwebdev |
Comments
Comment #2
Falco010Comment #3
Falco010Created patch for issue described above.
Comment #4
a.milkovskyFalco010, thanks. The patch #3 works for me.
Comment #5
a.milkovskyNeeds work for tests fix.
Comment #6
Ramya Balasubramanian CreditAttribution: Ramya Balasubramanian at UniMity Solutions Pvt Limited commented@Falco010 , the above patch is working. Thanks
Comment #8
andypostComment #10
johnwebdev CreditAttribution: johnwebdev commentedSo the problem is that
returns the default langcode as an fallback unless you pass an argument that it should not, hence why the test fails.
I also added a test.
Comment #11
johnwebdev CreditAttribution: johnwebdev commentedNeeds review for tests :-)
Comment #13
andypostLooks great to go!
Comment #15
andypostComment #17
tstoecklerBack to RTBC after random failure.
Comment #19
catchCommitted ccf36c3 and pushed to 8.6.x. Thanks!
Comment #22
Gogowitsch CreditAttribution: Gogowitsch as a volunteer and at QuoData GmbH Quality & Statistics for ÖQUASTA commentedThere is a contrib module User Account Language Negotiation that combines the User and Url language negotiation into one that saves the user's preferred language in their account settings. This allows the site to transition to the user's previously preferred language during login if only the "User account saver" detection plugin is enabled.
Comment #25
smustgrave CreditAttribution: smustgrave at Mobomo commentedClosed https://www.drupal.org/project/drupal/issues/2726505 as a duplicate and moved credit over.