[英]How can I get the translated term with entityTypeManager in Drupal 9?
This is the code.这是代码。 How can I get
$tree
translated to the langage inserted in $langage
.如何将
$tree
翻译成插入$langage
的语言。
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('secteur', 0, 1, TRUE);
foreach ($tree as $term) {
$id = $term->get('tid')->value;
$result[$id] = $term->get('name')->value;
}
$variables['parentterms'] = $result;
Thanks in advance.提前致谢。
You can use EntityRepository::getTranslationFromContext()
:您可以使用
EntityRepository::getTranslationFromContext()
:
Gets the entity translation to be used in the given context.
获取要在给定上下文中使用的实体翻译。
This will check whether a translation for the desired language is available and if not, it will fall back to the most appropriate translation based on the provided context.
这将检查所需语言的翻译是否可用,如果没有,它将根据提供的上下文回退到最合适的翻译。
$entityRepository = \Drupal::service('entity.repository');
foreach ($tree as $term) {
$id = $term->get('tid')->value;
$result[$id] = $entityRepository->getTranslationFromContext($term, $language)
}
Also, since the language of the current context defaults to the current content language, you don't need to set $language
.此外,由于当前上下文的语言默认为当前内容语言,因此您无需设置
$language
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.