2

I would like to display a node in NL. This node is not translatable but I would like to have the fields labels translated in NL.

This rendering occurs in a cron job and is used to render an email in the recipient language (ex: NL).

Unfortunately, the following code output the labels in the current language of the page (if I run it outside the cron) or in the default language of the site from within the cron job

$entity = Drupal\node\Entity\Node::load($nid);
$view_builder = Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$summary = $view_builder->view($entity, 'summary', 'nl');
$render=Drupal::service('renderer')
->renderPlain($summary);
dpm($render);

Solutions I have already tested:

1-I have tried the solution which set the config override language to the desired language here without luck

2-I have also tried the solution which set a custom language negotiator here without success

3-Finally I have tried the solution which use the 'language' session parameter here without any result

I have traced the code and I think that the language is selected/choose in the render part... But it was really difficult to understand this due to the complexity.

I was also thinking to add a '#langcode' to the build but I couldn't find an example.

3
  • Try to add $entity = $entity->getTranslation('nl'); as second line. Even if not translatable this might provide the entity in the right context.
    – leymannx
    Mar 6, 2023 at 20:44
  • Thank you. As it is not translatable, this code generate an exception: Invalid translation language (nl) specified.
    – Baud
    Mar 7, 2023 at 8:08
  • Ah, okay, alright. Damn
    – leymannx
    Mar 7, 2023 at 16:02

1 Answer 1

3
+100

To display translated field labels you need to add solution 1 to the code you have already and also clear the cached field definitions, which unfortunately don't cache per config language.

The complete code should look like this:

$language_manager = \Drupal::languageManager();

$nid = 1;
$langcode = 'nl';
$language = $language_manager->getLanguage($langcode);

$original_language = $language_manager->getConfigOverrideLanguage();
$language_manager->setConfigOverrideLanguage($language);
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
drupal_static_reset('options_allowed_values');

$entity = Node::load($nid);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
$summary = $view_builder->view($entity, 'summary', $langcode);
$render = \Drupal::service('renderer')
  ->renderPlain($summary);

$language_manager->setConfigOverrideLanguage($original_language);
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
6
  • Many thanks!!! This is working but with a side affect: after running this code, if I display /fr/node/xxx/edit field labels are in dutch (this issue arise for other kind of elements/pages but not everywhere, node/edit is just an example and sometimes it is in french as expected). A cache clear solve this. Another issue: this is not working for computed fields added in entity_bundle_field_info(): labels are not translated. And I am wondering if this code is suitable to send, by cron, +/-15 notifications each 7min knowing the fact that the code runs for each notification.
    – Baud
    Mar 8, 2023 at 11:52
  • 1) Run clearCachedFieldDefinitions() again, see the edit. - 2) For labels defined in code try solution 2 (custom language negotiator). 3) You could cache the rendered summary with your own cache key containing the custom email language, invalidated by the node cache tag as any other rendered node.
    – 4uk4
    Mar 8, 2023 at 12:31
  • 1
    Instead of all this, a different approach would be to make a sub-request or a guzzle request. The endpoint could be a path with language prefix for a controller returning the summary as already rendered response, bypassing the themed page.
    – 4uk4
    Mar 8, 2023 at 12:35
  • The endpoint looks nice... I never use this Technic. I will make some test and I assume it will solve the computed fields issue. again, many thanks for your help.
    – Baud
    Mar 8, 2023 at 13:28
  • If you want to go this route, this discussion might be helpful to choose sub-request vs guzzle and to work around a core issue drupal.stackexchange.com/questions/303396/…
    – 4uk4
    Mar 8, 2023 at 13:44

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.