I have Drupal 9.4.5 site. I've been hunting a problem where translations in the wrong language show up for guest users, after each release.
- Default language: English
- Interface Language Detection: Only method "Selected language" (Language based on a selected language) is checked with value: German
The intention is that this site uses only German as language. We use English as default language, a) because that seems to work better for Drupal and b) because we have some English-speaking developers on the team. Everything gets translated to German. This only concerns texts translated via t() in the templates.
The problem: After a new release is deployed to the platform.sh installation, some blocks show English translations. This only happens for guest users, everything is correctly translated for logged-in users. So it's not a problem of a missing translation. Mainly it happens with a combination of blocks and views. One is a block generated by a view, the other custom block uses a view internally.
Remedy so far: The English texts go away on cache clear. I'm not sure why this helps, as the last step of installing the release is already a "drush cr".
So far, I haven't been able to reproduce it locally and I'm not aware of the probem showing up without a release happening first. I tried to disable the platform.sh cache for a specific route, but after the next release this route showed English texts again.
So this seems to be a cache issue in some way. I'm looking for an idea why Drupal would ever render an English text on a German page in the first place!? There is no language switch available and no language part of the URL. Any idea is welcome.
Update 29.09.: By now I have added debug output to the template and I really think this is a problem of Drupal outputting English translations when it should output the German text.
My current test case:
- A block bundle using an entity reference field to allow the user to pick entities to be displayed.
- The block's display settings is set to "referenced entity" with the display mode "teaser".
- There is no class for the block.
- There is a template in the theme for the entities being displayed. It contains this code:
{% trans %}
{{ comment_count }} comment
{% plural comment_count %}
{{ comment_count }} comments
{% endtrans %}
In the preprocess function for the entity, I added this:
$variables['current_language_content'] = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$variables['current_language_interface'] = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
And this in the template, directly beneath the comment count output:
<!-- current language content: {{ current_language_content }}, current language interface: {{ current_language_interface }} -->
<!-- just as is: {{ "Join now!"|t }} -->
<!-- explicitly de: {{ "Join now!"|t({'langcode': 'de'}) }} -->
Now I have a page with four German teasers and one English teaser, and all of them show this in the source code: <!-- current language content: de, current language interface: de -->
The two extra translated Strings show the correct German translation for the first four teasers and then for the fifth:
<!-- current language content: de, current language interface: de -->
<!-- just as is: Join now! -->
<!-- explicitly de: Join now! -->
This test String is shown to be correctly translated in the UI interface and is not part of any config file.
There might be caching issues involved, but this seems to be broken on a level beneath the cache. Also, we had one instance yesterday were it needed multiple cache clears and some hours before the texts turned German again.
Sooo... Is there any other language setting that I can check? Are there known bugs of the translation method?