Risposte:
Usa il seguente codice:
$curr_langcode = \Drupal::languageManager()->getCurrentLanguage(\Drupal\Core\Language\LanguageInterface::TYPE_CONTENT)->getId();
// retrieve term
$taxonomy_term = \Drupal\taxonomy\Entity\Term::load($tid);
// retrieve the translated taxonomy term in specified language ($curr_langcode) with fallback to default language if translation not exists
$taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode);
// get the value of the field "myfield"
$myfield_translated = $taxonomy_term_trans->myfield->value;
Dovresti (devi) usare il servizio invece nella prima riga per language_manager. Inoltre accorcerei il codice usando i tag use.
Da qualche parte all'inizio del file:
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Language\LanguageInterface;
e più avanti nel codice in alcune funzioni
$curr_langcode = \Drupal::service('language_manager')->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
// Retrieve term.
$taxonomy_term = Term::load($tid);
// Retrieve the translated taxonomy term in specified language
// ($curr_langcode) with fallback to default language if translation not
// exists.
$taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode);
// Get the value of the field "myfield".
$myfield_translated = $taxonomy_term_trans->myfield->value;
I frammenti sopra restituiranno anche termini non tradotti. Devi verificare se un termine è tradotto con la funzione hasTranslation:
$vocabulary = 'MY_VOCABULARY_NAME';
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', $vocabulary);
$query->sort('weight');
$tids = $query->execute();
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
$termList = array();
foreach($terms as $term) {
if($term->hasTranslation($language)){
$translated_term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
$tid = $term->id();
$termList[$tid] = $translated_term->getName();
}
}
// To print a list of translated terms.
foreach($termList as $tid => $name) {
print $name;
}
Per collegare i tag alla loro pagina dei termini: Vedi: Ottieni termini di tassonomia