2

As mentioned here and here, there is a (complicated) way, to get can get a translated labels for bundle fields.

use Drupal\field\Entity\FieldConfig;

// Override language before loading field configuration.
$language = \Drupal::languageManager()->getLanguage('fr');
\Drupal::languageManager()->setConfigOverrideLanguage($langu‌​age);

// $entity_type, $bundle, $field.
$field_info = FieldConfig::loadByName('entity_type', 'bundle', 'myField'); 

$label = $field_info->getLabel();

I however cannot find a way, how to achieve this for base fields. A hack like supplying a(any) bundle also leads to no success.

3

There are two possible translation methods for a base field, interface translation and configuration translation.

Base fields are defined in code and translated by t(), which gets the translatable string from the core interface translation table.

$translatableMarkup = t($label, [], ["langcode" => $langcode]);
//optionally, render to string
$translatedLabel = $translatableMarkup->render();

If a base field override exists in configuration, then it contains a translatable label:

// Override language before loading field configuration.
$language = \Drupal::languageManager()->getLanguage('fr');
\Drupal::languageManager()->setConfigOverrideLanguage($langu‌​age);

$field_override = BaseFieldOverride::loadByName('entity_type', 'bundle', 'myField');
if ($field_override) {
  $label = $field_override->getLabel();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.