4

I am working on a multilingual site right now, and am using the Internationalization suite of modules to build it out.

The site has some custom blocks in it that I would normally use DRUPAL_CACHE_GLOBAL in the hook_block_info() definition. However, some of these will have translatable strings in them, so I really need to cache them per-language.

Is this possible? Or, is my only real option to use DRUPAL_CACHE_PER_PAGE?

3
  • Could you use DRUPAL_CACHE_CUSTOM? I've not had much experience with multilingual sites, but I suppose in theory you could cache the content in a custom language based cache (e.g. cache_set/cache_get with mpd_awesome_site_fr, mpd_awesome_site_ru)
    – Chapabu
    Commented Sep 6, 2013 at 8:52
  • @Chapabu I am considering that. I have just seen very little use of it in the wild (the core forum module is the only place I have ever seen it), so I don't really know how much of a "best practice" it is.
    – mpdonadio
    Commented Sep 6, 2013 at 14:02
  • This module on GitHub seems to do some multilingual block caching stuff of it's own - don't know if it's helpful
    – Chapabu
    Commented Sep 6, 2013 at 14:26

1 Answer 1

6

Just use DRUPAL_CACHE_GLOBAL. On multilingual sites the users language is part of the (cid), i.e. the key used when storing/fetching entries to/from the block cache.

The function responsible for this behavior is drupal_render_cid_parts which is called from _block_get_cache_id.

You can embed the following fragment into a custom PHP block in order to verify the mechanism:

$fakeblock = (object) array(
  'module' => 'some-module',
  'delta' => 'some-delta',
  'cache' => DRUPAL_CACHE_GLOBAL,
);
print _block_get_cache_id($fakeblock);

On a multilingual site, the fragment should print something like:

some-module:some-delta:bartik:en

1
  • Thanks. Didn't catch that when I traced everything out the first time.
    – mpdonadio
    Commented Nov 4, 2013 at 21:49

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.