1

Drupal does cache Twig templates. Normally, it is a good thing, as long as you don't have a multilingual site.

In my case every t()is not working as the template is cached in the wrong in English, giving German no chance to ever be outputted.

Is this the proper way to deal with t() calls?

 $settings['cache']['bins']['render'] = 'cache.backend.null'; 
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

It seems a bit wrong. As I understand these settings are more for the development process, not for production. Also there really should be a setting to use caching in a language specific way.

1
  • Have you thought of including the language in cache tags? Commented Dec 19, 2016 at 16:56

2 Answers 2

1

In Drupal cache content is language specific. Default is a cache context for the language. Sometimes you need to clear the cache one more time, but then this should work.

You can check this in the database table cache_render. All cid's should contain [languages:language_interface]=langcode and if you visit the same page in different languages you should see multiple cache entries.

Disabling the cache with a null backend is only for development. For production you need to find a solution for the language specific caching issue.

1
  • Thanks for your answer. I have seen that block do have two types of caches. One for the build array and another one for the block itself (theme, label). How to set the build cache I understand - do you know how to set the other one?
    – mogio
    Commented Dec 20, 2016 at 20:13
1

You can pass options to t, specifically the langcode. I believe that would look something like this:

{{ 'Your text here'|t({}, {'langcode' => node.language().getId()} }}

Untested, so the syntax might be slightly different. Also not sure what woud happen if you pass in a node that has a language like "not specified", it would probably fail.

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.