Switching configuration language on-the-fly doesn't work due to caching

Created on 13 June 2019, almost 6 years ago
Updated 11 November 2023, over 1 year ago

Drupal::languageManager() has the great function setConfigOverrideLanguage(). Sometimes it can be very useful to switch the current config language to sth. different when trying to render mixed language content. For example on a Japanese site I want to render a specific entity (e.g. a Contact form) in English with English labels, English select options etc. I use getConfigOverrideLanguage() to save the current language and switch to English by calling setConfigOverrideLanguage().

Unfortunately, this doesn't work as expected. It turns out I have to call:

\Drupal::service("entity_field.manager")->clearCachedFieldDefinitions();

and:

drupal_static_reset('options_allowed_values');

to make this really work. This needs to be called after setConfigOverrideLanguage(), when changing to English and when changing back to Japanese. Of course, this is not really a good solution from a performance point-of-view, because the caches get lost unnecessarily on every switch.

When I check the source code of EntityFieldManager::clearCachedFieldDefinitions(), I see that EntityFieldManager does indeed respect at least the interface language for the CID but not the ConfigOverrideLanguage and not for the internal variables like $this->baseFieldDefinitions etc. Once a definition gets stored in there, EntityFieldManager always fetches it from the variable no matter if the ConfigOverrideLanguage has changed in the mean time.

Rendering mixed-language content should be easier IMHO.

πŸ› Bug report
Status

Closed: works as designed

Version

9.5

Component
Language systemΒ  β†’

Last updated 8 days ago

  • Maintained by
  • πŸ‡©πŸ‡ͺGermany @sun
Created by

πŸ‡©πŸ‡ͺGermany rgpublic DΓΌsseldorf πŸ‡©πŸ‡ͺ πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024