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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Falco010 created an issue. See original summary.

Falco010’s picture

Issue summary: View changes
Falco010’s picture

Created patch for issue described above.

a.milkovsky’s picture

Falco010, thanks. The patch #3 works for me.

a.milkovsky’s picture

Status: Active » Needs work

Needs work for tests fix.

Ramya Balasubramanian’s picture

@Falco010 , the above patch is working. Thanks

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.5.x-dev » 8.6.x-dev
Assigned: Falco010 » Unassigned
Issue tags: +Needs tests

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnwebdev’s picture

So the problem is that

$this->currentUser->getPreferredLangcode();

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.

johnwebdev’s picture

Status: Needs work » Needs review

Needs review for tests :-)

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Looks great to go!

Status: Reviewed & tested by the community » Needs work
andypost’s picture

Status: Needs work » Reviewed & tested by the community

tstoeckler’s picture

Back to RTBC after random failure.

  • catch committed 7e77a48 on 8.7.x
    Issue #2907546 by johndevman, Falco010: User's language preference is...
catch’s picture

Version: 8.7.x-dev » 8.6.x-dev
Status: Reviewed & tested by the community » Fixed

Committed ccf36c3 and pushed to 8.6.x. Thanks!

  • catch committed ccf36c3 on 8.6.x
    Issue #2907546 by johndevman, Falco010: User's language preference is...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Gogowitsch’s picture

There 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.

smustgrave’s picture