Score:0

Get Translated Field Label for Entity

cn flag

I have a field I have translated. But I can not figure out how to get that translated field label programmatically. I've found a few posts on here, but none of them work for me.

Pretty easy to get the default field label:

$fieldDefinition = $model->get($field)->getFieldDefinition();
$fieldLabel = $fieldDefinition->getLabel();

Does anyone know a way to get that in 2022?

I thought the answer might lie in here somewhere, but so far no luck. Just trying to grab the spanish translation right now...

$language_manager = \Drupal::languageManager();

$language = $language_manager->getLanguage('es');
$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);

Any help would be great.

Score:0
cn flag

Config translation works in the background as override. So you don't need to do anything.

Unless have a very specific use cases, like sending an email in a different language. Then you can change the config override language, like the code you've shown so far, and reset it after you've sent the email:

use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\field\Entity\FieldConfig;

...

$language_manager = \Drupal::languageManager();

$language = $language_manager->getLanguage('es');
$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);

// load a new field definition object instead of referencing the one stored in the entity
// in case of a translated base field:
$field_definition = BaseFieldOverride::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
// in case of a translated configured field:
$field_definition = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);

$field_label = $field_definition->getLabel();

$language_manager->setConfigOverrideLanguage($original_language);
xpersonas avatar
cn flag
Yeah, I'm creating a spec chart component. So that's created with code, and not just a rendered field. Something must not be clicking. The code I have doesn't get the translated field label. This `$fieldDefinition->getLabel()` is always the same - english. I wish it was easy like `$fieldDefinition->getTranslation('es')->getLabel()` but obviously that doesn't work.
4uk4 avatar
cn flag
Are you sure you have a translation stored in the config? See this topic how to get and set language overrides on a lower level: https://drupal.stackexchange.com/questions/282995/how-to-get-translated-field-storage-config. It could also help to export the config and check the config names and the language overrides (exported to sub directories).
xpersonas avatar
cn flag
I do see it in when I export the config. It's in there. But still, no matter what I do, it's in english. My `$fieldDefinition` which is `Drupal\Core\Field\Entity\BaseFieldOverride` always shows the `langcode = "en"`. I just don't know where my disconnect is or what I'm not understanding. I really appreciate the help. I keep thinking something is going to click. It's super easy getting the field value though `$model->getTranslation($lang)->get($field)->getString()`.
4uk4 avatar
cn flag
Yes, this is a bit confusing. When loading a config with a language override the langcode is still the original language of the config object while the config values inside of the object are translated.
4uk4 avatar
cn flag
The field definition you get from the entity seems to be cached in memory. I edit the answer how to load the object in a different way.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.