Add Language Switcher on admin toolbar What is the correct way for adding Language Switcher (Dropdown) or links on the admin toolbar menu from my custom module or from a contributed module?
Well, I got something up and running via hook_toolbar. Only thing left is, that you can't click directly on the "Language: EN" string in the toolbar. You have to click slightly next to it. Maybe you can fix this.



Apart from that, please also have a look at the Devel module and their implementation of hook_toolbar where they have an extra src/ToolbarHandler.php class for everything. You better should rebuild the code below into a dedicated handler.



use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_toolbar().
 */
function MYMODULE_toolbar() {

  // Get languages, get current route.
  $current_language = \Drupal::languageManager()-&gt;getCurrentLanguage()-&gt;getId();
  $languages = \Drupal::languageManager()-&gt;getLanguages();
  $route = \Drupal::service('path.matcher')
    -&gt;isFrontPage() ? '&lt;front&gt;' : '&lt;current&gt;';

  // Get links.
  $links = [];
  foreach ($languages as $language) {
    $url = new Url($route, [], ['language' =&gt; $language]);
    $links[] = [
      '#markup' =&gt; Link::fromTextAndUrl($language-&gt;getName(), $url)
        -&gt;toString(),
    ];
  }

  // Set cache.
  $items['admin_toolbar_langswitch'] = [
    '#cache' =&gt; [
      'contexts' =&gt; [
        'languages:language_interface',
        'url',
      ],
    ],
  ];

  // Build toolbar item and tray.
  $items['admin_toolbar_langswitch'] += [
    '#type'   =&gt; 'toolbar_item',
    '#weight' =&gt; 999,
    'tab'     =&gt; [
      '#type'       =&gt; 'html_tag',
      '#tag'        =&gt; 'div',
      '#value'      =&gt; '&lt;span&gt;' . t('Language') . '&lt;/span&gt;: &lt;span class="current-language"&gt;' . strtoupper($current_language) . '&lt;/span&gt;',
      '#attributes' =&gt; [
        'class' =&gt; ['toolbar-item-admin-toolbar-langswitch'],
        'title' =&gt; t('Admin Toolbar Langswitch'),
      ],
    ],
    'tray'    =&gt; [
      '#heading' =&gt; t('Admin Toolbar Langswitch'),
      'content'  =&gt; [
        '#theme'      =&gt; 'item_list',
        '#items'      =&gt; $links,
        '#attributes' =&gt; [
          'class' =&gt; ['toolbar-menu'],
        ],
      ],
    ],
  ];

  return $items;
}