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->view($entity, 'compact_teaser', 'de');
 $rendered_teaser = $renderer->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' => $params['account']);

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

  // Restore the language manager.
  • 1
    Welcome to Drupal Answers! I added the link to the documentation page, and the relevant code. I hope I got the relevant one. – kiamlaluno May 28 '16 at 6:27

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.