Drupal 8 以编程方式翻译段落

Drupal 8 translating paragraphs programmatically

段落应该在其组成字段的级别上进行翻译,而不是在 paragraph_field 级别上进行翻译。那么如何以编程方式翻译段落呢?

更明确地说,我的段落字段是不可翻译的,但组件字段是。那么如何加载节点、遍历段落项并向字段添加翻译?

有人举个例子吗? 谢谢。

以下 https://www.flocondetoile.fr/blog/translate-programmatically-drupal-8 节点翻译:

这是我实际代码的抽象,我还没有实际测试过:

$node = node_load(12);

if ($node->hasTranslation('de')) {
  $transl_node = $node->getTranslation('de');

  foreach ($transl_node->field_paragraph  => $paragraph) {
      $entity_array = $paragraph->toArray();
      $translated_fields = [];
        $translated_fields['field_body'] = array(
          'value' => 'translated value',
          'format' => 'full'
        );
        $translated_fields['field_section_title'] = 'translated section title';

        $translated_entity_array = array_merge($entity_array, $translated_fields);
        if (!$paragraph->hasTranslation('de')) {
          $paragraph->addTranslation('de', $translated_entity_array);
          $paragraph->save();
        }
  }
  $transl_node->save();
}