简体   繁体   English

如何在 Drupal 9 中使用 entityTypeManager 获取翻译后的术语?

[英]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.

相关问题 如何获取Drupal页面的术语ID? - how can i get term id of a page in drupal? 翻译的内容字段标签以原始语言显示在Drupal 8的节点页面中,该如何显示翻译的标签? - Translated content field labels are showing in the original language in the node page in Drupal 8, How can I display the translated labels instead? 在 Drupal 中如何获取 tnid 或已翻译节点的节点 ID? - In Drupal how to get tnid or the node id of the translated node? Drupal-如何使用taxonomy_get_term_by_name从名称中获取术语ID - Drupal - How to get term Id from name with taxonomy_get_term_by_name (Drupal 7) 如何修改 select 列表的现有术语参考? - (Drupal 7) How do I modify an existing term reference of a select list? 如此众多的教程指南,但如何过滤一个Drupal 7视图以显示具有分类术语的节点? - So Many Tutorial Guides, But How Can I Filter A Drupal 7 View To Show Nodes With A Taxonomy Term? 如何使用Pathauto模块使URL反映Drupal的术语结构? - How can I make URL reflecting Drupal's term structure realized by using Pathauto module? 如何使用SQL查询获取drupal 7中节点的实际文件? - How can I get the actual files of a node in drupal 7 with SQL query? 如何使用Drupal 7返回并获取JSON数据 - How I can return and get JSON data with Drupal 7 如何以编程方式获取Drupal模块名称? - How can I get Drupal module name programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM