1

Is there a way in the drupal 8 api to figure out if a language prefix is present in the url or not? Cause I need this information in order to display a custom language switcher if no prefix exists in the url. So for language detection I only have the prefix enabled.

Unfortunately this code is not working for me:

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

Cause it always returns me a language. When no prefix is present in the url it returns me (as excepted) the fallback language.

Has someone a similar situation or can help me here?

2

There might be something more elegant, but doing it the dirty way is pretty simple:

$languagecode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$parts = array_filter(explode('/', \Drupal::request()->getRequestUri()));

$has_prefix = count($parts) && $parts[0] == $languagecode;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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