How to programmatically create entity with certain language applied to fields? I am programmatically creating some content during profile installation and since Drupal uses "en" langcode during this period I am getting the language from config and it is working fine.

The issue I have is that I am creating a node and I will end up with it being in English. So I have set the "langcode" property of the node during creation which worked as expected. But the fields are not responding to this fact(I have only the body field attached).

So I have tried to key the value(s) by the langcode(Drupal 7 style) but it resulted in the field not being populated in DB at all.

The node entity nor field item list nor field item have methods for setting the langcode so I am unsure how to proceed with this.

Any ideas?

This is my code:

// Create 'terms of service' page
  $tos = entity_create('node', [
    'type' =&gt; 'page',
    'title' =&gt; t('Terms of service', [], ['langcode' =&gt; $langcode]),
    'uid' =&gt; 1,
    'status' =&gt; 1,
    'langcode' =&gt; $langcode, // this is working on node only
    'defaultLangcode' =&gt; $langcode, // no change on field
    'activeLangcode' =&gt; $langcode, // no change on field
    'body' =&gt; [
      $langcode =&gt; [ // this is not working
        [
          'value' =&gt; '&lt;p&gt;' . t('This is a placeholder. Please edit this page and use your own text.', [], ['langcode' =&gt; $langcode]) . '&lt;/p&gt;',
          'summary' =&gt; '',
          'format' =&gt; FULL_HTML_FORMAT
        ]
      ]
    ]
  ]);
  $tos-&gt;save();




I have found setLangcode() method on the FieldItemList but that didn't work as well.



I have tried to save the node without body, then set the body and save the node again, but the field is in English again(and the node in installed language). Is it possible that this is kind of a (edge case)bug that relates to language manager?



I have checked the entity constructor and this is also not working:

'body' =&gt; [
      $langcode =&gt; [
        [
          'value' =&gt; [
            'value' =&gt; 'some text',
            'summary' =&gt; '',
            'format' =&gt; FULL_HTML_FORMAT
          ]
        ]
      ]
    ]
I have found a solution.
As I have mentioned this was during the profile installation process and it is related to How to get correct language code during installation?

The solution was to make sure the installed language exists before the modules are being installed and set it as default. Normally, translation are the last installation task. So I have copied the following code from install_download_additional_translations_operations() into a task in my profile that runs before modules are installed.

if ($langcode != 'en') {
    \Drupal::service('module_installer')-&gt;install(['language']);
    if (!($language = entity_load('configurable_language', $langcode))) {
      // Create the language if not already shipped with a profile.
      $language = ConfigurableLanguage::createFromLangcode($langcode);
    }
    $language-&gt;save();

    \Drupal::service('language.default')-&gt;set($language);
  }


I have also removed the $langcode from my code since translations were now working as expected.