Why I cannot get translated entities I wrote service to get all necessary data for my page. Now I wanted to load them with translations. But whey I try, I got error: 


  InvalidArgumentException: Invalid translation language (id of language here) specified.


When I open page in default language it works fine. Does it mean that I need to translate all of the things in order to make it work?

My service:


namespace service;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use entity\Entity;

class PostTypeService
{
    public static function getEntititiesByPostType($postType, $entities = array(), $nid = NULL)
    {

        if ($nid != NULL) {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->condition('nid', $nid)
                ->execute();
        } else {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->execute();

        }

        $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
        $returnValues = array();
        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
        foreach ($nodes as $index => $node) {
            foreach ($entities as $entity) {
                $type = $entity->type;
                $entity_name = $entity->entity;
                if ($type == 'image') {
                    $returnValues[$postType][$index][$entity->entity] = file_create_url($node->$entity_name->entity->getFileUri());
                } else if ($type == 'url') {
                    $returnValues[$postType][$index][$entity->entity . '_uri'] = $node->$entity_name->uri;
                    $returnValues[$postType][$index][$entity->entity . '_title'] = $node->$entity_name->title;
                } else if ($type == 'list') {
                    foreach ($node->$entity_name as $list_item) {
                        $returnValues[$postType][$index][$entity->entity][] = $list_item->value;
                    }
                } else {
                    $translation = $node->getTranslation($language);
                    $returnValues[$postType][$index][$entity->entity] = $translation->$entity_name->$type;
                }
            }
            $returnValues[$postType][$index]['node_id'] = $index;
            $options = array('absolute' => TRUE);
            $url = Url::fromRoute('entity.node.canonical', ['node' => $index], $options)->toString();
            $returnValues[$postType][$index]['node_url'] = $url;
            $returnValues[$postType][$index]['lang'] = $language;
        }
        return $returnValues;
    }
This solves the problem:



namespace service;

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use entity\Entity;

class PostTypeService
{
    public static function getEntititiesByPostType($postType, $entities = array(), $nid = NULL)
    {



        if ($nid != NULL) {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->condition('nid', $nid)
                ->execute();
        } else {
            $nids = \Drupal::entityQuery('node')
                ->condition('type', $postType)
                ->condition('status', 1)
                ->execute();

        }

        $nodes = \Drupal\node\Entity\Node::loadMultiple($nids);


        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
        $defaultLang = \Drupal::languageManager()->getDefaultLanguage()->getId();

        $returnValues = array();
        foreach ($nodes as $index => $node) {

            if (!array_key_exists($language, $node->getTranslationLanguages())) {
                $translation = $node->getTranslation($defaultLang);
            } else {
                $translation = $node->getTranslation($language);
            }

            foreach ($entities as $entity) {
                $type = $entity->type;
                $entity_name = $entity->entity;
                if ($type == 'image') {
                    $returnValues[$postType][$index][$entity->entity] = file_create_url($translation->$entity_name->entity->getFileUri());
                } else if ($type == 'url') {
                    $returnValues[$postType][$index][$entity->entity . '_uri'] = $translation->$entity_name->uri;
                    $returnValues[$postType][$index][$entity->entity . '_title'] = $translation->$entity_name->title;
                } else if ($type == 'list') {
                    foreach ($translation->$entity_name as $list_item) {
                        $returnValues[$postType][$index][$entity->entity][] = $list_item->value;
                    }
                } else {
                    $returnValues[$postType][$index][$entity->entity] = $translation->$entity_name->$type;
                }
            }
            $returnValues[$postType][$index]['node_id'] = $index;
            $options = array('absolute' => TRUE);
            $url = Url::fromRoute('entity.node.canonical', ['node' => $index], $options)->toString();
            $returnValues[$postType][$index]['node_url'] = $url;
            $returnValues[$postType][$index]['lang'] = $language;
        }
        return $returnValues;
    }
}