Last active
January 10, 2019 08:24
-
-
Save Erikdekamps/9330c3be15877731e9bad29473dd7aa1 to your computer and use it in GitHub Desktop.
Drupal 8 - Create taxonomy terms with translations programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The languages array. | |
$languages = [ | |
'dutch' => [ | |
'label' => 'Dutch', | |
'translations' => [ | |
'nl' => 'Nederlands', | |
'de' => 'Niederländisch', | |
], | |
], | |
'english' => [ | |
'label' => 'English', | |
'translations' => [ | |
'nl' => 'Engels', | |
'de' => 'Englisch', | |
], | |
], | |
'spanish' => [ | |
'label' => 'Spaans', | |
'translations' => [ | |
'nl' => 'Spaans', | |
'de' => 'Spanisch', | |
], | |
], | |
'italian' => [ | |
'label' => 'Italian', | |
'translations' => [ | |
'nl' => 'Italiaans', | |
'de' => 'Italienisch', | |
], | |
], | |
'german' => [ | |
'label' => 'German', | |
'translations' => [ | |
'nl' => 'Duits', | |
'de' => 'Deutsch', | |
], | |
], | |
'french' => [ | |
'label' => 'French', | |
'translations' => [ | |
'nl' => 'Frans', | |
'de' => 'Französisch', | |
], | |
], | |
'portugese' => [ | |
'label' => 'Portuguese', | |
'translations' => [ | |
'nl' => 'Portugees', | |
'de' => 'Portugiesisch', | |
], | |
], | |
'russian' => [ | |
'label' => 'Russian', | |
'translations' => [ | |
'nl' => 'Russisch', | |
'de' => 'Russisch', | |
], | |
], | |
'swedish' => [ | |
'label' => 'Swedish', | |
'translations' => [ | |
'nl' => 'Zweeds', | |
'de' => 'Schwedisch', | |
], | |
], | |
'chinese' => [ | |
'label' => 'Chinese', | |
'translations' => [ | |
'nl' => 'Chinees', | |
'de' => 'Chinesisch', | |
], | |
], | |
'arabic' => [ | |
'label' => 'Arabic', | |
'translations' => [ | |
'nl' => 'Arabisch', | |
'de' => 'Arabisch', | |
], | |
], | |
'japanese' => [ | |
'label' => 'Japanese', | |
'translations' => [ | |
'nl' => 'Japans', | |
'de' => 'Japanisch', | |
], | |
], | |
]; | |
// Loop through the languages. | |
foreach ($languages as $language) { | |
// Create the language term. | |
$term = Term::create([ | |
'name' => $language['label'], | |
'vid' => 'languages', | |
'langcode' => 'en', | |
]); | |
// Add all translations. | |
foreach ($language['translations'] as $langcode => $translation) { | |
$term->addTranslation($langcode, [ | |
'name' => $translation, | |
]); | |
} | |
// Save the term. | |
$term->save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment