Problem/Motivation

Translating field labels requires manually clearing all caches in order to translated field label to be updated to anonymous users. Translating other strings (e.g. "Home") does not need any manual cache clearing.

Steps to reproduce

1. Install Drupal with standard installation profile. Do not use any development settings like cache.backend.null or turn off Twig cache.
2. Enable following modules language, locale.
3. Add language (e.g. Finnish).
4. Create article node as admin user add some tag. This way the field label Tags becomes visible.
5. Visit the created article at /fi/node/1 as an anonymous user and translation for "Tags" field label, imported when language was added (3.) is rendered correctly.
6. As an admin user change translation for "Tags" field label at /admin/config/regional/translate and submit form.
7. As an anonymous user, visit /fi/node/1 and same translation as in 5. is still visible.

Also, even translated field labels on the Content type fields page (/admin/structure/types/manage/article/fields) show the original values, until all caches are cleared.

Proposed resolution

Updating field label translations should invalidate appropriate caches.

Need to discuss and review more general solution, if possible.

Original report:
--
I have a Drupal multilingual website, when I translate the label for a content type field, and I set the label to be shown in the Manage display options, it's shown in the source language when visiting a translation.

See http://stackoverflow.com/questions/35683639/translated-content-field-lab...

The problem doesn't always occur and seems to be cache related, setting the following lines in settings.local.php will disable caching and "solves" the problem:

$settings['class_loader_auto_detect'] = FALSE;
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Edit:
- as I'm using a node tease with views, this might be a views issue.

Workaround

Disable all caches during development, by turning on development mode with drush theme:dev on (new in Drush 13.6) which disables all caching (Drupal+Twig). (Toggle Twig debug, caching and CSS/JS aggregation with Drush)

Comments

mpp created an issue. See original summary.

mpp’s picture

Status: Active » Needs review
StatusFileSize
new573 bytes

This patch is a workaround, it will (re-)translate the label. This might be a views issue as the display I'm using is a node teaser in a views list.

mpp’s picture

Issue summary: View changes
mpp’s picture

Issue summary: View changes
mpp’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: 2824003-2.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

sokru’s picture

I can confirm that this is still an issue in Drupal 8.7.8, I'm facing this on user profile, so not views related.

sokru’s picture

StatusFileSize
new666 bytes

Spend many hours trying to figure out correct cache to flush, but ended up to obviously bad practice of flushing all caches on form submit. Hopefully someone else finds proper solution for this.

sokru’s picture

Component: transliteration system » locale.module
Issue tags: +Needs tests
sokru’s picture

Title: Content field labels are not being translated » Translating field labels does not invalidate cache
Issue summary: View changes
sokru’s picture

Issue summary: View changes
sokru’s picture

Issue summary: View changes
manafov_mehman’s picture

Assigned: Unassigned » manafov_mehman

as there is no way to know if the translated string is in a node or not so you don't have any other options besides clearing the whole cache. Clearing cache is a time consuming and resource intensive each time a string is translated, so i think this is a desired behavior

manafov_mehman’s picture

Assigned: manafov_mehman » Unassigned

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kostask’s picture

I had the same problem and clearing the Plugin-cache translated the field label.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Can we try clearing just the plugin cache?

yogeshmpawar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new899 bytes
new865 bytes

Trying to clear plugin cache.

Status: Needs review » Needs work

The last submitted patch, 27: 2824003-27.patch, failed testing. View results

yogeshmpawar’s picture

Status: Needs work » Needs review
StatusFileSize
new675 bytes
new1009 bytes

Updated patch will resolve test failures.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Did not test

Moving to NW for test cases

fathima.asmat’s picture

#29 doesn't work for me when translating taxonomy or node field labels. Technically clearing entity_field.manager cache should do the trick, however it does not unfortunately. Only the combination of clearing render, page cache along with the entity field manager work for me. Though I don't like this workaround, I'm using this as an interim solution for my projects until a proper fix is released.

I have got these in config_translation/src/Form/ConfigTranslationFormBase.php:

\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
\Drupal::service('plugin.cache_clearer')->clearCachedDefinitions();
\Drupal::service('cache.render')->invalidateAll();
\Drupal::service('cache.dynamic_page_cache')->invalidateAll();

Attached a patch if anyone wants to use the workaround for now.

fathima.asmat’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Was previously tagged for tests which still needs to happen. Not ready for review.

ayush.khare’s picture

StatusFileSize
new4.33 KB

Fixed CCF in #33.

mohit_aghera’s picture

Issue summary: View changes
StatusFileSize
new4.9 KB

I got the slightly different results with the test case.
Cache was not invalidated with the services used in #33 or #36

I was able to make it work using following cache invalidation

        \Drupal::service('cache.dynamic_page_cache')->invalidateAll();
        \Drupal::service('cache.render')->invalidateAll();
        \Drupal::service('cache.discovery')->invalidateAll();
        \Drupal::service('cache.page')->invalidateAll();

I feel that invalidating the cache that way isn't a good idea.
Probably we should emit the cache tags related to field_instance config whenever title is visible and invalidate those cache tags.

I tried to do that, however, this wasn't working as expected. Tried using following approach:
- Added a dummy cache tag called "field_config" in view() method of FormatterBase.php class.
$elements['#cache']['tags'][] = 'field_config';

- Later in ConfigTranslationFormBase.php's submit() callback, I tried to invalidate cache tag along with discovery cache. See following snippet.

    Cache::invalidateTags(['field_config']);
    \Drupal::cache('discovery')->invalidate('entity_bundle_field_definitions:node:article:fr');

Since this isn't a generic approach, I haven't added that in the patch. Because all the configurations are not field config etc.
Probably someone more experienced with configuration api/translation API can chime in and throw other ideas.

Meanwhile, I've added a test-only patch so others can validate that when we do further work.
This is failing for me on local.

Keeping this in needs work since the discussion related to approach is required.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ressa’s picture

Priority: Normal » Major
Issue summary: View changes

I ran into this, and was puzzled that only field labels from one language were getting used in rendered html in Solr. Turning on development mode with drush theme:dev on (new in Drush 13.6) which disables all caching (Drupal+Twig) and refreshing fixed it, and the correct, translated labels were shown, both under the content type, and in the rendered html in Solr.

I would say this is a pretty big deal, so maybe raising the Status level a notch could be considered?

PS. Something else I noticed: If you enter a new value in a Label field and press Enter, it does not save the form, even though there seems to be a page reload, and the value stays the new one. But if you revisit the form, the original value is still there. Perhaps there's an issue for this already, or we need to create one?

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.