Translatable labels for menu links

Created on 30 March 2024, 3 months ago

Thanks for this very useful module !

I would like to suggest making the label of the language links in the main menu configurable and translatable.

Currently, the module adds links with labels such as "English" and "Deutsch", I would like to be able to display "en" and "de" instead.

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇹🇭Thailand manarak

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

Comments & Activities

  • Issue created by @manarak
  • 🇩🇪Germany FeyP

    Thanks for filing this issue.

    We don't actually provide the titles of the links in the module. The module uses the links (including the link title) from Drupal Core's language switcher and makes them available for display as menu links.

    If you want to change the language switch links, including the link titles, there are a bunch of contributed modules available that you should be able to install in addition to this module that allow for various alterations of the language switch links. Alternatively, you can use hook_language_switch_links_alter() in a custom module.

    For your specific request, there is also an example in the FAQ on the project page that I think does exactly what you asked for by just changing a template file in your theme.

  • 🇹🇭Thailand manarak

    Thank you for your very fast reply.

    I wouldn't want to do this via template, because menus are used in several places on the site, and it's in general less scalable than a preprocess function or similar.

    The problem is, neither menu preprocess nor the hook you mentioned seem to work.

    I have tried different things in the theme's .theme file, such as

    function mytheme_language_switch_links_alter(array &$links) {
        if (isset($links['en']['link'])) {
          $links['en']['link']['#title']->setLinkTitle('en');
        }
    }

    or

    function mytheme_language_switch_links_alter(array &$links) {
      if (isset($links['en']['link']['#title'])) { 
        $links['en']['link']['#title'] = 'en'; 
      }
    }

    and none seems to work

Production build 0.69.0 2024