function TermDevelGenerate::generateTermTranslation

Same name in other branches
  1. 5.x devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\TermDevelGenerate::generateTermTranslation()

Create translation for the given term.

Parameters

array $translate_language: Potential translate languages array.

\Drupal\taxonomy\TermInterface $term: Term to add translations to.

Return value

int Number of translations added.

1 call to TermDevelGenerate::generateTermTranslation()
TermDevelGenerate::generateTerms in devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php
Generates taxonomy terms for a list of given vocabularies.

File

devel_generate/src/Plugin/DevelGenerate/TermDevelGenerate.php, line 402

Class

TermDevelGenerate
Provides a TermDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function generateTermTranslation(array $translate_language, TermInterface $term) {
    if (is_null($this->contentTranslationManager)) {
        return 0;
    }
    if (!$this->contentTranslationManager
        ->isEnabled('taxonomy_term', $term->bundle())) {
        return 0;
    }
    if ($term->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED || $term->langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
        return 0;
    }
    $num_translations = 0;
    // Translate term to each target language.
    $skip_languages = [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        LanguageInterface::LANGCODE_NOT_APPLICABLE,
        $term->langcode->value,
    ];
    foreach ($translate_language as $langcode) {
        if (in_array($langcode, $skip_languages)) {
            continue;
        }
        $translation_term = $term->addTranslation($langcode);
        $translation_term->setName($term->getName() . ' (' . $langcode . ')');
        $this->populateFields($translation_term);
        $translation_term->save();
        $num_translations++;
    }
    return $num_translations;
}