Doesn't work correctly when 2 gtranslate blocks are enabled

Created on 25 October 2023, 8 months ago
Updated 19 March 2024, 3 months ago

When 2 gtranslate blocks are active on the same page, the gtranslate dropdown widget gets rendered twice in the same block, instead of one time in each block.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇺🇸United States AaronBauman Philadelphia

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

Merge Requests

Comments & Activities

  • Issue created by @AaronBauman
  • 🇮🇳India naveenvalecha New Delhi

    I'm getting the same issue in one of the project

  • First commit to issue fork.
  • 🇬🇧United Kingdom BWilliams1992

    This only fixes the issue when cdn is turned off, otherwise it uses the code from the cdn where the javascript is not patched.
    I think the root cause is here https://git.drupalcode.org/issue/gtranslate-3396734/-/blob/3396734/src/P...
    It will execute that finction on each build resulting in multiple select lists, ran in the build function here https://git.drupalcode.org/issue/gtranslate-3396734/-/blob/3396734/src/P...

  • 🇦🇺Australia pasan.gamage

    I've the CDN turned off but still having the same issue.
    I'm loading the block in my page template under two different wrappers for desktop and one at a different location for mobile
    {{ drupal_entity('block', 'gtranslate') }}
    It was working for me in 8.x branch but after 3.x this seem to have broken and now both blocks are loading one after the other.

  • 🇦🇺Australia pasan.gamage

    Attaching screenshot as mentioned in #5

  • 🇺🇸United States joelsteidl

    @BWilliams1992 is on to something with the javascript getting included twice. This module needs some love.

    This refactor is headed in the right direction but wasn't working for me. https://www.drupal.org/project/gtranslate/issues/3346829 🐛 Refactor to use libraries and compatibility with Big Pipe Needs work

    My sinful quick fix for now. If the block is found more than once I'm just doing a string replace to remove the javascript from getting added again. Be sure you pay attention to which widget you are using.

    function hook_preprocess_block(&$variables) {
      if ($variables['plugin_id'] == 'gtranslate_block') {
        if (isset($variables['attributes']['id']) && $variables['attributes']['id'] != 'block-gtranslate') {
          // Hack to remove the script tag from the gtranslate block if included more
          // than once on the page. See https://www.drupal.org/project/gtranslate/issues/3396734.
          $script = "<script>(function(){var js = document.createElement('script');js.setAttribute('src', 'https://cdn.gtranslate.net/widgets/latest/ln.js');js.setAttribute('data-gt-orig-url', '/');js.setAttribute('data-gt-orig-domain', 'tpa.lndo.site');document.body.appendChild(js);})();</script>";
          $variables['content']['#gtranslate_html'] = str_replace($script, '', $variables['content']['#gtranslate_html']);
        }
      }
    }
  • First commit to issue fork.
  • The solution of the Merge request only works for the "Dropdown" widget. I added another solution for the "Nice Dropdown with Flags" widget. We will probably need a different solution for each widget type.

  • 🇺🇸United States quicksketch

    🐛 Refactor to use libraries and compatibility with Big Pipe Needs work is definitely a big improvement on the overall situation.

    For our site, where we are using the "Dropdown" widget, the widget renders twice correctly, but it's also appending two <div id="google_translate_element2"> elements to the first widget on the page, see screenshot below:

    After applying 🐛 Refactor to use libraries and compatibility with Big Pipe Needs work , it seems to do better in that the "google_translate_element2" element gets added after each corresponding widget, but it still has the problem that the same ID is being used twice. This is causing a ding by our accessibility testing tools, and it's malformed HTML to use the same ID twice.

  • 🇺🇸United States quicksketch

    (Sorry wrong screenshot attached, fixing previous comment)

Production build 0.69.0 2024