Score:2

How to display translated fields labels of a node view

sa flag

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.

leymannx avatar
ne flag
Try to add `$entity = $entity->getTranslation('nl');` as second line. Even if not translatable this might provide the entity in the right context.
Baud avatar
sa flag
Thank you. As it is not translatable, this code generate an exception: `Invalid translation language (nl) specified.`
leymannx avatar
ne flag
Ah, okay, alright. Damn
Score:3
cn flag

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();
Baud avatar
sa flag
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.
4uk4 avatar
cn flag
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 avatar
cn flag
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.
Baud avatar
sa flag
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.
4uk4 avatar
cn flag
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 https://drupal.stackexchange.com/questions/303396/making-an-http-subrequest-causes-currentroutematch-to-have-the-wrong-route
Baud avatar
sa flag
Unfortunately, I have the same problem with the endpoint solution... see: https://drupal.stackexchange.com/q/314997/61398
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.