When I use Node::load(10)->toArray() I see that my module thinks that the current language is English. I have the language key (fr) in the url and when I place \Drupal::languageManager()->getCurrentLanguage()->getId() in my code it says (fr). How can I get Node::load(10) to see the current language the user is using?

When I translate the content type into another language, all translations of the same page seem to have the same id to place into the load function.


You could try the following:

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

Good luck!



use Drupal\Core\Language\LanguageInterface;
use Drupal\node\Entity\Node;
$node = Node::load(10);
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
$translation = $node->getTranslation($langcode);
// Now deal with the right language from here on.

Hope this works!

  • 1
    In this example $langcode is not a string. It cause me an error. Use '$node->getTranslation($langcode->getId());' – Pauleau May 13 '19 at 8:49
  • I second that comment from @Pauleau; getCurrentLanguage() returns an object implementing the LanguageInterface – Stefanos Petrakis Mar 2 '20 at 15:45

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.