5

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.

8

You could try the following:

$lang_code = \Drupal::languageManager()->getCurrentLanguage()->getId();
Node::load(10)->getTranslation($lang_code)->toArray();

Good luck!

5

UPDATED ANSWER:

<?php
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!

  • In this example $langcode is not a string. It cause me an error. Use '$node->getTranslation($langcode->getId());' – Pauleau May 13 at 8:49

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.