I know to get the current language of a website:

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

However, given that I have a node alias

$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$nid);

How do I get the language of that $alias node?

  • This question doesn't make sense to me: You can't get a translated alias without LanguageManager, so just use the exact code you posted yourself in code line 1. AliasManager is doing the same.
    – Hudri
    Mar 6 '20 at 10:44

To get the path alias of specific language try with:

$current_lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
/** @var \Drupal\Core\Path\AliasManager $aliasManager */
$aliasManager =  \Drupal::service('path.alias_manager');
$alias = $aliasManager->getAliasByPath('/node/10', $current_lang);

If you want to get the language code from alias try with:

$aliasArray = \Drupal::service('path.alias_storage')->load(['source' => '/node/' . $nid]);
$lang_code = $aliasArray['langcode'];

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.