4

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?

0

2 Answers 2

9

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();
  $language_manager->setConfigOverrideLanguage($language);
  $mail_config = \Drupal::config('user.mail');

  // Restore the language manager.
  $language_manager->setConfigOverrideLanguage($original_language);
1
  • 1
    Welcome to Drupal Answers! I added the link to the documentation page, and the relevant code. I hope I got the relevant one.
    – avpaderno
    Commented May 28, 2016 at 6:27
0

Seems like this do not works anymore - see https://www.drupal.org/project/drupal/issues/3061599 :-(

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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