Drupal 8: get language prefix

Photo by Goran Ivos on Unsplash


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();
        return "/" . $prefixes[$language];
    return null;