Programatically render a node in a specific language I want to render a node display programmatically for sending it to a search engine. It works ok but is ignoring the language and renders the field labels in the source language (which is English).

I use the following code for creating the teaser markup.

 $render_array_teaser = $view_builder-&gt;view($entity, 'compact_teaser', 'de');
 $rendered_teaser = $renderer-&gt;renderPlain($render_array_teaser);


The result gives me a teaser that includes a field named "field_sku" that has the label "SKU number". If I output the teaser in the browser the label gets correctly translated to German = "Artikelnummer" if the current language is "German".

If I render the teaser with the above code the label stays English even if I set the langcode parameter to 'de'.

How to get the teaser with German lables?
The field labels are used from the config translation system. You also need to set the config override language. See user_mail() in core.

  // Temporarily alter the language manager.
  $language_manager = \Drupal::languageManager();
  $langcode = $message['langcode'];
  $variables = array('user' =&gt; $params['account']);

  $language = \Drupal::languageManager()-&gt;getLanguage($params['account']-&gt;getPreferredLangcode());
  $original_language = $language_manager-&gt;getConfigOverrideLanguage();
  $language_manager-&gt;setConfigOverrideLanguage($language);
  $mail_config = \Drupal::config('user.mail');

  // Restore the language manager.
  $language_manager-&gt;setConfigOverrideLanguage($original_language);