Use slected language instead of default language for monolingual sites

Created on 22 November 2022, about 2 years ago
Updated 14 September 2024, 3 months ago

Problem/Motivation

We have a site with a site with English and German enabled. We want to use English as default Language because we want to have our configs etc. in English. To show our visitors the website in German we use the selected language negotiation plugin and selected German.

The simple_sitemap method isMultilingual evaluates to FALSE because we don't use an URL based negotiation plugin. So the getUrlVariants method uses getAlternateUrlsForDefaultLanguage. The problem now is that our path aliases apply only for German but the sitemap is generated for English and we end up with a sitemap w/o path aliases.

Proposed resolution

I'm not sure but doesn't it make sense to use the value of the selected language negotiation plugin instead of the default language? Because that's what the users will see.

The default value of the selected language plugin is the default language anyway - so in this case it doesn't make a difference. I don't see a scenario where you have Language A selected but want to have your sitemap in Language B - but maybe there is one.

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇩🇪Germany Harlor Berlin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇭Switzerland handkerchief

    +1, similar case with me.

    Default language is English.
    Second language is German.

    But the Site uses only German without prefixes and so on. Reason for that setting are problems with module translations if you don't install drupal with english as default languge.

    So aliases only exist in German, but the sitemap will always create the English-Version wich is node/1, node/2 etc.

  • 🇨🇭Switzerland handkerchief

    Workaround:

    /**
     * Implements hook_simple_sitemap_links_alter().
     */
    function HOOK_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
      $sitemap_id = $sitemap_variant->id();
      $node_storage = \Drupal::entityTypeManager()->getStorage('node');
      if ($sitemap_id === 'default') {
        foreach ($links as $key => $link) {
          $nid = $link['meta']['entity_info']['id'];
          $node = $node_storage->load($nid);
          if (str_contains($link['url'], '/node/')) {
            $links[$key]['url'] = $node->toUrl()->setAbsolute()->toString();
          }
        }
      }
    }
    
  • 🇳🇱Netherlands Dimiter

    I think I am experiencing a very similar issue, except that I don't have any problems with nodes but with pages from views that should be added to the sitemap. The individual nodes are all correctly added to the sitemap (only the non-english version, using the correct canonical url), but the pages from a view that has been added to the sitemap (using "Simple XML Sitemap (Views)") are included both in the English and non-english language, even tough the view and all the nodes in the view are all configured to use the non-english language.

  • 🇷🇺Russia walkingdexter

    I tested the following scenario on a clean install with 4.x-dev:

    1. Enable two languages ​​- English and non-english.
    2. Set English as default language.
    3. Use the "Selected language" plugin for language detection and configure it with a non-english language.
    4. Enable content translation for the "Article" content type.
    5. Add a new node of type "Article" and translate it into a non-english language.

    Now when I set a URL alias for the created node, I get a 404 error if the alias has a non-english language. If I change the alias language to English or unspecified, then the 404 error does not occur. The sitemap is also correct in this case.

    So I can't reproduce the problem on a clean install. Maybe I'm missing something. Feel free to reopen if you can provide more information to reproduce the problem. In other cases see #4 and #5 for possible solutions. Also consider changing the URL alias language.

  • 🇨🇭Switzerland handkerchief

    @walkingdexter You tested another scenario, our scenario is:

    1. New Drupal installation in English
    2. Add another language, e.g. German. But English still is the default language.
    3. Add nodes directly in the German language, no translations
    4. Generate the sitemap

    Then you will get the effects described in #3.

    I thinks the problem is, the site default language is English, but only nodes in German exist without translations. Again the reason for this site configuration are effects like 🐛 Configuration language being overwritten during module install Needs work or 🐛 Installing a module causes translations to be overwritten Fixed . So to avoid such translation problems if you have another default or installation language as English, we use English as default language, although only German is used without translations.

  • 🇷🇺Russia walkingdexter

    @handkerchief In your scenario I still get the 404 error. Can you provide more details on step 3?

    • Is the Content Translation module disabled?
    • What settings are set on /admin/config/regional/content-language?
    • What settings are set on /admin/config/regional/language/detection?
Production build 0.71.5 2024