Drupal 8: Get language prefix

Posted on: Fri, 05/12/2017 - 17:49 By: Darko
Configure the language prefix in Drupal 8

Our Drupal expert on configuring language prefix in Drupal 8

Language prefix is user configurable, for example you can set it to null for default language or to be different from the language code.


If you need to get a language prefix in your code, in Drupal 8, this is how to do it:


 function getLanguagePrefix() {  if($prefixes = Drupal::config('language.negotiation')->get('url.prefixes')) {       $language = Drupal::languageManager()->getCurrentLanguage()->getId();       if($prefixes[$language]){         return "/" . $prefixes[$language];       }     return null;   }