5

I am building internal in my custom module that I am porting to Drupal 8: https://www.drupal.org/project/taxonomy_facets

I need to get a current language prefix so I can prepend it to the link. I Drupal 7 it was as simple as:

global $language;
$prefix = $language->prefix;
$mylink = $prefix . '/example/something';

How can do this in Drupal 8?

EDIT: My issue specifically is that I have a language prefix set to NONE for the default language at Home >> Administration >> Configuration >> Regional and language >> Languages >> Detection and selection. (admin/config/regional/language/detection/url)

For the default language, I want the code to return the real prefix, i. e. NONE, not en (the code for the default language, English).

1
  • @Kevin Not a dupe-- the linked question is about Twig files; this question is about getting the language for the URL path. Dec 5, 2017 at 12:12

1 Answer 1

8

In Drupal 8, you use the languageManager class to get the language code:

$languagecode = \Drupal::languageManager()->getCurrentLanguage()->getId();

EDIT: If you want to return NONE if the current language is the default language, you probably need to make a helper function:

$languagecode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$default_languagecode = \Drupal::languageManager()->getDefaultLanguage()->getId();
if ($languagecode == $default_languagecode) {
  return "NONE";
}
else {
  return $languagecode;
}

helper function to ge it from settings

function getLanguagePrefix() {
    if($prefixes = \Drupal::config('language.negotiation')->get('url.prefixes')) {
      $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
      return "/". $prefixes[$language];
    }
    return null;
  }
6
  • 1
    Thanks, this issue is that if I have language prefix se to non for default language in Home >> Administration >> Configuration >> Regional and language >> Languages >> Detection and selection admin/config/regional/language/detection/url Dec 4, 2017 at 19:15
  • 1
    @DarkoKantic What's the issue? If you set the prefix to none, you don't get a language code? Dec 4, 2017 at 19:17
  • 1
    sorry, problem in commentin, yes, this is my intended comment Thanks, this works if there is prefix set for all languages. My issue is that I have language prefix set to 'non', for a default language, in Home >> Administration >> Configuration >> Regional and language >> Languages >> Detection and selection. admin/config/regional/language/detection/url In my case the above code will return me 'en' for my default language, English. Is there a way to return real prefix, i. e. non for this case? Dec 4, 2017 at 19:21
  • 1
    @DarkoKantic I updated my answer. I don't know of any easy way to do it. Dec 4, 2017 at 19:28
  • 5
    Since language id and prefix are different things and you want to pick the prefix, here is the code: $language = \Drupal::languageManager()->getCurrentLanguage(); $prefixes = \Drupal::config('language.negotiation')->get('url.prefixes'); $language_prefix = $prefixes[$language->getId()];
    – Capy
    Sep 15, 2021 at 10:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.